Neo4j遍历子图

时间:2014-05-23 05:37:17

标签: java neo4j traversal subgraph

我想从Neo4j图数据库中提取子图。输入将是两个节点。我们需要从两个节点中找到所有一跳邻居并形成节点的并集(让它称之为闭包)。现在我们必须获得此闭包中的所有关系(包括输入节点的节点的并集)。如何使用java"有效地解决neo4j中的这个问题"。请建议。

2 个答案:

答案 0 :(得分:2)

这样的东西?

// find closure
Set<Node> nodes = new HashSet<>();
for (Node n : inputs) {
  for (Relationship rel : n.getRelationships()) {
      nodes.add(r.getOtherNode(n));
  }
}
nodes.addAll(inputs);

// find rels withing closure
Set<Relationship> rels = new HashSet<>();
for (Node n : nodes) {
  for (Relationship rel : n.getRelationships()) {
      if (nodes.contains(r.getOtherNode(n))) rels.add(rel);
  }
}

return rels;

答案 1 :(得分:0)

在关闭内部时,需要花费大量时间。我提供了2个节点作为输入,其中大小为2300.对于此输入,大约需要2分钟来获取数据。但我们需要在几秒钟内获得数据 Neo4j中的总节点数:20000个节点。 在Neo4j中的总重量:360000边缘 以下是用于获取数据的代码。

public void getSemanticContext()     {

    List<Node> inputs = new ArrayList<>();
    List<Integer> qterms1 = new ArrayList<>();
    Set<Node> nodes = new HashSet<>();
    Set<Integer> closure = new HashSet<>();
    rdbms.createConnection();

    RandomWalk_RDBMS rwalk = new RandomWalk_RDBMS(rdbms, operation);
    Map<Integer, Double> sortedTerms=null;


    Transaction tx = graphDb.beginTx();

     try
     {
        ResourceIterator<Node> id1 =  graphDb.findNodesByLabelAndProperty( DynamicLabel.label("concept"), "name", "copy" ).iterator();
        ResourceIterator<Node> id2 =  graphDb.findNodesByLabelAndProperty( DynamicLabel.label( "concept"), "name", "main" ).iterator();

        if ( id1.hasNext() )
        {
            firstNode = id1.next();
        }
        if ( id2.hasNext() )
        {
            secondNode = id2.next();
        }
        System.out.println(firstNode.getProperty("id"));
        System.out.println(firstNode.getProperty("name"));

        // find closure

        qterms1.add(Integer.parseInt(firstNode.getProperty("id").toString()));
        qterms1.add(Integer.parseInt(secondNode.getProperty("id").toString()));
        inputs.add(firstNode);
        inputs.add(secondNode);

        closure.add(Integer.parseInt(firstNode.getProperty("id").toString()));
        closure.add(Integer.parseInt(secondNode.getProperty("id").toString()));
        for (Node n : inputs) 
        {
          for (Relationship rel : n.getRelationships()) {
              nodes.add(rel.getOtherNode(n));
              closure.add(Integer.parseInt(rel.getOtherNode(n).getProperty("id").toString()));
          }
        }
        nodes.addAll(inputs);
        System.out.println("closure generated");
        System.out.println(nodes);
        genScoreMap= new HashMap<>();
        // find rels withing closure
        Set<Relationship> rels = new HashSet<>();
        for (Node n : nodes) {
            HashMap<Integer, Double> innerMap = new HashMap<Integer, Double>();
          for (Relationship rel : n.getRelationships()) {
              if (nodes.contains(rel.getOtherNode(n))) 
                  {
                    rels.add(rel);
                    innerMap.put(Integer.parseInt(rel.getOtherNode(n).getProperty("id").toString()), Double.parseDouble(rel.getProperty("generatability_score").toString()));
                  }
              genScoreMap.put(Integer.parseInt(n.getProperty("id").toString()), innerMap);
          }
        }

     }
     catch(Exception e)
     {
         e.printStackTrace();
     }
     tx.close();
    // System.out.println(genScoreMap);
     System.out.println("semantic context generated");
     sortedTerms=rwalk.randomWalk(qterms1,genScoreMap,closure);
    printResult(sortedTerms,rwalk);
}