Neo4j 框架介绍和特性
Neo4j 是一个高性能的、开源的图数据库。它将数据存储为图结构,其中节点表示实体,边表示实体之间的关系。这种图数据模型非常适合处理复杂的关系型数据,能够高效地进行关系查询和遍历。
Neo4j 的主要特性包括:
- 强大的图查询语言 Cypher:Cypher 是一种专门为 Neo4j 设计的声明式查询语言,使得查询和操作图数据变得直观和高效。
Cypher 语言常用操作基本使用说明:
创建节点:
CREATE (n:Person {name: 'Alice', age: 30})
上述语句创建了一个名为"Alice",年龄为 30 岁的"Person"类型的节点。
创建关系:
MATCH (a:Person {name: 'Alice'}), (b:Person {name: 'Bob'})
CREATE (a)-[r:FRIEND {since: '2023'}]->(b)
1.2.
此语句在"Alice"和"Bob"节点之间创建了名为"FRIEND",建立时间为"2023"的关系。
查询节点:
MATCH (n:Person) RETURN n
1.
返回所有"Person"类型的节点。
查询具有特定属性的节点:
MATCH (n:Person {age: 30}) RETURN n
1.
查找年龄为 30 岁的"Person"节点。
更新节点属性:
MATCH (n:Person {name: 'Alice'})
SET n.age = 31
RETURN n
1.2.3.
将"Alice"节点的年龄更新为 31 岁。
删除节点或关系:
MATCH (n:Person {name: 'Alice'})
DELETE n
1.2.
删除"Alice"节点。
MATCH (a:Person {name: 'Alice'})-[r:FRIEND]->(b:Person {name: 'Bob'})
DELETE r
1.2.
删除"Alice"和"Bob"之间的"FRIEND"关系。
按照条件筛选和排序结果:
MATCH (n:Person)
WHERE n.age > 20
RETURN n.name, n.age
ORDER BY n.age DESC
1.2.3.4.
查找年龄大于 20 岁的"Person"节点,并按照年龄降序排列返回其姓名和年龄。
使用聚合函数:
MATCH (n:Person)
RETURN COUNT(n) AS personCount
1.2.
计算"Person"节点的数量。
路径查询:
MATCH p = (a:Person {name: 'Alice'})-[*1..3]->(b:Person {name: 'Bob'})
RETURN p
1.2.
查找从"Alice"到"Bob"最多经过 3 步的路径。
关联查询:
MATCH (a:Person)-[r:FRIEND]->(b:Person)
WHERE a.name = 'Alice' AND b.age > 25
RETURN b
1.2.3.
查找"Alice"的朋友中年龄大于 25 岁的人。
系统安装及基本使用说明
(一)使用 Docker 安装 Neo4j
- 拉取 Neo4j 的 Docker 镜像:docker pull neo4j
- 启动 Neo4j 容器:docker run -d -p 7474:7474 -p 7687:7687 --name neo4j neo4j
(二)启动 Neo4j 服务
(三)基本使用
- 访问 Neo4j 浏览器界面,通常在 http://localhost:7474/ 。
- 使用 Cypher 语言创建节点、关系和进行查询操作。
项目依赖配置(pom.xml)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 其他必要的依赖 -->
</dependencies>
1.2.3.4.5.6.7.8.9.10.11.
YAML 属性文件配置(application.yml)
spring:
data:
neo4j:
uri: bolt://localhost:7687
username: your_username
password: your_password
1.2.3.4.5.6.
代码示例
实体类
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
@NodeEntity
public class Person {
@Id
@GeneratedValue
private Long id;
private String name;
// 新增:朋友列表
private List<Person> friends;
// 构造函数、getter 和 setter 方法
}
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.
数据访问层
import org.springframework.data.neo4j.repository.Neo4jRepository;
public interface PersonRepository extends Neo4jRepository<Person, Long> {
// 新增:根据用户名查找用户及其朋友
Person findByNameAndFetchFriends(String name);
// 新增:获取知识图谱的方法
List<Person> getKnowledgeGraph();
}
1.2.3.4.5.6.7.8.9.10.
服务层
import org.springframework.stereotype.Service;
@Service
public class PersonService {
private final PersonRepository personRepository;
public PersonService(PersonRepository personRepository) {
this.personRepository = personRepository;
}
public void savePerson(Person person) {
personRepository.save(person);
}
// 新增:添加朋友关系
public void addFriendship(String personName1, String personName2) {
Person person1 = personRepository.findByNameAndFetchFriends(personName1);
Person person2 = personRepository.findByNameAndFetchFriends(personName2);
person1.getFriends().add(person2);
person2.getFriends().add(person1);
personRepository.save(person1);
personRepository.save(person2);
}
// 新增:获取用户的朋友列表
public List<Person> getFriends(String personName) {
Person person = personRepository.findByNameAndFetchFriends(personName);
return person.getFriends();
}
// 新增:获取知识图谱
public List<Person> getKnowledgeGraph() {
return personRepository.getKnowledgeGraph();
}
}
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.
控制器层
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PersonController {
private final PersonService personService;
public PersonController(PersonService personService) {
this.personService = personService;
}
@PostMapping("/persons")
public void createPerson(@RequestBody Person person) {
personService.savePerson(person);
}
// 新增:添加朋友关系的接口
@PostMapping("/addFriendship")
public void addFriendship(@RequestBody AddFriendshipRequest request) {
personService.addFriendship(request.getPersonName1(), request.getPersonName2());
}
// 新增:获取用户朋友列表的接口
@GetMapping("/friends/{personName}")
public List<Person> getFriends(@PathVariable String personName) {
return personService.getFriends(personName);
}
// 新增:查看知识图谱的接口
@GetMapping("/knowledgeGraph")
public List<Person> getKnowledgeGraph() {
return personService.getKnowledgeGraph();
}
// 新增:添加朋友关系的请求类
static class AddFriendshipRequest {
private String personName1;
private String personName2;
public String getPersonName1() {
return personName1;
}
public void setPersonName1(String personName1) {
this.personName1 = personName1;
}
public String getPersonName2() {
return personName2;
}
public void setPersonName2(String personName2) {
this.personName2 = personName2;
}
}
}
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.
前端代码(使用 Vue.js 示例)
<template>
<div>
<input v-model="newPerson.name" placeholder="输入人员姓名" />
<button @click="createPerson">创建人员</button>
<input v-model="personName1" placeholder="输入第一个人员姓名" />
<input v-model="personName2" placeholder="输入第二个人员姓名" />
<button @click="addFriendship">添加朋友关系</button>
<input v-model="personNameForFriends" placeholder="输入要查看朋友的人员姓名" />
<button @click="getFriends">获取朋友列表</button>
<button @click="getKnowledgeGraph">查看知识图谱</button>
<ul v-if="knowledgeGraph.length > 0">
<li v-for="person in knowledgeGraph" :key="person.name">{{ person.name }}</li>
</ul>
<ul v-if="friends.length > 0">
<li v-for="friend in friends" :key="friend.name">{{ friend.name }}</li>
</ul>
</div>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
newPerson: {
name: ''
},
personName1: '',
personName2: '',
personNameForFriends: '',
knowledgeGraph: [],
friends: []
};
},
methods: {
createPerson() {
axios.post('/persons', this.newPerson)
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
},
addFriendship() {
axios.post('/addFriendship', {
personName1: this.personName1,
personName2: this.personName2
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});
},
getFriends() {
axios.get(`/friends/${this.personNameForFriends}`)
.then(response => {
this.friends = response.data;
})
.catch(error => {
console.error(error);
});
},
getKnowledgeGraph() {
axios.get('/knowledgeGraph')
.then(response => {
this.knowledgeGraph = response.data;
})
.catch(error => {
console.error(error);
});
}
}
}
</script>
1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.80.81.82.83.
总结:
本次架构方案通过整合 Springboot 和 Neo4j,实现了一套包含人员创建、朋友关系管理以及知识图谱查看的功能系统。在实现过程中,充分考虑了功能的完整性、性能优化以及用户体验等方面。但在实际应用中,仍需根据具体业务需求和数据规模进行进一步的调整和优化,以确保系统的稳定和高效运行。