简体   繁体   English

使用Java从neo4j数据库查询两个节点

[英]Query two nodes from neo4j database using java

I am new to Neo4j and I would like to query a Neo4j database containing only nodes in order to create links between them according to 2 lists I already have. 我是Neo4j的新手,我想查询仅包含节点的Neo4j数据库,以便根据已有的2个列表在它们之间创建链接。

For example, I want to connect nodes with names in a List A with nodes with names from List B . 例如,我想将名称为List A的节点与名称为List B节点连接起来。

This is the code I wrote : 这是我写的代码:

public class Main {

    public static void main(String[] args) {

        GraphDatabaseFactory graphDbFactory = new GraphDatabaseFactory();
        GraphDatabaseService graphDb = graphDbFactory.newEmbeddedDatabase("C:\\Zakaria\\NeoTests\\Tetralecture");

        ExecutionEngine execEngine = new ExecutionEngine(graphDb);

/* Here is a loop to read from listA and listB so no need to worry about them */

        try (Transaction ignored = graphDb.beginTx()) {
            String query = "MATCH (auteur1:AUTEUR{Name:'" + listA.get(i) + "'}), (auteur2:AUTEUR{Name:'" + listB.get(j) + "'}) return auteur1, auteur2";
            ExecutionResult execResult = execEngine.execute(query);
            Iterator<Node> aut_column = execResult.columnAs("auteur1");
            for(Node node : IteratorUtil.asIterable(aut_column)) {
                String nodeResult = node + " : " + node.getProperty("Name");
                System.out.println(nodeResult);
            }
        }

    }

}

This example only displays one list of authors from one colunm auteur1 , I would like to be able to display both of them. 本示例仅显示来自一个auteur1一个作者列表,我希望能够同时显示两个作者。

If I can do that, I think maniputating the nodes from both lists and creating links between them would be easier. 如果可以,我认为从两个列表中删除节点并在它们之间创建链接会更容易。

Thanks for your help! 谢谢你的帮助!

Does this work for you? 这对您有用吗?

public class Main {

    public static void main(String[] args) {

        GraphDatabaseFactory graphDbFactory = new GraphDatabaseFactory();
        GraphDatabaseService graphDb = graphDbFactory.newEmbeddedDatabase("C:\\Zakaria\\NeoTests\\Tetralecture");

        ExecutionEngine execEngine = new ExecutionEngine(graphDb);

/* Here is a loop to read from listA and listB so no need to worry about them */

        try (Transaction ignored = graphDb.beginTx()) {
            String query = "MATCH (auteur1:AUTEUR{Name:'" + listA.get(i) + "'}), (auteur2:AUTEUR{Name:'" + listB.get(j) + "'}) return auteur1, auteur2";
            ExecutionResult execResult = execEngine.execute(query);
            for(Map<String, Object> row : execResult) {
                final Node node1 = (Node)row.get("auteur1");
                final Node node2 = (Node)row.get("auteur2");
                String nodeResult = node1 + " : " + node1.getProperty("Name") + "; " + node2 + " : " + node2.getProperty("Name");
                System.out.println(nodeResult);
            }
        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM