0

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

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 Relations(p) as 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)
  })
}

 }
}
4

1 回答 1

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)))
    })
}
于 2014-05-21T21:10:27.047 回答