Cypher:使用Scala AnormCypher查找路径?

时间:2014-05-20 12:10:50

标签: scala cypher anormcypher

AnormCypher doc提供了如何使用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

我正在尝试使用相同的aproach来获取以下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)

1 个答案:

答案 0 :(得分:1)

Cypher的路径从2.0开始被更改,因此您无法直接使用它们,因为它们不是收藏品。在AnormCypher中可能应该有某种新的Path类型,但是现在您可以使用路径以及relationships()nodes()

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

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