You can declare a fixed binary field using the
BINARY()
statement. When you do this, you
must also specify the field's size in bytes. You then read and
write the field value using a base64 encoded buffer. However, if the
buffer does not equal the specified size, then
IllegalArgumentException
is thrown when
you attempt to write the field.
If you want to store a large binary object, then you should use the LOB APIs rather than a binary field. For information on using the LOB APIs, see Oracle NoSQL API Large Object API.
Fixed binary should be used over the binary datatype any time you know that all the field values will be of the same size. Fixed binary is a more compact storage format because it does not need to store the size of the array. See Using Binary for information on the binary datatype.
To define a simple two-field table where the primary key is a UID and the second field contains a fixed binary field, you use the following DDL statement:
CREATE TABLE myTable ( uid INTEGER, myFixedByteArray BINARY(10), PRIMARY KEY (uid) )
CHECK
, DEFAULT
and NOT NULL
constraints are not supported for binary values.
To write the byte array:
var fixed64 = new Buffer('1234567890').toString('base64'); var row = {uid: 0, myFixedByteArray: fixed64 }; store.put('myTable', row, function (err) { if (err) throw err; else { console.log("Row inserted."); } });
To read the fixed binary field, use a base64 encoded buffer:
var primaryKey = {uid: 0}; store.get('myTable', primaryKey, function (err, returnRow) { if (err) throw err; else { var fba = returnRow.currentRow.myFixedByteArray; var buf = new Buffer(fba, 'base64').toString('utf-8'); console.log(buf); store.close(); } });