0

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.

For example, I want to connect nodes with names in a List A with nodes with names from 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.

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!

1 Answer 1

1

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);
            }
        }

    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.