Using Binary

You can declare a field as binary using the BINARY statement. You then read and write the field value as a base64 encoded buffer.

If you want to store a large binary object, then you should use the LOB APIs rather than a binary field, which are only available using the Java Key/Value API. For information on using the LOB APIs, see the Oracle NoSQL API Large Object API introduction.

Note that 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 Fixed Binary for information on the fixed binary datatype.

To define a simple two-field table where the primary key is a UID and the second field contains a binary field, you use the following statement:

CREATE TABLE myTable (
    uid INTEGER,
    myByteArray BINARY,
    PRIMARY KEY(uid)
) 

CHECK, DEFAULT and NOT NULL constraints are not supported for binary values.

To write the binary field, use a base64 encoded byte array.

   var file64 = new Buffer(fs.readFileSync('image.jpg'))
      .toString('base64');

   var row = { uid: 0,
               myByteArray: file64}
   store.put('myTable', row,
           function (err) {
                if (err)
                    throw err;
                else {
                    console.log("Row inserted.");
                }
           }); 

To read the binary field, retrieve it as you would any data field. Here, we base64 decode the store object, and stream it to disk without storing it in memory.

For example:

   var primaryKey = {uid: 0};
   store.get('myTable', primaryKey,
           function (err, returnRow) {
                if (err)
                    throw err;
                else {
                    fs.writeFileSync('image-retrieved.jpg',
                        returnRow.currentRow.myByteArray, 
                        'base64');
                    store.close();
                }
           });