java - Neo4j : Retrieving All Nodes and Relationship connected to a Node in Neo4j Rest OR through Cypher

标签 java neo4j

我想检索所有节点和连接到节点的关系。

我尝试通过两种方式做到这一点:

第 1 通过 Neo4j REST API我试过了

URI traverserUri = new URI( startNode.toString() + "/traverse/node" );
WebResource resource = Client.create()
        .resource( traverserUri );
String jsonTraverserPayload = t.toJson();
ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
        .type( MediaType.APPLICATION_JSON )
        .entity( jsonTraverserPayload )
        .post( ClientResponse.class );

System.out.println( String.format(
        "POST [%s] to [%s], status code [%d], returned data: "
                + System.getProperty( "line.separator" ) + "%s",
        jsonTraverserPayload, traverserUri, response.getStatus(),
        response.getEntity( String.class ) ) );
response.close();

并获得以下响应:

[ {
  "outgoing_relationships" : "http://localhost:7474/db/data/node/82/relationships/out",
  "data" : {
    "band" : "The Clash",
    "name" : "Joe Strummer"
  },
  "traverse" : "http://localhost:7474/db/data/node/82/traverse/{returnType}",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/82/relationships/all/{-list|&|types}",
  "property" : "http://localhost:7474/db/data/node/82/properties/{key}",
  "all_relationships" : "http://localhost:7474/db/data/node/82/relationships/all",
  "self" : "http://localhost:7474/db/data/node/82",
  "properties" : "http://localhost:7474/db/data/node/82/properties",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/82/relationships/out/{-list|&|types}",
  "incoming_relationships" : "http://localhost:7474/db/data/node/82/relationships/in",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/82/relationships/in/{-list|&|types}",
  "create_relationship" : "http://localhost:7474/db/data/node/82/relationships"
}, {
  "outgoing_relationships" : "http://localhost:7474/db/data/node/83/relationships/out",
  "data" : {
  }]

但问题是,如果我想再次查看此节点的关系,我将不得不点击链接 "http://localhost:7474/db/data/node/82/relationships/all"

我们不能在不再次点击链接的情况下直接显示节点及其关系而不是链接到关系的数据吗????

第二 我试图做的事情是从密码查询中得到这个:

START a=node(3)
MATCH (a)-[:KNOWS]->(b)-[:KNOWS]->(c)-[:KNOWS]->(d)
RETURN a,b,c,d

但是这也没有用,因为在 (b)(c) 会有多个值,因此我将不得不迭代和编写另一个查询

我们不能在单个查询中完成这项工作,因为我有太多的关联关系以至于很难一次又一次地迭代。任何帮助将不胜感激。

最佳答案

使用 Cypher 很容易让所有节点连接到给定节点

START a=node(3)
MATCH (a)-[:KNOWS*]->(d)
RETURN distinct d

但是如果你有大量的连接节点和深度连接,你可能无法获得良好的性能。

如果您知道连接的边界,在查询中明确指定它会有助于提高性能,

START a=node(3)
MATCH (a)-[:KNOWS*1..3]->(d)
RETURN Distinct d

关于java - Neo4j : Retrieving All Nodes and Relationship connected to a Node in Neo4j Rest OR through Cypher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18741918/

相关文章:

java - 在初始化 SpringBootApplication 的 ApplicationContextInitializer bean 之前启动一个 @WebServlet

java - Neo4j获取TransactionEventHandler的信息

Neo4j - 关系方向会影响密码性能吗?

java - 如何将 gif 图像转换为 jpg?

java - 为要插入数据库的数据实现队列

java - 无法在 javafx 中将数组列表作为参数从一个窗口传递到另一个窗口

java - 在未加权图中找到从源到目的地的所有最短路径

neo4j - 如何使用 Cypher 在 Neo4j 中建立关系 "replace"?

java - Cypher 查询 friend 的 friend ,过滤掉那些已经是 friend 的人

python - 将 neo4j 查询结果加载到 python 的 `igraph` 图中