我正在尝试使用Java连接到Neo4J,并且一直关注此sample demo。
我设置了一个新的Maven项目,并添加了代码,这些代码在节点之间创建了一些节点和关系。这就是我的主要课程的样子 -
package neo4j;
import org.neo4j.cypher.internal.compiler.v2_3.No;
import org.neo4j.graphdb.*;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import java.io.File;
/**
* Created by manish on 5/15/17.
*/
public class Neo4JDemo {
public enum NodeType implements Label {
Person, Course, Dummy;
}
public enum RelationType implements RelationshipType {
Knows, BelongsTo;
}
public static void main(String[] args) {
GraphDatabaseFactory databaseFactory = new GraphDatabaseFactory();
File file = new File("/home/manish/Documents/Development/Software/neo4j-community-3.1.4/data/databases/graph.db");
GraphDatabaseService graphDatabaseService = databaseFactory.newEmbeddedDatabase(file);
try(Transaction tx = graphDatabaseService.beginTx()) {
Node bobNode = graphDatabaseService.createNode(NodeType.Person);
bobNode.setProperty("PID", 5001);
bobNode.setProperty("Name", "Bob");
bobNode.setProperty("Age", 23);
Node bNode = graphDatabaseService.createNode(NodeType.Dummy);
bNode.setProperty("PID", 5001);
bNode.setProperty("Name", "Bob");
bNode.setProperty("Age", 23);
Node aliceNode = graphDatabaseService.createNode(NodeType.Person);
aliceNode.setProperty("PID", 5002);
aliceNode.setProperty("Name", "Alice");
aliceNode.setProperty("Age", 20);
Node eveNode = graphDatabaseService.createNode(NodeType.Person);
eveNode.setProperty("PID", 5003);
eveNode.setProperty("Name", "Eve");
eveNode.setProperty("Age", 25);
Node itNode = graphDatabaseService.createNode(NodeType.Course);
itNode.setProperty("ID", 1);
itNode.setProperty("Name", "IT Beginners");
itNode.setProperty("Location", "Room 154");
Node ecNode = graphDatabaseService.createNode(NodeType.Course);
ecNode.setProperty("ID", 2);
ecNode.setProperty("Name", "Advanced Electronics");
bobNode.createRelationshipTo(aliceNode, RelationType.Knows);
Relationship bobRelIt = bobNode.createRelationshipTo(itNode, RelationType.BelongsTo);
bobRelIt.setProperty("Role", "Student");
Relationship bobRelEc = bobNode.createRelationshipTo(ecNode, RelationType.BelongsTo);
bobRelEc.setProperty("Role", "Supply Teacher");
Relationship aliceRelIt = aliceNode.createRelationshipTo(itNode, RelationType.BelongsTo);
aliceRelIt.setProperty("Role", "Teacher");
tx.success();
}
graphDatabaseService.shutdown();
}
}
当我运行项目时,它成功编译 -
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for neo4j:neo4j-setup:jar:1.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 10, column 15
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building neo4j-setup 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ neo4j-setup ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/manish/Documents/Development/Thesis/Neo4J-Test1/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ neo4j-setup ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /home/manish/Documents/Development/Thesis/Neo4J-Test1/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.420 s
[INFO] Finished at: 2017-05-15T19:24:46-04:00
[INFO] Final Memory: 25M/263M
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
但是,当我转到localhost:7474
的Neo4J浏览器时,没有通过此Java代码创建的类型的节点或关系 -
我不确定会出现什么问题,并希望对此有任何帮助。
更新 - 包括pom.xml
-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>neo4j</groupId>
<artifactId>neo4j-setup</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>
<name>neo4j-setup</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>3.1.4</version>
</dependency>
</dependencies>
</project>