标签:org 高级 sage 标准 public img exception 操作 hda
其他开发语言如.NET、JavaScript、Python、PHP等也希望能够和Neo4j相互集成,就像操作传统的关系型数据库那样熟练的操作Neo4j这个图数据库。
Java Driver:面向Java开发人员。
.net Driver:面向.net开发人员。
JavaScript Driver:面向前端开发人员。
Python Driver:面向Python开发人员。
版本 | 依赖库 | 描述 |
---|---|---|
Neo4j社区版 | org.neo4j:neo4j | 社区版,有完全的ACID事务 |
Neo4j企业版 | org.neo4j:neo4j-enterprise | 企业版,添加高级监控,在线备份和高可用功能集群 |
<!-- neo4j的依赖包 -->
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>3.4.18</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
package com.sunxiaping.neo4j;
import org.junit.Before;
import org.junit.Test;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import java.io.File;
public class EmbeddedNeo4jTest {
GraphDatabaseService graphDb = null;
@Before
public void before() {
//数据库文件的目录地址
String storeDir = "D:\\develop\\Neo4j Desktop\\data\\neo4jDatabases\\database-420aa183-f1e8-44e5-b34e-dbe6190064f7\\installation-3.4.18\\data\\databases\\graph.db";
//创建GraphDatabaseService实例,这代表是数据库的实例
graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(new File(storeDir));
Transaction transaction = graphDb.beginTx();
}
@Test
public void after() {
//关闭数据库
if (null != graphDb) {
graphDb.shutdown();
}
}
/**
* 连接数据库
*/
@Test
public void connectDb() {
System.out.println("连接数据库..." + graphDb);
}
}
package com.sunxiaping.neo4j;
import org.junit.Before;
import org.junit.Test;
import org.neo4j.graphdb.*;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.io.fs.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.Map;
public class EmbeddedNeo4jTest {
GraphDatabaseService graphDb = null;
Transaction transaction = null;
String storeDir = null;
@Before
public void before() {
//数据库文件的目录地址
storeDir = "D:\\develop\\neo4j-community-3.5.19\\data\\databases\\graph.db";
//创建GraphDatabaseService实例,这代表是数据库的实例
graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(new File(storeDir));
transaction = graphDb.beginTx();
}
@Test
public void after() {
//关闭数据库
if (null != graphDb) {
graphDb.shutdown();
}
}
/**
* 通过删除数据库文件的方式创建数据库
*/
@Test
public void deleteDb() throws IOException {
FileUtils.deleteRecursively(new File(storeDir));
}
/**
* 新增节点和关系
*/
@Test
public void createDataTest() {
try {
//创建节点
Node firstNode = graphDb.createNode();
firstNode.setProperty("Spring Festival", "春节");
firstNode.setProperty("Peking Opera", "京剧");
firstNode.setProperty("Dumpling", "饺子");
firstNode.addLabel(Label.label("China"));
//创建节点
Node secondNode = graphDb.createNode();
secondNode.setProperty("generous", "慷慨");
secondNode.setProperty("be ready to help others", "乐于助人");
secondNode.setProperty("Hospitality", "热情好客");
secondNode.addLabel(Label.label("Person"));
secondNode.addLabel(Label.label("Chinese"));
//创建关系
Relationship relationship = firstNode.createRelationshipTo(secondNode, RelationshipType.withName("CountryPeople"));
relationship.setProperty("message", "国家和人");
System.out.println("============firstNode===========");
Object spring_festival = firstNode.getProperty("Spring Festival");
System.out.println("spring_festival = " + spring_festival);
Object peking_opera = firstNode.getProperty("Peking Opera");
System.out.println("peking_opera = " + peking_opera);
Object dumpling = firstNode.getProperty("Dumpling");
System.out.println("dumpling = " + dumpling);
Map<String, Object> allProperties = firstNode.getAllProperties();
System.out.println("allProperties = " + allProperties);
Iterable<Label> labels = firstNode.getLabels();
System.out.println("labels = " + labels);
System.out.println("============secondNode===========");
Object generous = secondNode.getProperty("generous");
System.out.println("generous = " + generous);
Object be_ready_to_help_others = secondNode.getProperty("be ready to help others");
System.out.println("be_ready_to_help_others = " + be_ready_to_help_others);
Object hospitality = secondNode.getProperty("Hospitality");
System.out.println("hospitality = " + hospitality);
System.out.println("============relationship===========");
Object message = relationship.getProperty("message");
System.out.println("message = " + message);
//提交事务
transaction.success();
} catch (Exception e) {
transaction.failure();
} finally {
transaction.close();
}
}
/**
* 获取节点和关系
*/
@Test
public void getDataTest() {
try {
ResourceIterator<Node> firstNodes = graphDb.findNodes(Label.label("China"));
firstNodes.forEachRemaining(node -> {
System.out.println("node = " + node.getAllProperties());
Iterable<Relationship> relationships = node.getRelationships();
relationships.forEach(relationship -> {
System.out.println("relationship = " + relationship.getType());
Node endNode = relationship.getEndNode();
System.out.println("endNode = " + endNode.getAllProperties());
});
});
//提交事务
transaction.success();
} catch (Exception e) {
transaction.failure();
} finally {
transaction.close();
}
}
@Test
public void cypherTest() {
try {
String cypher = "MATCH (n) RETURN n";
Result result = graphDb.execute(cypher);
while (result.hasNext()) {
Map<String, Object> map = result.next();
map.forEach((key, value) -> {
System.out.println(key + ":" + value);
});
}
//提交事务
transaction.success();
} catch (Exception e) {
transaction.failure();
} finally {
transaction.close();
}
}
}
标签:org 高级 sage 标准 public img exception 操作 hda
原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/13362100.html