Similarly, we can write client applications using Java to interact and work with Neo4j. One way to do this is with the official Neo4j Java driver, via Apache Maven. To accomplish this, we will create a new Maven project with the following dependency in pom.xml:
<dependencies>
<dependency>
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
Once that is done, we will write our Java code (similar to the first Python example) to create a new language with an edge to the Welcome message node and print out the results to the console. We will start by writing a Java class to handle all of our interactions with Neo4j and name it Neo4jConnection. To start this class, we will give it four Neo4j-specific imports and then create two constructors:
import org.neo4j.driver.v1.AuthTokens;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.GraphDatabase;
import org.neo4j.driver.v1.Session;
public class Neo4jConnection {
private Driver driver;
private Session session;
public Neo4jConnection() {
}
public Neo4jConnection(String node, String user, String pwd) {
connect(node,user,pwd);
}
Next, we will create a public method to connect to our Neo4j instance:
public void connect(String node, String user, String pwd) {
driver = GraphDatabase.driver( "bolt://"
+ node + ":7687", AuthTokens.basic( user, pwd ) );
session = driver.session();
}
Additionally, we will expose a public getter for the Session object:
public Session getSession() { return session; }
Finally, we will add a public method to close the connection to Neo4j by invoking the close methods on the driver and session objects:
public void close() {
session.close();
driver.close();
}
}
Lastly, we will create a short main class Neo4jHelloWorld to connect to our instance. This class will connect to Neo4j using our Neo4jConnection class. It will then query the message node and its edge to the Java language node and print the results to the console. First, we will define our imports and establish our connection to Neo4j:
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.StatementResult;
import org.neo4j.driver.v1.Record;
import static org.neo4j.driver.v1.Values.parameters;
public class Neo4jHelloWorld {
public static void main(String[] args) {
Neo4jConnection conn = new Neo4jConnection(
"192.168.0.100","neodba","flynnLives");
Session session = conn.getSession();
To start out, we will create the Java language node:
session.run( "CREATE (:Language {name:{name},version:{ver}})",
parameters("name", "Java", "ver", "1.8.0_74"));
Next, we will create the ACCESSED_FROM relationship edge from the Java language node to the Welcome message node:
String createRelationship = "MATCH (m:Message),(l:Language) "
+ "WHERE m.title = {title} AND l.name={language} "
+ "CREATE (m)-[:ACCESSED_FROM]->(l);";
session.run(createRelationship,
parameters("title", "Welcome", "language", "Java"));
Here, we will query the message node(s) accessed from Java and build a result set:
String queryRelationship = "MATCH (m:Message)-[:ACCESSED_FROM]->"
+ "(l:Language {name:{language}}) "
+ "RETURN m.title,l.name;";
StatementResultresultSet = session.run(queryRelationship,
parameters("language", "Java"));
Finally, we will process the result set, print the output to the console, and close our connection to Neo4j:
while (resultSet.hasNext()) {
Record result = resultSet.next();
System.out.println( result.get("m.title")
+ " from " + result.get("l.name"));
}
session.close();
}
}
Running this code yields the following output:
"Welcome" from "Java"
Similar to our second Python script, we will now write a Java class to account for the proposed ISS Expedition 52/53 mission. We'll use the same Neo4jConnection class to handle the connection to our Neo4j instance. Our new main class, Neo4jISS53, will use the same imports that our last Java main class did. It will start similarly, where we connect it to our instance:
public class Neo4jISS53 {
public static void main(String[] args) {
Neo4jConnection conn = new
Neo4jConnection("192.168.0.100","neodba","flynnLives");
Session session = conn.getSession();
Next, we'll start by creating a node for the ISS-52/53 (Soyuz) mission:
String createMission = "CREATE (:Mission {name:{name}})";
session.run(createMission,parameters("name","ISS-52/53 (Soyuz)"));
With the Mission node in place, we will create edges to the three NASA astronauts who are scheduled to serve on that mission:
String createRelationship = "MATCH (m:Mission),(a:Astronaut) "
+ "WHERE m.name={mname} AND a.name={aname} "
+ "CREATE (a)-[:FLEW_ON]->(m)";
session.run(createRelationship, parameters("mname","ISS-52/53 (Soyuz)",
"aname","Joseph M. Acaba"));
session.run(createRelationship, parameters("mname","ISS-52/53 (Soyuz)",
"aname","Mark T. VandeHei"));
session.run(createRelationship, parameters("mname","ISS-52/53 (Soyuz)",
"aname","Randolph J. Bresnik"));
With the node and edges built, we can then query the mission for astronauts who flew on it and process the result set:
String queryRelationship = "MATCH (m:Mission {name:{name}})"
+ "<-[:FLEW_ON]-"
+ "(a:Astronaut) RETURN m.name,a.name;";
StatementResult resultSet = session.run(queryRelationship,
parameters("name", "ISS-52/53 (Soyuz)"));
while (resultSet.hasNext()) {
Record result = resultSet.next();
System.out.println( result.get("a.name")
+ " flew on " + result.get("m.name"));
}
Finally, we can close the connection to our Neo4j instance:
session.close(); } }
Running this code yields the following output:
"Randolph J. Bresnik" flew on "ISS-52/53 (Soyuz)" "Mark T. VandeHei" flew on "ISS-52/53 (Soyuz)" "Joseph M. Acaba" flew on "ISS-52/53 (Soyuz)"