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 the Store.encode_base_64() method to encode the data before writing it to the store.

    iFile = open("image.jpg")
    image = store.encode_base_64(iFile.read())
    iFile.close()
     
    row_d = {'uid' : 0,
             'myByteArray' : image
            }
    try:
        store.put("myTable", row_d)
        logging.debug("Store write succeeded.")
    except IllegalArgumentException, iae:
        logging.error("Could not write table.")
        logging.error(iae.message)
        sys.exit(-1) 

To read the binary field, retrieve it as you would any data field. Use Store.decode_base_64() to decode the data before writing it to disk.

For example:

    try:
        primary_key_d = {"uid" : 0}
        row = store.get("myTable", primary_key_d)
        if not row:
            logging.debug("Row retrieval failed")
        else:
            logging.debug("Row retrieval succeeded.")
            image = store.decode_base_64(row['myByteArray'])
            iFile = open("out.jpg", 'w')
            iFile.write(image)
            iFile.close()
    except IllegalArgumentException, iae:
        logging.error("Row retrieval failed.")
        logging.error(iae.message)
        return
    except KeyError, ke:
        logging.error("Row display failed. Bad key: %s" % ke.message)