在 TinkerPop 中顶点属性可以包含复杂的对象吗?

In TinkerPop can vertex properties contain complex objects?

我正在玩 TinkerGraphgremlin-scala,我发现它能够持久化复杂对象:

case class InnerObj(a: Int, b: String)
case class ComplexObj(a: Int, b: InnerObj)
case class SuperComplexObj(a : String, b: ComplexObj)

class GremlinQueriesSpec extends FlatSpec
  with ScalaFutures with MustMatchers {

  behavior of "Gremlin queries"

  it must "be able to persist complex objects containing collections" taggedAs Integration in {

    val g = TinkerGraph.open()
    implicit val graph = g.asScala

    val user = RandomData.randomUserDataAggregate

    graph + user

    graph.V().toCC[UserDataAggregate].toList() must be eq List(user)
  }
}

然而,docs are not completely clear to me. On еру one hand there's not much structure available for property values besides lists, sets, and metaproperties. On the other hand docs say:

A Property denotes a key/value pair associated with an Edge. A property is much like a Java8 Optional in that a property can be not present (i.e. empty). The key of a property is always a String and the value of a property is an arbitrary Java object. Each underlying graph engine will typically have constraints on what Java objects are allowed to be used as values.

好的,看来要看实现了。但是可以在 Gremlin 查询中使用嵌套对象吗?

确实要看实现。您正在使用 TinkerGraph,它可以反过来存储任何 Java 对象,因此您可以自由地将任何您喜欢的东西放在那里:

gremlin> g.addV().property('function',{it.length()})
==>v[2]
gremlin> g.V().sideEffect{println(it.get().value('function')("four"))}
4
==>v[2]

疯了吧?当然,如果您开始在其中随机 odds/ends 并需要持久化这些对象或将它们推送到网络(例如通过 Gremlin 服务器),您将需要考虑序列化等问题。

嵌套对象作为 TinkerGraph 的值没有问题 属性。只是要小心。在深入研究将复杂 Java 对象存储为属性的路径之前,真正停止考虑您的模式。也许最好将这些对象建模为图形的元素,作为第一个 class 公民,以使 Gremlin 遍历直接处理它们。