Using Fixed Binary

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:

    b64buffer = store.encode_base_64('1234567890')
     
    row_d = {'uid' : 0,
             'myFixedByteArray' : b64buffer
            }
    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 fixed binary field, use Store.decode_base_64():

    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.")
            b64buffer = row['myFixedByteArray']
            print store.decode_base_64(b64buffer)
    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)