Neo4j 图数据库

Neo4j是一个高性能的,NOSQL图形数据库,它将结构化数据存储在网络上而不是表中。由于知识图谱中存在大量的关系型信息(实体—关系—实体), 使用结构化数据库进行存储将产生大量的冗余存储信息, 因此将图数据库作为知识图谱的存储容器成为流行的选择。当前较为常用的图数据库主要有 Neo4j 等。

Neo4j的安装

  1. 下载

官方下载链接:https://neo4j.com/download-center/#community

根据JDK版本,选择下载Neo4j,这里下载neo4j-community-3.5.5-windows.zip,支持JDK1.8。

下载好之后,直接解压到合适的路径就可以了,无需安装。

      1. 配置环境变量

接下来要配置环境变量了,与刚才JAVA环境变量的配置方法极为相似,因此在这里只进行简单描述。

  1. 在系统变量区域,新建环境变量,命名为NEO4J_HOME,变量值设置为刚才neo4j的安装路径,我这里是D:\software\neo4j\neo4j-community-4.2.4。
  2. 编辑系统变量区的Path,点击新建,然后输入 %NEO4J_HOME%\bin,最后,点击确定进行保存就可以了。
      1. 启动Neo4j

以管理员身份运行cmd,然后,在命令行处输入neo4j.bat console

如出现此界面,则证明neo4j启动成功。

在浏览器中输入界面中给出的网址http://localhost:7474/,则会显示如下界面。

默认的用户名和密码均为neo4j。

SpringBoot集成Neo4j

前台可以使用vue + d3.js,后台是SpringBoot2配合Neo4j。

      1. pom文件

Spring生态中Spring-data部分不仅仅提供了Spring-data-jpa , 也提供了Spring-data-neo4j 支持spring和 neo4j的完美融合:

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-data-neo4j</artifactId>

</dependency>

      1. yml文件

spring:

  data:

    neo4j:

      uri: bolt://localhost:7687

      username: neo4j

      password: neusoft

      1. Config配置

package com.neusoft.hifly.neo4j.config;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;

import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration

// Neo4j扫描Repositories所在包,可以理解为mybatis扫描mapper

@EnableNeo4jRepositories(basePackages = "com.neusoft.hifly.neo4j.repository")

// 激活隐式事务

@EnableTransactionManagement

public class Neo4jConfig {

}

      1. 建立NodeEntity

( 有点类似于Mysql中的table 映射的对象类,mysql中叫做ORM,neo4j中叫做OGM [object graph mapping])

  1. 节点的标签实体

package com.neusoft.hifly.neo4j.entity;

import org.springframework.data.neo4j.core.schema.Id;

import org.springframework.data.neo4j.core.schema.Node;

import org.springframework.data.neo4j.core.schema.Property;

/**

 * 部门节点实体类

 */

@Node("Dept")

public class Dept {

    @Id

    private Long id;

    @Property(name = "deptName")

    private String deptName;

    public Dept(Long id, String deptName) {

        super();

        this.id = id;

        this.deptName = deptName;

    }

    public Long getId() {

        return id;

    }

    public void setId(Long id) {

        this.id = id;

    }

    public String getDeptName() {

        return deptName;

    }

    public void setDeptName(String deptName) {

        this.deptName = deptName;

    }

}

  1. 关系实体

package com.neusoft.hifly.neo4j.entity;

import static org.springframework.data.neo4j.core.schema.Relationship.Direction.INCOMING;

import java.util.List;

import org.springframework.data.neo4j.core.schema.Id;

import org.springframework.data.neo4j.core.schema.Node;

import org.springframework.data.neo4j.core.schema.Relationship;

/**

 * 关系 实体类

 */

@Node("Ship")

public class RelationShip {

    @Id

    private Long id;

    @Relationship(type = "PARENT", direction = INCOMING)

    private Dept parent;

    @Relationship(type = "CHILD", direction = INCOMING)

    private List<Dept> child;

    public RelationShip(Long id, Dept parent, List<Dept> child) {

        super();

        this.id = id;

        this.parent = parent;

        this.child = child;

    }

    public Long getId() {

        return id;

    }

    public void setId(Long id) {

        this.id = id;

    }

    public Dept getParent() {

        return parent;

    }

    public void setParent(Dept parent) {

        this.parent = parent;

    }

    public List<Dept> getChild() {

        return child;

    }

    public void setChild(List<Dept> child) {

        this.child = child;

    }

}

      1. 编写Service

package com.neusoft.hifly.neo4j.repository;

import java.util.List;

import org.springframework.data.neo4j.repository.Neo4jRepository;

import org.springframework.data.neo4j.repository.query.Query;

import org.springframework.data.repository.query.Param;

import org.springframework.stereotype.Repository;

import com.neusoft.hifly.neo4j.entity.Dept;

@Repository

public interface DeptRepository extends Neo4jRepository<Dept, Long> {

    @Query("MATCH (d:Dept) WHERE d.deptName CONTAINS $title RETURN d")

    List<Dept> findByTitle(@Param("title") String title);

}

package com.neusoft.hifly.neo4j.repository;

import org.springframework.data.neo4j.repository.Neo4jRepository;

import org.springframework.stereotype.Repository;

import com.neusoft.hifly.neo4j.entity.RelationShip;

@Repository

