Introduction to Neo4j (#1098)
* Nashorn * Nashorn x2 * Nashorn added trailing newlines * Cleanup * Formatted script lines * Change system outs to asserts * Change Nashorn to be Junit tests * Remove empty test * Added Neo4j section * Revert NashornTest
This commit is contained in:
parent
92ad8123f1
commit
a9d8c5f3a3
|
@ -10,6 +10,44 @@
|
|||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.neo4j</groupId>
|
||||
<artifactId>neo4j</artifactId>
|
||||
<version>3.1.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.neo4j.driver</groupId>
|
||||
<artifactId>neo4j-java-driver</artifactId>
|
||||
<version>1.1.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.neo4j</groupId>
|
||||
<artifactId>neo4j-jdbc-driver</artifactId>
|
||||
<version>3.0.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.neo4j</groupId>
|
||||
<artifactId>neo4j-ogm-core</artifactId>
|
||||
<version>2.1.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.neo4j</groupId>
|
||||
<artifactId>neo4j-ogm-embedded-driver</artifactId>
|
||||
<version>2.1.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.inject</groupId>
|
||||
<artifactId>guice</artifactId>
|
||||
<version>4.1.0</version>
|
||||
<classifier>no_aop</classifier>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.collections</groupId>
|
||||
|
@ -63,7 +101,6 @@
|
|||
<artifactId>grep4j</artifactId>
|
||||
<version>${grep4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- web -->
|
||||
|
||||
<!-- marshalling -->
|
||||
|
@ -263,7 +300,8 @@
|
|||
<configuration>
|
||||
<shadedArtifactAttached>true</shadedArtifactAttached>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.graph;
|
||||
|
||||
import org.neo4j.ogm.annotation.GraphId;
|
||||
import org.neo4j.ogm.annotation.NodeEntity;
|
||||
import org.neo4j.ogm.annotation.Relationship;
|
||||
|
||||
/**
|
||||
* @author Danil Kornishev (danil.kornishev@mastercard.com)
|
||||
*/
|
||||
@NodeEntity
|
||||
public class Car {
|
||||
@GraphId
|
||||
private Long id;
|
||||
|
||||
private String make;
|
||||
|
||||
@Relationship(direction = "INCOMING")
|
||||
private Company company;
|
||||
|
||||
public Car(String make, String model) {
|
||||
this.make = make;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getMake() {
|
||||
return make;
|
||||
}
|
||||
|
||||
public void setMake(String make) {
|
||||
this.make = make;
|
||||
}
|
||||
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
public void setModel(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
private String model;
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package com.baeldung.graph;
|
||||
|
||||
import org.neo4j.ogm.annotation.NodeEntity;
|
||||
import org.neo4j.ogm.annotation.Relationship;
|
||||
|
||||
/**
|
||||
* @author Danil Kornishev (danil.kornishev@mastercard.com)
|
||||
*/
|
||||
@NodeEntity
|
||||
public class Company {
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
@Relationship(type="owns")
|
||||
private Car car;
|
||||
|
||||
public Company(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Car getCar() {
|
||||
return car;
|
||||
}
|
||||
|
||||
public void setCar(Car car) {
|
||||
this.car = car;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package com.baeldung.graph;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.neo4j.driver.v1.AuthTokens;
|
||||
import org.neo4j.driver.v1.Driver;
|
||||
import org.neo4j.driver.v1.GraphDatabase;
|
||||
import org.neo4j.driver.v1.Session;
|
||||
import org.neo4j.driver.v1.StatementResult;
|
||||
import org.testng.Assert;
|
||||
|
||||
@Ignore
|
||||
public class Neo4JServerTest {
|
||||
|
||||
@Test
|
||||
public void standAloneDriver() {
|
||||
Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "12345"));
|
||||
Session session = driver.session();
|
||||
|
||||
session.run("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||
"RETURN baeldung, tesla");
|
||||
|
||||
StatementResult result = session.run("MATCH (company:Company)-[:owns]-> (car:Car)" +
|
||||
"WHERE car.make='tesla' and car.model='modelX'" +
|
||||
"RETURN company.name");
|
||||
|
||||
Assert.assertTrue(result.hasNext());
|
||||
Assert.assertEquals(result.next().get("company.name").asString(), "Baeldung");
|
||||
|
||||
session.close();
|
||||
driver.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void standAloneJdbc() throws Exception {
|
||||
Connection con = DriverManager.getConnection("jdbc:neo4j:bolt://localhost/?user=neo4j,password=12345,scheme=basic");
|
||||
|
||||
// Querying
|
||||
try (Statement stmt = con.createStatement()) {
|
||||
stmt.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||
"RETURN baeldung, tesla");
|
||||
|
||||
ResultSet rs = stmt.executeQuery("MATCH (company:Company)-[:owns]-> (car:Car)" +
|
||||
"WHERE car.make='tesla' and car.model='modelX'" +
|
||||
"RETURN company.name");
|
||||
|
||||
while (rs.next()) {
|
||||
Assert.assertEquals(rs.getString("company.name"), "Baeldung");
|
||||
}
|
||||
}
|
||||
con.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.graph;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.neo4j.ogm.config.Configuration;
|
||||
import org.neo4j.ogm.model.Result;
|
||||
import org.neo4j.ogm.session.Session;
|
||||
import org.neo4j.ogm.session.SessionFactory;
|
||||
import org.testng.Assert;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Danil Kornishev (danil.kornishev@mastercard.com)
|
||||
*/
|
||||
public class Neo4jOgmTest {
|
||||
|
||||
@Test
|
||||
public void testOgm() {
|
||||
Configuration conf = new Configuration();
|
||||
conf.driverConfiguration().setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver");
|
||||
|
||||
SessionFactory factory = new SessionFactory(conf, "com.baeldung.graph");
|
||||
Session session = factory.openSession();
|
||||
|
||||
Car tesla = new Car("tesla", "modelS");
|
||||
Company baeldung = new Company("baeldung");
|
||||
|
||||
baeldung.setCar(tesla);
|
||||
|
||||
session.save(baeldung);
|
||||
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("make", "tesla");
|
||||
Result result = session.query("MATCH (car:Car) <-[:owns]- (company:Company)" +
|
||||
" WHERE car.make=$make" +
|
||||
" RETURN company", params);
|
||||
|
||||
Map<String, Object> firstResult = result.iterator().next();
|
||||
|
||||
Assert.assertEquals(firstResult.size(), 1);
|
||||
|
||||
Company actual = (Company) firstResult.get("company");
|
||||
Assert.assertEquals(actual.getName(), baeldung.getName());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,167 @@
|
|||
package com.baeldung.graph;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.neo4j.graphdb.GraphDatabaseService;
|
||||
import org.neo4j.graphdb.Label;
|
||||
import org.neo4j.graphdb.Node;
|
||||
import org.neo4j.graphdb.NotFoundException;
|
||||
import org.neo4j.graphdb.RelationshipType;
|
||||
import org.neo4j.graphdb.Result;
|
||||
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
|
||||
import org.testng.Assert;
|
||||
|
||||
public class Neo4jTest {
|
||||
|
||||
private static GraphDatabaseService graphDb;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
GraphDatabaseFactory graphDbFactory = new GraphDatabaseFactory();
|
||||
graphDb = graphDbFactory.newEmbeddedDatabase(new File("data/cars"));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
graphDb.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPersonCar() {
|
||||
graphDb.beginTx();
|
||||
Node car = graphDb.createNode(Label.label("Car"));
|
||||
car.setProperty("make", "tesla");
|
||||
car.setProperty("model", "model3");
|
||||
|
||||
Node owner = graphDb.createNode(Label.label("Person"));
|
||||
owner.setProperty("firstName", "baeldung");
|
||||
owner.setProperty("lastName", "baeldung");
|
||||
|
||||
owner.createRelationshipTo(car, RelationshipType.withName("owner"));
|
||||
|
||||
Result result = graphDb.execute("MATCH (c:Car) <-[owner]- (p:Person) " +
|
||||
"WHERE c.make = 'tesla'" +
|
||||
"RETURN p.firstName, p.lastName");
|
||||
|
||||
Map<String, Object> firstResult = result.next();
|
||||
Assert.assertEquals("baeldung", firstResult.get("p.firstName"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNode() {
|
||||
|
||||
graphDb.beginTx();
|
||||
|
||||
Result result = graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"})" +
|
||||
"RETURN baeldung");
|
||||
|
||||
Map<String, Object> firstResult = result.next();
|
||||
Node firstNode = (Node) firstResult.get("baeldung");
|
||||
Assert.assertEquals(firstNode.getProperty("name"), "Baeldung");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateNodeAndLink() {
|
||||
graphDb.beginTx();
|
||||
|
||||
Result result = graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||
"RETURN baeldung, tesla");
|
||||
|
||||
Map<String, Object> firstResult = result.next();
|
||||
Assert.assertTrue(firstResult.containsKey("baeldung"));
|
||||
Assert.assertTrue(firstResult.containsKey("tesla"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAndReturn() {
|
||||
graphDb.beginTx();
|
||||
|
||||
graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||
"RETURN baeldung, tesla");
|
||||
|
||||
Result result = graphDb.execute("MATCH (company:Company)-[:owns]-> (car:Car)" +
|
||||
"WHERE car.make='tesla' and car.model='modelX'" +
|
||||
"RETURN company.name");
|
||||
|
||||
Map<String, Object> firstResult = result.next();
|
||||
Assert.assertEquals(firstResult.get("company.name"), "Baeldung");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdate() {
|
||||
graphDb.beginTx();
|
||||
|
||||
graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||
"RETURN baeldung, tesla");
|
||||
|
||||
Result result = graphDb.execute("MATCH (car:Car)" +
|
||||
"WHERE car.make='tesla'" +
|
||||
" SET car.milage=120" +
|
||||
" SET car :Car:Electro" +
|
||||
" SET car.model=NULL" +
|
||||
" RETURN car");
|
||||
|
||||
Map<String, Object> firstResult = result.next();
|
||||
Node car = (Node) firstResult.get("car");
|
||||
|
||||
Assert.assertEquals(car.getProperty("milage"), 120L);
|
||||
Assert.assertEquals(car.getLabels(), Arrays.asList(Label.label("Car"), Label.label("Electro")));
|
||||
|
||||
try {
|
||||
car.getProperty("model");
|
||||
Assert.fail();
|
||||
} catch (NotFoundException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelete() {
|
||||
graphDb.beginTx();
|
||||
|
||||
graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " +
|
||||
"-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" +
|
||||
"RETURN baeldung, tesla");
|
||||
|
||||
graphDb.execute("MATCH (company:Company)" +
|
||||
" WHERE company.name='Baeldung'" +
|
||||
" DELETE company");
|
||||
|
||||
Result result = graphDb.execute("MATCH (company:Company)" +
|
||||
" WHERE company.name='Baeldung'" +
|
||||
" RETURN company");
|
||||
|
||||
Assert.assertFalse(result.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBindings() {
|
||||
graphDb.beginTx();
|
||||
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("name", "baeldung");
|
||||
params.put("make", "tesla");
|
||||
params.put("model", "modelS");
|
||||
|
||||
Result result = graphDb.execute("CREATE (baeldung:Company {name:$name}) " +
|
||||
"-[:owns]-> (tesla:Car {make: $make, model: $model})" +
|
||||
"RETURN baeldung, tesla", params);
|
||||
|
||||
Map<String, Object> firstResult = result.next();
|
||||
Assert.assertTrue(firstResult.containsKey("baeldung"));
|
||||
Assert.assertTrue(firstResult.containsKey("tesla"));
|
||||
|
||||
Node car = (Node) firstResult.get("tesla");
|
||||
Assert.assertEquals(car.getProperty("model"), "modelS");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue