Here's my graph
I'm trying to get the details of the nodes and its relationship in java code and below is my code. I'm able to get the details of the nodes but not the relationships. And the java docs has no information and methods that allow exposing of the relationships.
My Code
import java.util.List;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Record;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.neo4j.driver.Value;
import org.neo4j.driver.internal.InternalResult;
public class Neo4jTest {
private Driver driver;
public Neo4jTest() {
loadDriver();
}
private void loadDriver() {
driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "Test"));
}
public Result createNodes(String query) {
try (Session session = driver.session())
{
InternalResult result = (InternalResult) session.writeTransaction(tx -> tx.run(query));
return result;
}
}
public List<Record> execute(String query) {
try (Session session = driver.session())
{
Result result = session.run(query);
return result.list();
}
}
public void cleanUp(String object) {
String cleanUpQuery = "match(n:" + object +") detach delete n";
execute(cleanUpQuery);
}
public void printResults(List<Record> results) {
for (Record record : results) {
Value rec = record.get("n");
System.out.print("Record --> [");
for (String key : rec.keys()) {
System.out.print(key + " : " + rec.get(key) + ", ");
}
System.out.println("]");
//TODO: Need to get all Relationship of this particular node
}
}
public static void main(String[] args) {
String createQuery = "create \n" +
"(a:People {name : \"Vivek\", age : 20}), \n" +
"(b:People {name : \"Abdul\", age : 25}), \n" +
"(c:People {name : \"John\", age : 22}),\n" +
"" +
"(a)-[:KNOWS]->(b),\n" +
"(b)-[:FRIEND_OF]->(c),\n" +
"(c)-[:KNOWS]->(a)";
String fetchQuery = "match(n:People) return n";
Neo4jTest obj = new Neo4jTest();
obj.cleanUp("People");
// Create Nodes
obj.createNodes(createQuery);
// Fetch Nodes
List<Record> results = obj.execute(fetchQuery);
// Process Results
obj.printResults(results);
}
}
Output
Record --> [name : "Vivek", age : 20, ]
Record --> [name : "Abdul", age : 25, ]
Record --> [name : "John", age : 22, ]
JavaDoc Link -> https://neo4j.com/docs/api/java-driver/current/
Relevant Result Objects
org.neo4j.driver.Record;
org.neo4j.driver.Value;
Other Details
Neo4j Community Version - 3.5.14
Neo4j Java Driver Version - 4.0.0
Update
(for cybersam answer)
The first query doesn't return the relationship as part of the r variable.
[Update] And the second query also doesn't return relationship
Results in the neo4j browser and code are the same.
Output for query
MATCH (n:People) WITH COLLECT(n) AS nodes MATCH (:People)-[r]->(:People) WITH nodes, COLLECT(TYPE(r)) AS rels RETURN nodes, rels
Thanks for any help!