Neo4j Java操作
1. Neo4j Java Driver方式操作
使用该方式对数据进行操作时,必须先将Neo4j的服务启动起来。
从官方下载neo4j的Java驱动:https://neo4j.com/download-center/#drivers
将驱动导入eclipse的项目中,具体操作如下:
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.neo4j.driver.v1.Transaction;
import org.neo4j.driver.v1.TransactionWork;
import static org.neo4j.driver.v1.Values.parameters;
public class Neo4jConn implements AutoCloseable {
private final Driver driver;
public Neo4jConn(String uri, String user, String password) {
driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password));
}
@Override
public void close() throws Exception {
driver.close();
}
public void printGreeting(final String message) {
try (Session session = driver.session()) {
String greeting = session.writeTransaction(new TransactionWork
@Override
public String execute(Transaction tx) {
StatementResult result = tx.run("CREATE (a:Greeting) " + "SET a.message = $message "
+ "RETURN a.message + ', from node ' + id(a)", parameters("message", message));
return result.single().get(0).asString();
}
});
System.out.println(greeting);
}
}
public static void main(String... args) throws Exception {
try (Neo4jConn greeter = new Neo4jConn("bolt://localhost:7687", "neo4j", "123456")) {
System.out.println("get conn!!!");
greeter.printGreeting("hello, world");
}
}
}
2.Neo4j 嵌入式(Embedded)Java AP方式操作,包含Native JAVA API 和Cypher JAVA API两种方式
使用该方式时,必选先停止Neo4j的服务,否则报错:
java.io.IOException: Couldn't lock lock file C:\TPNeo4jDB\lock because another process already holds the lock.
1> 节点操作
(1)节点:GraphDatabaseService().createNode()
(2)建立两个节点之间的关系:Node.createRelationShipTo(...)
(3)节点添加属性:Node.setProperty(name,value) Neo4j中不允许null属性值
(4)节点属性删除:Node.removeProperty(String propertyName)
2> 关系操作
(1)关系:RelationshipType()接口,DynamicRelationShipType()
(2)枚举类型:实现👆的接口
(3)关系添加属性:RelationShip.setProperty(String name, Object value)
(4)创建节点与节点之间的关系:node1.createRelationshipTo(node2,关系类型)
3> 标签
(1)接口Label(),可以通过枚举类型实现标签
(2)节点添加标签:Node.addLabel()
需要先将neo4j包中的Lib目录下所有的jar文件导入到eclipse
Native JAVA API详细代码:
package com.dwlm;
import java.io.File;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
public class Neo4jNativeJavaApi {
//使用该方式Neo4j必须为停止状态
public static void main(String[] args) {
// 指定Neo4j数据存储路径
String dirName = "/Users/goboo/Library/neo4j-community-3.4.10/data/databases/graph.db";
File path = new File(dirName);
// 创建Neo4j数据库
GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
GraphDatabaseService db = dbFactory.newEmbeddedDatabase(path);
try (Transaction tx = db.beginTx()) {
// 进行数据库的操作
// 1.创建节点,同时要设置节点的标签
Node javaNode = db.createNode(Tutorials.JAVA);
Node scalaNode = db.createNode(Tutorials.SCALA);
// 2.设置节点属性
javaNode.setProperty("TutorialID", "JAVA001");
javaNode.setProperty("Title", "Learn Java");
javaNode.setProperty("NoOfChapters", "25");
javaNode.setProperty("Status", "Completed");
scalaNode.setProperty("TutorialID", "SCALA001");
scalaNode.setProperty("Title", "Learn Scala");
scalaNode.setProperty("NoOfChapters", "20");
scalaNode.setProperty("Status", "Completed");
// 3.创建关系,同时设置关系的标签
Relationship relationship = javaNode.createRelationshipTo(scalaNode, TutorialRelationships.JVM_LANGIAGES);
// 4.设置关系属性
relationship.setProperty("Id", "1234");
relationship.setProperty("OOPS", "YES");
relationship.setProperty("FP", "YES");
tx.success();
}
System.out.println("Done successfully");
}
}
Cypher Java API详细代码:
3. spring data Neo4j方式操作