public interface RelationShipRepository extends Neo4jRepository<RelationShip, Long> {

}

package com.neusoft.hifly.neo4j.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.neusoft.hifly.neo4j.entity.Dept;

import com.neusoft.hifly.neo4j.repository.DeptRepository;

@Service

public class DeptService {

    @Autowired

    private DeptRepository deptRepository;

    public Iterable<Dept> saveAll(List<Dept> depts) {

        return deptRepository.saveAll(depts);

    }

    public void deleteById(Long id) {

        deptRepository.deleteById(id);

    }

    public void deleteAll() {

        deptRepository.deleteAll();

    }

    public List<Dept> findByTitle(String title) {

        return deptRepository.findByTitle(title);

    }

}

package com.neusoft.hifly.neo4j.service;

import java.util.List;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.neusoft.hifly.neo4j.entity.RelationShip;

import com.neusoft.hifly.neo4j.repository.RelationShipRepository;

@Service

public class RelationShipService {

    @Autowired

    private RelationShipRepository relationShipRepository;

    public Iterable<RelationShip> saveAll(List<RelationShip> depts) {

        return relationShipRepository.saveAll(depts);

    }

    public void deleteById(Long id) {

        relationShipRepository.deleteById(id);

    }

    public void deleteAll() {

        relationShipRepository.deleteAll();

    }

    public Optional<RelationShip> findById(Long id) {

        return relationShipRepository.findById(id);

    }

}

  1. 编写Controller

package com.neusoft.hifly.neo4j.api;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.CrossOrigin;

import org.springframework.web.bind.annotation.DeleteMapping;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RestController;

import com.neusoft.hifly.commons.id.IdWorker;

import com.neusoft.hifly.core.pojo.ApiResponse;

import com.neusoft.hifly.neo4j.entity.Dept;

import com.neusoft.hifly.neo4j.entity.RelationShip;

import com.neusoft.hifly.neo4j.service.DeptService;

import com.neusoft.hifly.neo4j.service.RelationShipService;

import io.swagger.v3.oas.annotations.Operation;

import io.swagger.v3.oas.annotations.tags.Tag;

@CrossOrigin

@RestController

@RequestMapping("/api/neo4j")

@Tag(name = "Neo4j例子")

public class TestController {

    @Autowired

    private IdWorker idWorker;

    @Autowired

    private DeptService deptService;

    @Autowired

    private RelationShipService relationShipService;

    /**

     * CEO -设计部 - 设计1 - 设计2 -技术部 - 前端技术部 - 后端技术部 - 测试技术部

     */

    @Operation(summary = "生成数据")

    @PostMapping("")

    public ApiResponse<?> create() {

        Dept CEO = new Dept(idWorker.nextId(), "CEO");

        Dept dept1 = new Dept(idWorker.nextId(), "设计部");

        Dept dept11 = new Dept(idWorker.nextId(), "设计1");

        Dept dept12 = new Dept(idWorker.nextId(), "设计2");

        Dept dept2 = new Dept(idWorker.nextId(), "技术部");

        Dept dept21 = new Dept(idWorker.nextId(), "前端技术部");

        Dept dept22 = new Dept(idWorker.nextId(), "后端技术部");

        Dept dept23 = new Dept(idWorker.nextId(), "测试技术部");

        List<Dept> depts = new ArrayList<>(Arrays.asList(CEO, dept1, dept11, dept12, dept2, dept21, dept22, dept23));

        deptService.saveAll(depts);

        RelationShip relationShip1 = new RelationShip(idWorker.nextId(), CEO,

                new ArrayList<>(Arrays.asList(dept1, dept2)));

        RelationShip relationShip2 = new RelationShip(idWorker.nextId(), dept1,

                new ArrayList<>(Arrays.asList(dept11, dept12)));

        RelationShip relationShip3 = new RelationShip(idWorker.nextId(), dept2,

                new ArrayList<>(Arrays.asList(dept21, dept22, dept23)));

        List<RelationShip> relationShips = new ArrayList<>(Arrays.asList(relationShip1, relationShip2, relationShip3));

        relationShipService.saveAll(relationShips);

        return new ApiResponse<>("成功!");

    }

    @Operation(summary = "获取关系")

    @GetMapping("")

    public ApiResponse<RelationShip> getById(Long id) {

        Optional<RelationShip> byId = relationShipService.findById(id);

        return new ApiResponse<RelationShip>(byId.orElse(null));

    }

    @Operation(summary = "删除关系")

    @DeleteMapping("")

    public ApiResponse<?> deleteRelationShip(Long id) {

        relationShipService.deleteById(id);

        return new ApiResponse<>();

    }

    @Operation(summary = "删除部门")

    @DeleteMapping("/dept")

    public ApiResponse<?> deleteDept(Long id) {

        deptService.deleteById(id);

        return new ApiResponse<>();

    }

    @Operation(summary = "删除所有数据")

    @DeleteMapping("/all")

    public ApiResponse<?> deleteAll() {

        deptService.deleteAll();

        relationShipService.deleteAll();

        return new ApiResponse<>();

    }

    @Operation(summary = "查询")

    @GetMapping("/title")

    public ApiResponse<List<Dept>> getByTitle(@RequestParam("title") String title) {

        return new ApiResponse<List<Dept>>(deptService.findByTitle(title));

    }

}

      1. 运行程序,查看结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寻梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值