Neo4j入门指南
Neo4j是一个高性能的,NOSQL图形数据库,它将结构化数据存储在网络上而不是表中。它是一个嵌入式的、基于磁盘的、具备完全的事务特性的Java持久化引擎,但是它将结构化数据存储在网络(从数学角度叫做图)上而不是表中。Neo4j也可以被看作是一个高性能的图引擎,该引擎具有成熟数据库的所有特性。程序员工作在一个面向对象的、灵活的网络结构下而不是严格、静态的表中——但是他们可以享受到具备完全的事务特性、企业级的数据库的所有好处。
从Docker Hub上pull一个Neo4j的镜像
docker pull neo4j
启动一个Neo4j的容器, 在Home目录下建立一个/neo4j/data文件夹用于存放数据
docker run -d
--name neo4j
--publish=7474:7474
--publish=7687:7687
--volume=$HOME/neo4j/data:/data
neo4j
在浏览器中访问 http://ipaddress:7474 可以看到如下的UI界面,输入默认用户名和密码neo4j即可连接到neo4j数据库。
Neo4j使用Cypher语法操作数据库,CRUD示例
创建节点
CREATE (e:Country{name:"中国"})
CREATE (e1:Country{name:"美国"})
CREATE (c:Company{name:"腾讯",createtime:"2000-12-20"})
CREATE (c1:Company{name:"苹果",createtime:"2002-07-08"})
CREATE (c2:Company{name:"阿里",createtime:"2004-12-20"})
CREATE (c3:Company{name:"美团",createtime:"2010-12-20"})
CREATE (c4:Company{name:"亚马逊",createtime:"2007-07-08"})
创建关系
MATCH (e:Country) WHERE e.name="中国" MATCH (c:Company) WHERE c.name="腾讯" CREATE (c)-[r:BELONG_TO]->(e) RETURN e,c,r
MATCH (e:Country) WHERE e.name="中国" MATCH (c:Company) WHERE c.name="美团" CREATE (c)-[r:BELONG_TO]->(e) RETURN e,c,r
MATCH (e:Country) WHERE e.name="中国" MATCH (c:Company) WHERE c.name="阿里" CREATE (c)-[r:BELONG_TO]->(e) RETURN e,c,r
MATCH (e:Country) WHERE e.name="美国" MATCH (c:Company) WHERE c.name="亚马逊" CREATE (c)-[r:BELONG_TO]->(e) RETURN e,c,r
MATCH (e:Country) WHERE e.name="美国" MATCH (c:Company) WHERE c.name="苹果" CREATE (c)-[r:BELONG_TO]->(e) RETURN e,c,r
MATCH (e:Country{name:"中国"}) MATCH (e1:Country{name:"美国"}) CREATE (e)-[r:PARTNER{desc:"成为合作伙伴",time:"2000-02-08"}]->(e1) RETURN e,e1,r
MATCH (e:Country{name:"中国"}) MATCH (e1:Country{name:"美国"}) CREATE (e1)-[r:PARTNER{desc:"成为合作伙伴",time:"2000-02-08"}]->(e) RETURN e,e1,r
查询节点、关系
# 查询 Company 类型的节点
MATCH (e:Company) RETURN e
MATCH (e:Company) RETURN e.name,e.createtime LIMIT 25
# 查询所有的节点
MATCH (n) RETURN n LIMIT 25
# 查询 BELONG_TO 类型的关系
MATCH p=()-[r:BELONG_TO]->() RETURN p LIMIT 25
MATCH (e:Company{name:"美团"})-[r]-(e1:Country{name:"中国"}) RETURN e,r,e1
# 查询所有的关系
MATCH p=()-->() RETURN p LIMIT 25
删除节点、关系
# 删除节点(删除节点前,需保证该节点不存在关系)
MATCH (e:Company) WHERE e.name="美团" DELETE e
# 仅仅删除`美团`与`中国`之间的关系
MATCH (e:Company{name:"美团"})-[r]-(e1:Country{name:"中国"}) DELETE r
# 删除`美团`与`中国`节点以及他们之间的关系
MATCH (e:Company{name:"美团"})-[r]-(e1:Country{name:"中国"}) DELETE e,r,e1
# 删除关系为BELONG_TO的节点以及他们的关系
MATCH p=()-[r:BELONG_TO]->() DELETE p
# 删除所有存在关系的节点以及他们之间的关系
MATCH p=()-->() DELETE p
修改节点、关系属性
MATCH (e:Company) WHERE e.name="美团" SET e.name="美团点评" RETURN e
MATCH (e:Company{name:"美团点评"})-[r]->(c:Country) SET r.name="属于" RETURN e,r,c
所需依赖
compile('org.neo4j:neo4j:3.4.3')
示例代码
public static void main(String[] args) throws Exception {
GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
GraphDatabaseService db = dbFactory.newEmbeddedDatabase(new File("E:/TPNeo4jDB"));
try (Transaction tx = db.beginTx()) {
Node javaNode = db.createNode(Tutorials.JAVA);
javaNode.setProperty("TutorialID", "JAVA001");
javaNode.setProperty("Title", "Learn Java");
javaNode.setProperty("NoOfChapters", "25");
javaNode.setProperty("Status", "Completed");
Node scalaNode = db.createNode(Tutorials.SCALA);
scalaNode.setProperty("TutorialID", "SCALA001");
scalaNode.setProperty("Title", "Learn Scala");
scalaNode.setProperty("NoOfChapters", "20");
scalaNode.setProperty("Status", "Completed");
Relationship relationship = javaNode.createRelationshipTo
(scalaNode, TutorialRelationships.JVM_LANGIAGES);
relationship.setProperty("Id", "1234");
relationship.setProperty("OOPS", "YES");
relationship.setProperty("FP", "YES");
tx.success();
}
System.out.println("Done successfully");
}
public enum Tutorials implements Label {
JAVA, SCALA, SQL, NEO4J,;
}
public enum TutorialRelationships implements RelationshipType {
JVM_LANGIAGES, NON_JVM_LANGIAGES;
}
所需依赖
compile('org.neo4j.driver:neo4j-java-driver:1.6.1')
示例代码
public static void main(String[] args) throws Exception {
Driver driver = GraphDatabase.driver("bolt://192.168.15.145:7687", AuthTokens.basic("neo4j", "123456"));
Session session = driver.session();
Map saveMap = new HashMap<>(2);
saveMap.put("name", "Arthur001");
saveMap.put("title", "King001");
session.run("CREATE (a:Person {name: {name}, title: {title}})", saveMap);
Map queryMap = new HashMap<>(1);
queryMap.put("name", "Arthur001");
StatementResult result = session.run("MATCH (a:Person) WHERE a.name = {name} " +
"RETURN a.name AS name, a.title AS title", queryMap);
while (result.hasNext()) {
Record record = result.next();
System.out.println(record.get("title").asString() + " " + record.get("name").asString());
}
session.close();
driver.close();
}
所需依赖
compile ('org.springframework.data:spring-data-neo4j:5.0.8.RELEASE')
示例代码
Domain Entities
@NodeEntity
public class Movie {
@Id @GeneratedValue Long id;
String title;
Person director;
@Relationship(type="ACTS_IN", direction = Relationship.INCOMING)
Set actors;
@Relationship(type = "RATED")
List ratings;
}
Repositories
interface MovieRepository extends Neo4jRepository<Movie, Long> {
// derived finder
Movie findByTitle(String title);
@Query("MATCH (m:Movie)<-[rating:RATED]-(user) WHERE id(movie)={movie} return rating")
List getRatings(@Param("movie") Movie movie);
// Co-Actors
Set findByActorsMoviesActorName(String name);
@Query("MATCH (movie:Movie)-[:HAS_GENRE]->(genre)<-[:HAS_GENRE]-(similar)
WHERE id(movie) = {0} RETURN similar")
List findSimilarMovies(Movie movie);
}
Configuration
@Configuration
@EnableTransactionManagement
@ComponentScan("org.neo4j.cineasts")
@EnableNeo4jRepositories("org.neo4j.cineasts.repository")
public class PersistenceContext {
@Bean
public SessionFactory getSessionFactory() {
return new SessionFactory(configuration(), "org.neo4j.cineasts.domain");
}
@Bean
public Neo4jTransactionManager transactionManager() throws Exception {
return new Neo4jTransactionManager(getSessionFactory());
}
@Bean
public org.neo4j.ogm.config.Configuration configuration() {
return new org.neo4j.ogm.config.Configuration.Builder()
.uri("bolt://localhost")
.build();
}
}
Example
@Autowired MovieRepository repo;
Iterable movies = repo.findAll();
Movie movie = repo.findByTitle("The Matrix");
repo.save(movie);
List ratings = repo.getRatings(movie);