1

在这种情况下,首先,我打开了一个 Janusgraph,创建了一些顶点和边并关闭了它;后来,我打开了同一个 Janusgraph,创建了一些顶点和边并关闭了它。

毕竟,我想找到从一个顶点到另一个顶点的路径,我发现这两个顶点不在一个路径中,因为它们创建的时间不同;但是,如果我将它们一起创建,则路径是正确的。

以下是一些用于创建的代码:

val grap = JanusGraphFactory.open("d:\\janusgraph\\janusgraph-hbase.properties")
     val mgmt = grap.openManagement()

     mgmt.makePropertyKey("value").dataType(classOf[String]).make()

     mgmt.makeEdgeLabel("deviceid").make()

     mgmt.makeVertexLabel("phone").make()


    val tx = grap.newTransaction()

    val phone1 = tx.addVertex(T.label, "phone", "value", "13700000001")
    val phone2 = tx.addVertex(T.label, "phone", "value", "13700000002")
    val phone3 = tx.addVertex(T.label, "phone", "value", "13700000003")
    val phone4 = tx.addVertex(T.label, "phone", "value", "13700000004")
    val phone5 = tx.addVertex(T.label, "phone", "value", "13700000005")
    val dev1 = tx.addVertex(T.label, "dev", "value", "dev1")
    val dev2 = tx.addVertex(T.label, "dev", "value", "dev2")
    val dev3 = tx.addVertex(T.label, "dev", "value", "dev3")*/


    phone1.addEdge("phone-dev", dev1, "value", "13700000001_dev1")
    phone2.addEdge("phone-dev", dev1, "value", "13700000002_dev1")
    phone2.addEdge("phone-dev", dev2, "value", "13700000002_dev2")

    phone3.addEdge("phone-dev", dev2, "value", "13700000003_dev2")

    phone4.addEdge("phone-dev", dev2, "value", "13700000004_dev2")
    phone4.addEdge("phone-dev", dev3, "value", "13700000004_dev3")


    tx.commit()
    tx.close()

以下是查找代码:

  val graph = JanusGraphFactory.open("d:\\janusgraph\\janusgraph-hbase.properties")
    //val graph = JanusGraphFactory.open(configuration)
    val g = graph.traversal()
    val result2 = g.V().hasLabel("phone").repeat(both().simplePath()).until(bothE().count().is(1)).path().by("value").toSet
    //val result = g.V().hasLabel("phone").repeat(both().simplePath()).until(bothE().count().is(1)).path().by

    val it = result2.iterator()
    while (it.hasNext) {
        println("path=>" + it.next())
    }
    System.exit(1)
4

1 回答 1

0

JanusGraph 有一个叫做Transactional Scope的东西。按照这个概念,

所有图形元素(顶点、边和类型)都与检索或创建它们的事务范围相关联。在 TinkerPop 的默认事务语义下,事务是使用图上的第一个操作自动创建的,并使用 commit() 或 rollback() 显式关闭。一旦事务关闭,与该事务关联的所有图形元素都会变得陈旧且不可用。但是,JanusGraph 会自动将顶点和类型转换到新的事务范围中,如下例所示:

    graph = JanusGraphFactory.open("berkeleyje:/tmp/janusgraph")
    juno = graph.addVertex() //Automatically opens a new transaction
    graph.tx().commit() //Ends transaction
    juno.property("name", "juno") //Vertex is automatically transitioned

另一方面,边缘不会自动转换,并且不能在其原始事务之外访问。它们必须显式转换:

    e = juno.addEdge("knows", graph.addVertex())
    graph.tx().commit() //Ends transaction
    e = g.E(e).next() //Need to refresh edge
    e.property("time", 99)
于 2022-02-14T18:16:14.067 回答