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!
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);
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.