AnormCypher:如何获得给定节点的完整路径?

时间:2014-05-21 09:54:34

标签: scala neo4j cypher anormcypher

对于由两组节点组成的图表:

n1 - > n2 - > n3 - > N4

n5 - > n6 - > N7

使用命令创建:

创建(n1 {id:'n1'}) - [:rel] - >(n2 {id:'n2'}) - [:rel] - >(n3 {id:'n3'}) - [:rel] - >(n4 {id:'n4'})

创建(n5 {id:'n5'}) - [:rel] - >(n6 {id:'n6'}) - [:rel] - >(n7 {id:'n7'})

对于这两个请求:

MATCH p =(n {id:'n1'}) - [*] - (m)将节点(p)作为节点返回;

MATCH p =(n {id:'n1'}) - [*] - (m)RETURN关系(p)为rels;

AnormCypher(http://anormcypher.org/)返回仅与节点n1和n2相关的信息,而Neo4J Web控制台返回完整路径。

如何在AnormCypher中获取所有节点和关系的完整路径?

演示此功能的程序(在此消息的末尾)输出:

ListBuffer(NeoNode(32,Map(id -> n1)), NeoNode(33,Map(id -> n2)))
Node: id=32 props=Map(id -> n1)
--Props keys:
----key: id val: n1
Node: id=33 props=Map(id -> n2)
--Props keys:
----key: id val: n2
ListBuffer(NeoRelationship(27,Map(),32,33))
Rel: id=27 start=32 end=33 props=Map()

代码:

object Simple {
   def main(args: Array[String]): Unit = {

Cypher("MATCH p = (n {id: 'n1'})-[*]-(m) RETURN nodes(p) as nodes;")().map { row =>

  println(row[Seq[org.anormcypher.NeoNode]]("nodes"))
  val nodes = row[Seq[org.anormcypher.NeoNode]]("nodes")

  nodes.map(n => {
    val props = n.props
    println("Node: id="+n.id+" props="+props)
    println("--Props keys: ")
    val x = props.keys
    props.keys.map( k=> println("----key: "+k+" val: "+props(k)))
    })
}

Cypher("MATCH p = (n {id: 'n1'})-[*]-(m) RETURN relationships(p) as rels ;")().map { row =>

  println(row[Seq[NeoRelationship]]("rels"))
  val rels = row[Seq[NeoRelationship]]("rels")
  rels.map(r => {
    val x = r.props
    println("Rel: id="+r.id+" start="+r.start+" end="+r.end+" props="+r.props)
  })
}

 }
}

1 个答案:

答案 0 :(得分:0)

问题是你的map函数正在创建一个惰性流,而你并没有迭代流的其余部分。如果您将.toList.last添加到.map的末尾,强制迭代整个流,则应获得更长的路径结果。

例如:

Cypher("MATCH p = (n {id: 'n1'})-[*]-(m) RETURN nodes(p) as nodes;")().map { row =>
  println(row[Seq[org.anormcypher.NeoNode]]("nodes"))
  val nodes = row[Seq[org.anormcypher.NeoNode]]("nodes")

  nodes.map(n => {
    val props = n.props
    println("Node: id="+n.id+" props="+props)
    println("--Props keys: ")
    val x = props.keys
    props.keys.map( k=> println("----key: "+k+" val: "+props(k)))
    })
}.toList

或者,您可以使用.foreach代替.map,而Cypher("MATCH p = (n {id: 'n1'})-[*]-(m) RETURN nodes(p) as nodes;")().foreach { row => println(row[Seq[org.anormcypher.NeoNode]]("nodes")) val nodes = row[Seq[org.anormcypher.NeoNode]]("nodes") nodes.map(n => { val props = n.props println("Node: id="+n.id+" props="+props) println("--Props keys: ") val x = props.keys props.keys.map( k=> println("----key: "+k+" val: "+props(k))) }) } 会为您执行此操作。

更新:这是一个没有返回类型的示例:

{{1}}