赞
踩
neo4j是一个高性能的NOSQL图形数据库,打破了关系型数据库的束缚,应用前景十分广泛。
我们尝试通过neo4j构建中医方剂和药物关系图。
我们可以通过docker安装neo4j
docker pull neo4j
创建 $HOME/neo4j/data 目录,然后通过这条命令运行neo4j,在此之前记得检查防火墙的端口开放情况
docker run --publish=7474:7474 --publish=7687:7687 --volume=$HOME/neo4j/data:/data -d neo4j
查看运行情况
docker ps
通过浏览器访问neo4j的控制面板,看到这个页面即为安装成功(默认密码是neo4j,登陆后需要重置密码)
关于springdata neo4j,还可以参考spring的官方示例项目
https://github.com/neo4j-examples/movies-java-spring-data-neo4j
首先构建一个springboot项目,添加如下的依赖
- <dependencies>
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.3.2</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-cache</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-jpa</artifactId>
- </dependency>
- <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>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid-spring-boot-starter</artifactId>
- <version>1.1.9</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.neo4j</groupId>
- <artifactId>neo4j</artifactId>
- <version>3.2.9</version>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>org.neo4j</groupId>
- <artifactId>neo4j-ogm-embedded-driver</artifactId>
- <version>${neo4j-ogm.version}</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.neo4j</groupId>
- <artifactId>neo4j-ogm-bolt-driver</artifactId>
- <version>${neo4j-ogm.version}</version>
- </dependency>
- </dependencies>
配置springboot的yml配置文件
- spring:
- data:
- neo4j:
- uri: bolt://你的地址:7687
- username: 你的账户
- password: 你的密码
我们新建一个方剂bean和一个药物bean
这个方剂bean中有一个List<YaowuWithLinked>属性,用于指明哪些药物bean与这个方剂bean相连
- @NodeEntity//必须要加这个注解
- public class FangjiWithLinked {//方剂节点
- @Id
- @GeneratedValue
- private Long id;//每个bean都必须添加这个属性
- private String fangjiName;//方剂名称
-
- @JsonIgnoreProperties("fangjiWithLinked")
- @Relationship(type = "containes", direction = Relationship.INCOMING)//这里的type是自己定义的
- private List<YaowuWithLinked> yaowuWithLinkeds;//与这个方剂节点相连的药物节点
-
- public FangjiWithLinked() {//记得添加空构造器
- }
-
- public List<YaowuWithLinked> getYaowuWithLinkeds() {
- return yaowuWithLinkeds;
- }
-
- public FangjiWithLinked(String fangjiName) {
- this.fangjiName = fangjiName;
- }
-
- public String getFangjiName() {
- return fangjiName;
- }
-
- public void setFangjiName(String fangjiName) {
- this.fangjiName = fangjiName;
- }
-
- public void addYaowuWithLinked(YaowuWithLinked... yaowu) {//快速添加药物节点的方法
- if (this.yaowuWithLinkeds == null) {
- this.yaowuWithLinkeds = new LinkedList<>();
- }
- this.yaowuWithLinkeds.addAll(Arrays.asList(yaowu));
- }
- }
- @NodeEntity
- public class YaowuWithLinked {
- @Id
- @GeneratedValue
- private Long id;
- private String name;
-
- public YaowuWithLinked() {
- }
-
- public YaowuWithLinked(String name) {
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
- }
然后创建dao,继承Neo4jRepository,即可实现基础的CRUD操作(复杂的逻辑还需要手动编写接口方法)。
public interface NodeDao extends Neo4jRepository<FangjiWithLinked, Long> {}
我们在test类中,试着插入一个节点,看看能否成功
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class KnowledgeGraphApplicationTests {
-
- @Autowired
- private NodeDao nodeDao;
-
- @Test
- public void contextLoads() {
- }
-
- @Test
- public void testInsertNewNode() {
- FangjiWithLinked test = new FangjiWithLinked("四君子汤");
- YaowuWithLinked yaowu1 = new YaowuWithLinked("人参");
- YaowuWithLinked yaowu2 = new YaowuWithLinked("白术");
- YaowuWithLinked yaowu3 = new YaowuWithLinked("茯苓");
- YaowuWithLinked yaowu4 = new YaowuWithLinked("甘草");
- test.addYaowuWithLinked(yaowu1,yaowu2,yaowu3,yaowu4);
- nodeDao.save(test);
- }
- }
调用这个test方法,然后查看neo4j的控制台,可以看到,已经成功的创建了节点
接下来,我们开始考虑如何将这些节点展示到前端,并试着添加搜索逻辑
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。