Search code examples
arraysschemablobjanusgraph

What data type is required to store a byte array in JanusGraph?


When automatic schema creation is on, I am able to store and retrieve byte arrays in properties, like:

byte[] myArray = new byte[10];
vertex.property("blob", myArray);
myArray = vertex.value("blob");

However I don't seem to find the correct property definition for when automatic is off. I've already tried Byte.class with Cardinality.LIST, it comes up with:

org.janusgraph.core.SchemaViolationException: Value [[B@a9153e9] is not an instance of the expected data type for property key [payload] and cannot be converted. Expected: class java.lang.Byte, found: class [B

Also Byte[].class is not supported.

Caused by: java.lang.IllegalArgumentException: Not a supported data type: class [Ljava.lang.Byte;

Solution

  • It's not explicitly mentioned in the JanusGraph docs, but you can just define the data type as byte[] like this:

    gremlin> mgmt = graph.openManagement()
    ==>org.janusgraph.graphdb.database.management.ManagementSystem@6cae2e4d
    gremlin> mgmt.makePropertyKey('blob').dataType(byte[].class).cardinality(Cardinality.SINGLE).make()
    ==>blob
    gremlin> mgmt.commit()
    ==>null
    

    Now, you can use it in your Gremlin traversals:

    gremlin> g = graph.traversal()
    ==>graphtraversalsource[standardjanusgraph[berkeleyje:/opt/janusgraph/conf/../db/berkeley], standard]
    gremlin> g.addV().property('blob', new byte[10])
    ==>v[4272]
    gremlin> g.V(4272).values('blob').next()
    ==>[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    

    Note however that arrays are currently unfortunately not supported via GraphSON in JanusGraph. So, you can basically only use them from Java with Gryo as the serializer right now. See JanusGraph/janusgraph#1295 for more information.

    Cardinality.LIST isn't intended for storing an array of values, but for cases where you just want to have multiple values for the same property, like multiple different addresses for a user for example.