Akshay
Akshay

Reputation: 359

Inconsistent handling property values in JanusGraph

Getting some problem while playing with JanusGraph. I am new in JanusGraph recently I have installed and followed the documentation for add vertex with its properties and few more but when I am trying to insert a property of float digit and set the key as "abc" and value would be 9.5f in this case I got an error but in the same query when I change the key to "a" or something else it works fine.

Example with key abc

g.addV("T22").property("abc", 9.5f)

Error

Value [9.5] is not an instance of the expected data type for property key [abc] and cannot be converted. Expected: class java.lang.Integer, found: class java.lang.Float

Example with key a

g.addV("T22").property("a", 9.5f) working fine

g.V(163848208).valueMap() {a=[10.5]}

updated

enter image description here

Got the same error again the error occurred only with 2 properties keys

  1. abc
  2. Mailing_Code

Upvotes: 3

Views: 1277

Answers (1)

Jason Plurad
Jason Plurad

Reputation: 6792

By default, JanusGraph uses an automatic schema maker. When a new property key is used, it will define a property key with its data type based on the value. In your scenario, it sounds like the first usage of abc used Integer rather than Float. Here's an example recreating your scenario:

gremlin> JanusGraph.version()
==>0.2.0
gremlin> graph = JanusGraphFactory.open('inmemory')
==>standardjanusgraph[inmemory:[127.0.0.1]]
gremlin> g = graph.traversal()
==>graphtraversalsource[standardjanusgraph[inmemory:[127.0.0.1]], standard]
gremlin> g.addV("T22").property("abc", 9).iterate()
gremlin> g.tx().commit()
==>null
gremlin> g.addV("T22").property("abc", 9.5f).iterate()
Value [9.5] is not an instance of the expected data type for property key [abc] and cannot be converted. Expected: class java.lang.Integer, found: class java.lang.Float

After a property key is defined, its data type cannot be changed. As described in the docs:

It is strongly encouraged to explicitly define all schema elements and to disable automatic schema creation by setting schema.default=none in the JanusGraph graph configuration.

Doing so will give you better control over the schema that is created. Here's an example on how to do that:

gremlin> graph = JanusGraphFactory.build().
......1>     set('storage.backend', 'inmemory').
......2>     set('schema.default', 'none').
......3>     open()
==>standardjanusgraph[inmemory:[127.0.0.1]]
gremlin> mgmt = graph.openManagement()
==>org.janusgraph.graphdb.database.management.ManagementSystem@46aa712c
gremlin> mgmt.makeVertexLabel('T22').make()
==>T22
gremlin> mgmt.makePropertyKey('abc').dataType(Float.class).make()
==>abc
gremlin> mgmt.commit()
==>null
gremlin> g = graph.traversal()
==>graphtraversalsource[standardjanusgraph[inmemory:[127.0.0.1]], standard]
gremlin> g.addV('T22').property('abc', 9).iterate()
gremlin> g.tx().commit()
==>null
gremlin> g.addV('T22').property('abc', 9.5f).iterate()
gremlin> g.tx().commit()
==>null
gremlin> g.V().values('abc').map{ [ it.get(), it.get().getClass().getName() ] }
11:29:34 WARN  org.janusgraph.graphdb.transaction.StandardJanusGraphTx  - Query requires iterating over all vertices [()]. For better performance, use indexes
==>[9.5,java.lang.Float]
==>[9.0,java.lang.Float]

Here is a link to the documentation on JanusGraph schema which has more information.

Upvotes: 3

Related Questions