对于由两组节点组成的图:
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)
})
}
}
}