Scala 中的 JanusGraph 索引
JanusGraph indexing in Scala
我正在使用 Spark 从数据流中创建 JanusGraph,但在索引和创建属性时遇到问题。我想通过名为 "register_id" 的顶点 属性 创建索引。我不确定我的做法是否正确。
所以,这是我的代码:
var gr1 = JanusGraphFactory.open("/Downloads/janusgraph-cassandra.properties")
gr1.close()
// This is done to clear the graph made in every run.
JanusGraphFactory.drop(gr1)
gr1 = JanusGraphFactory.open("/Downloads/janusgraph-cassandra.properties")
var reg_id_prop = gr1.makePropertyKey("register_id").dataType(classOf[String]).make()
var mgmt = gr1.openManagement()
gr1.tx().rollback()
mgmt.buildIndex("byRegId", classOf[Vertex]).addKey(reg_id_prop).buildCompositeIndex()
当我 运行 以上时,我收到一条错误消息:
"Vertex with id 5164 was removed".
此外,我如何检查我是否在 Scala 中有具有特定 属性 的顶点。我知道在 gremlin 中,g.V().has('name', 'property_value')
有效,但无法弄清楚如何在 Scala 中执行此操作。我试过 Gremlin-Scala 但似乎找不到它。
任何帮助将不胜感激。
您应该使用 mgmt
对象来构建架构,而不是 graph
对象。您还需要确保 mgmt.commit()
架构更新。
gr1 = JanusGraphFactory.open("/Downloads/janusgraph-cassandra.properties")
var mgmt = gr1.openManagement()
var reg_id_prop = mgmt.makePropertyKey("register_id").dataType(classOf[String]).make()
mgmt.buildIndex("byRegId", classOf[Vertex]).addKey(reg_id_prop).buildCompositeIndex()
mgmt.commit()
请参阅 JanusGraph 中的 indexing 文档。
关于使用复合索引检查顶点是否存在的第二个问题,您需要使用终止步骤来完成遍历。例如,在 Java 中,这将 return 一个布尔值:
g.V().has('name', 'property_value').hasNext()
参考 iterating the traversal 来自 JanusGraph 的文档。
阅读 gremlin-scala README,它似乎有一些终端步骤选项,您可以使用 head
、headOption
、toList
或 toSet
.
g.V().has('name', 'property_value').headOption
您还应该查看 gremlin-scala-examples and the gremlin-scala traversal specification。
我正在使用 Spark 从数据流中创建 JanusGraph,但在索引和创建属性时遇到问题。我想通过名为 "register_id" 的顶点 属性 创建索引。我不确定我的做法是否正确。
所以,这是我的代码:
var gr1 = JanusGraphFactory.open("/Downloads/janusgraph-cassandra.properties")
gr1.close()
// This is done to clear the graph made in every run.
JanusGraphFactory.drop(gr1)
gr1 = JanusGraphFactory.open("/Downloads/janusgraph-cassandra.properties")
var reg_id_prop = gr1.makePropertyKey("register_id").dataType(classOf[String]).make()
var mgmt = gr1.openManagement()
gr1.tx().rollback()
mgmt.buildIndex("byRegId", classOf[Vertex]).addKey(reg_id_prop).buildCompositeIndex()
当我 运行 以上时,我收到一条错误消息:
"Vertex with id 5164 was removed".
此外,我如何检查我是否在 Scala 中有具有特定 属性 的顶点。我知道在 gremlin 中,g.V().has('name', 'property_value')
有效,但无法弄清楚如何在 Scala 中执行此操作。我试过 Gremlin-Scala 但似乎找不到它。
任何帮助将不胜感激。
您应该使用 mgmt
对象来构建架构,而不是 graph
对象。您还需要确保 mgmt.commit()
架构更新。
gr1 = JanusGraphFactory.open("/Downloads/janusgraph-cassandra.properties")
var mgmt = gr1.openManagement()
var reg_id_prop = mgmt.makePropertyKey("register_id").dataType(classOf[String]).make()
mgmt.buildIndex("byRegId", classOf[Vertex]).addKey(reg_id_prop).buildCompositeIndex()
mgmt.commit()
请参阅 JanusGraph 中的 indexing 文档。
关于使用复合索引检查顶点是否存在的第二个问题,您需要使用终止步骤来完成遍历。例如,在 Java 中,这将 return 一个布尔值:
g.V().has('name', 'property_value').hasNext()
参考 iterating the traversal 来自 JanusGraph 的文档。
阅读 gremlin-scala README,它似乎有一些终端步骤选项,您可以使用 head
、headOption
、toList
或 toSet
.
g.V().has('name', 'property_value').headOption
您还应该查看 gremlin-scala-examples and the gremlin-scala traversal specification。