将密码查询的结果收集到哈希映射java中? [英] Collecting the result of cypher query into a hash map java?
问题描述
这是查找连接节点的后续问题。
代码是
firstNode = graphDb.createNode(); //创建节点
firstNode.setProperty(person,Andy);
Label myLabel = DynamicLabel.label(person);
firstNode.addLabel(myLabel); ...
relationship = firstNode.createRelationshipTo(secondNode,RelTypes.emails); //创建关系
relationship.setProperty(relationship,email); ....
ExecutionEngine engine = new ExecutionEngine(graphDb);
ExecutionResult result = engine.execute(MATCH(sender:person) - [:emails] - (receiver)RETURN sender,count(receiver)as count,collect(receiver)as ORDER BY count DESC);
System.out.println(result.dumpToString());
我得到的结果是:
sender | count |接收者
节点[2] {person:Chris} | 3 | [节点[4] {person:Elsa},Node [0] {person:Andy},Node [1] {person:Bobby}]
Node [4] {person:Elsa } | 3 | [节点[5] {person:Frank},Node [2] {person:Chris},Node [3] {person:David}]
Node [1] {person:Bobby } | 3 | [节点[2] {person:Chris},Node [3] {person:David},Node [0] {person:Andy}]
Node [5] {person:Frank } | 2 | [节点[3] {person:David},Node [4] {person:Elsa}
如何收集发件人作为关键字和接收者作为值?
例如: {Frank = [David,Elsa],Chris = [Elsa,Andy,Nobby] ..
有什么想法?
最初我尝试迭代类似的东西(Map< / p>
String,Object> row:result){
Node x =(Node)row.get(receivers);
System.out.println(x); (String prop:x.getPropertyKeys()){
System.out.println(prop +:+ x.getProperty(prop));
for
}
这引发了classcast异常。它适用于列发件人而不是接收者。
我对cypher很新颖。我不知道如何将结果转换为哈希映射。这可能是什么?
您可以重写密码以返回地图,而不是...(为了便于阅读, (发件人:人) - [:电子邮件] - >(收件人)
与发件人,收件人(收件人)。人)作为接收者
RETURN {发送者:发送者,接收者:接收者)
ORDER BY大小(接收者)DESC
This is a followup of Finding connected nodes question.
The code is
firstNode = graphDb.createNode();//creating nodes
firstNode.setProperty( "person", "Andy " );
Label myLabel = DynamicLabel.label("person");
firstNode.addLabel(myLabel); ...
relationship = firstNode.createRelationshipTo( secondNode, RelTypes.emails );// creating relationships
relationship.setProperty( "relationship", "email " );....
ExecutionEngine engine = new ExecutionEngine(graphDb);
ExecutionResult result = engine.execute("MATCH (sender:person)-[:emails]- (receiver)RETURN sender, count(receiver)as count, collect(receiver) as receivers ORDER BY count DESC ");
System.out.println(result.dumpToString());
The result I got was:
sender | count | receivers
Node[2]{person:"Chris"} | 3 | [Node[4]{person:"Elsa "},Node[0]{person:"Andy "},Node[1]{person:"Bobby"}]
Node[4]{person:"Elsa "} | 3 | [Node[5]{person:"Frank"},Node[2]{person:"Chris"},Node[3]{person:"David"}]
Node[1]{person:"Bobby"} | 3 | [Node[2]{person:"Chris"},Node[3]{person:"David"},Node[0]{person:"Andy "}]
Node[5]{person:"Frank"} | 2 | [Node[3]{person:"David"},Node[4]{person:"Elsa "}
How to collect the sender as key and receivers as values?
For ex : {Frank =[David, Elsa], Chris =[Elsa, Andy, Nobby]..
Any idea?
Initially I tried iterating something like this
for (Map<String,Object> row : result) {
Node x = (Node)row.get("receivers");
System.out.println(x);
for (String prop : x.getPropertyKeys()) {
System.out.println(prop +": "+x.getProperty(prop));
}
This throws classcast exception. It works for column "sender" and not for "receivers".
I am very new to cypher. I don't know how to transform the result into a hash map. How is this possible ?
You can rewrite the cypher to return a map instead... (split for readability)
MATCH (sender:person)-[:emails]->(receiver)
WITH sender, collect(receiver.person) as receivers
RETURN {sender: sender.person, receivers: receivers)
ORDER BY size(receivers) DESC
这篇关于将密码查询的结果收集到哈希映射java中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!