经过
janusgraph没有表的概念,但有类似表的schema的定义,称为label
。如:我们可以通过graph.openManagement().makeVertexLabel().make()
方法来创建顶点label。那么存在了类似表的label
,是否就有类似表字段类似的定义,答案是肯定的。janusgraph中用propertykey的定义来替代常用表的属性作为解决方案。
但是和关系型数据库不一样的是,janusgraph的字段是统一维护在一个池
中。可以简单地把这个池
理解为一个集合,而这个集合,维护了该图的所有label的字段属性。
我们可以通过执行如下代码,打印这个图的池
维护的所有字段。
StandardJanusGraph graph = GraphUtil.getStandardGraphFactory();
JanusGraphManagement mgt = graph.openManagement();
String s4 = mgt.printPropertyKeys();
System.out.println(s4);
打印信息如下:
然后我们可以通过执行如下语句,给该 label进行属性添加。
@Test
public void testInsertDateData() {
StandardJanusGraph standardGraphFactory = GraphUtil.getStandardGraphFactory();
JanusGraphManagement mgmt = standardGraphFactory.openManagement();
VertexLabel person = mgmt.makeVertexLabel("person").make();
PropertyKey name = mgmt.makePropertyKey("name4").dataType(String.class).cardinality(Cardinality.SET).make();
PropertyKey birthDate = mgmt.makePropertyKey("birthDate").dataType(Date.class).cardinality(Cardinality.SINGLE).make();
mgmt.addProperties(person, name, birthDate);
mgmt.commit();
}
不过在执行的时候,报异常了。
异常信息
org.janusgraph.core.SchemaViolationException: Adding this property for key [~T$SchemaName] and value [rtname4] violates a uniqueness constraint [SystemIndex#~T$SchemaName]
at org.janusgraph.graphdb.transaction.StandardJanusGraphTx.addProperty(StandardJanusGraphTx.java:817)
at org.janusgraph.graphdb.transaction.StandardJanusGraphTx.addProperty(StandardJanusGraphTx.java:745)
at org.janusgraph.graphdb.transaction.StandardJanusGraphTx.makeSchemaVertex(StandardJanusGraphTx.java:873)
at org.janusgraph.graphdb.transaction.StandardJanusGraphTx.makePropertyKey(StandardJanusGraphTx.java:893)
at org.janusgraph.graphdb.types.StandardPropertyKeyMaker.make(StandardPropertyKeyMaker.java:98)
at cn.haizhi.bigdata.graph.client.DataInsertTest.testInsertDateData(DataInsertTest.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
问题排查
如上,我们已经知道,在janusgraph的该图中的池
中,已经有name4
这个字段了,所以会报异常。
结论
- 同一个字段,只能被建立一次,即使是不同的
label
下创建。 - janusgraph中,图一个图下(在存储上表现为hbase的表,不同的图用的是不同的hbase表),只维护一份propertyKey的列表。