JanusGraph給Label添加載字段屬性時報org.janusgraph.core.SchemaViolationException異常

經過

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的列表。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章