2

AnormCypher 文档提供了一个如何使用 Stream API 检索数据的示例:http: //anormcypher.org/

“访问返回查询结果的第一种方法是使用 Stream API。

当您在任何 Cypher 语句上调用 apply() 时,您将收到一个 CypherRow 实例的惰性流,其中每一行都可以视为一个字典:

 // Create Cypher query
 val allCountries = Cypher("start n=node(*) where n.type = 'Country' return n.code as code, n.name as name")

 // Transform the resulting Stream[CypherRow] to a List[(String,String)]
 val countries = allCountries.apply().map(row => 
   row[String]("code") -> row[String]("name")
 ).toList

我正在尝试使用相同的方法通过以下 Cypher 查询获取路径:

MATCH p = (n {id: 'n5'})-[*]-(m) RETURN p;

然而,在运行此代码时:

Cypher("MATCH p = (n {id: 'n5'})-[*]-(m) RETURN p;")().map {row  =>
  println(row[Option[org.anormcypher.NeoRelationship]]("p"))
}

我得到异常(见下文)。CypherRow在这种情况下如何获取路径信息?

 Exception in thread "main" java.lang.RuntimeException: TypeDoesNotMatch(Unexpected type while building a relationship)
    at org.anormcypher.MayErr$$anonfun$get$1.apply(Utils.scala:21)
    at org.anormcypher.MayErr$$anonfun$get$1.apply(Utils.scala:21)
    at scala.util.Either.fold(Either.scala:97)
    at org.anormcypher.MayErr.get(Utils.scala:21)
    at org.anormcypher.CypherRow$class.apply(AnormCypher.scala:303)
    at org.anormcypher.CypherResultRow.apply(AnormCypher.scala:309)
    at bigdata.test.n4j.Simple$$anonfun$main$1.apply(Simple.scala:31)
    at bigdata.test.n4j.Simple$$anonfun$main$1.apply(Simple.scala:29)
    at scala.collection.immutable.Stream.map(Stream.scala:376)
    at bigdata.test.n4j.Simple$.main(Simple.scala:29)
4

1 回答 1

1

Cypher 中的路径在 2.0 中已更改,因此您不能直接轻松地使用它们,因为它们不是集合。AnormCypher 中可能应该有某种新的 Path 类型,但现在您可以将路径与relationships()or一起使用nodes()

例如,您可以这样做来提取关系:

Cypher("MATCH p = (n {id: 'n5'})-[*]-(m) RETURN relationships(p);")().map {row  =>
  println(row[Seq[NeoRelationship]]("relationships(p)"))
}
于 2014-05-20T13:57:24.910 回答