Java 操作 Neo4J 就是这么简单!

开发 后端
前几天阿粉给大家扩展了关于 Neo4J 图谱数据库的内容,今天阿粉教给大家如何使用 Java 来操作 Neo4j 数据库。

[[442141]]

本文转载自微信公众号「Java极客技术」,作者鸭血粉丝Tang。转载本文请联系Java极客技术公众号。

前几天阿粉给大家扩展了关于 Neo4J 图谱数据库的内容,今天阿粉教给大家如何使用 Java 来操作 Neo4j 数据库。

使用 Java 操作 Neo4J

首先我们先使用原生的这种方式,导入 jar 包,然后:

public class TestController { 
    public static void main(String[] args) { 
        Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j""Yinlidong1995.")); 
        Session session = driver.session(); 
        session.run("CREATE (n:Part {name: {name},title: {title}})", 
                parameters( "name""Arthur001""title""King001" )); 
        StatementResult result = session.run( "MATCH (a:Part) WHERE a.name = {name} " + 
                        "RETURN a.name AS name, a.title AS title", 
                parameters( "name""Arthur001")); 
        while (result.hasNext()) { 
            Record record = result.next(); 
            System.out.println( record.get( "title" ).asString() + "" + record.get( "name" ).asString() ); 
        } 
        session.close(); 
        driver.close(); 
    } 
} 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

这是一种比较古老的方式,来实现的,而且还是需要些 CQL 语句来进行实现。但是胜在非常好理解,这个时候,我们需要再来看看图,看看在 Neo4J 中他是怎么展现的。

通过这个,我们至少证明我们成功连上了,并且创建也成功了。

这时候有细心的读者就会问,为啥我之前在 GraphDatabase.driver 的地方,连接的是

bolt://localhost:7687.

这是因为,你启动的Neo4J 是7474,也就是说,Neo4J 服务里面可不是这个来连接,

SpringBoot 整合 Neo4j

1.创建SpringBoot项目

常规的创建SpringBoot项目,

创建完成之后,习惯性的要改一下 SpringBoot 的版本号,最好别用最新的,因为阿粉亲身经历,使用最新版的,出现了错误你都不知道怎么出现的,就是这么神奇,你永远都发现不了的bug。

我们把版本号改成2.1.0,这样的话,我们在 pom 文件中加入依赖 jar

<dependency> 
 <groupId>org.springframework.boot</groupId> 
 <artifactId>spring-boot-starter-data-neo4j</artifactId> 
</dependency> 
<dependency> 
 <groupId>org.projectlombok</groupId> 
 <artifactId>lombok</artifactId> 
 <version>1.16.10</version> 
</dependency> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

 

2.增加配置

spring: 
  data: 
    neo4j: 
      url: bolt://localhost:7687 
      username: neo4j 
      password: Yinlidong1995. 
  main: 
    allow-bean-definition-overriding: true 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

3.Neo4JConfig

package com.example.neo4j.config; 
 
import org.neo4j.driver.v1.AuthTokens; 
import org.neo4j.driver.v1.Driver; 
import org.neo4j.driver.v1.GraphDatabase; 
import org.neo4j.driver.v1.Session; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 
 
@Configuration 
@EnableNeo4jRepositories("com.example.neo4j.repository") 
@EnableTransactionManagement 
public class Neo4jConfig { 
    @Value("${spring.data.neo4j.url}") 
    private String url; 
    @Value("${spring.data.neo4j.username}") 
    private String userName; 
    @Value("${spring.data.neo4j.password}") 
    private String password; 
 
    @Bean(name = "session") 
    public Session neo4jSession() { 
        Driver driver = GraphDatabase.driver(url, AuthTokens.basic(userName, password)); 
        return driver.session(); 
    } 
} 
  • 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.

4.编写实体类

package com.example.neo4j.entry; 
 
import org.neo4j.ogm.annotation.*; 
 
import java.util.HashSet; 
import java.util.Set; 
@NodeEntity("group") 
@Data 
public class GroupNode { 
    @Id 
    @GeneratedValue 
    private Long id; 
 
    /** 
     * 班级名称 
     */ 
    @Property(name = "name") 
    private String name; 
 
    /** 
     * 编号 
     */ 
    private String num; 
 
    @Relationship(type = "RelationEdge") 
    private Set<RelationEdge> sets = new HashSet<>(); 
 
    public void addRelation(StudentNode sonNode, String name) { 
        RelationEdge relationNode = new RelationEdge(this, sonNode, name); 
        sets.add(relationNode); 
        sonNode.getSets().add(relationNode); 
    } 
} 
  • 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.

学生实体类:

package com.example.neo4j.entry; 
import org.neo4j.ogm.annotation.GeneratedValue; 
import org.neo4j.ogm.annotation.Id; 
import org.neo4j.ogm.annotation.NodeEntity; 
import org.neo4j.ogm.annotation.Relationship; 
 
import java.util.HashSet; 
import java.util.Set; 
 
/** 
 * 有点类似于Mysql中的table 映射的对象类,mysql中叫做ORM,neo4j中叫做OGM [object graph mapping] 
 */ 
@NodeEntity("student") 
@Data 
public class StudentNode { 
    @Id 
    @GeneratedValue 
    private Long id; 
 
    /** 
     * 学生名称 
     */ 
    private String name; 
 
    /** 
     * 性别 
     */ 
    private String sex; 
 
    @Relationship(type = "RelationEdge", direction = "INCOMING") 
    private Set<RelationEdge> sets = new HashSet<>(); 
    
} 
  • 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.
package com.example.neo4j.entry; 
import lombok.Data; 
import org.neo4j.ogm.annotation.*; 
 
@RelationshipEntity(type = "RelationEdge") 
@Data 
public class RelationEdge { 
    @Id 
    @GeneratedValue 
    private Long id; 
 
    // 关系名 
    private String name; 
 
    @StartNode 
    private GroupNode groupNode; 
 
    @EndNode 
    private StudentNode studentNode; 
 
    public RelationEdge(GroupNode parentNode, StudentNode sonNode, String name) { 
        this.groupNode = parentNode; 
        this.studentNode = sonNode; 
        this.name = name; 
    } 
} 
  • 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.

5.Repository接口

对应的学生接口:

package com.example.neo4j.repository; 
 
import com.example.neo4j.entry.StudentNode; 
import org.springframework.data.neo4j.repository.Neo4jRepository; 
 
public interface StudentRepository extends Neo4jRepository<StudentNode,Long> { 
} 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

对应的班级接口

package com.example.neo4j.repository; 
 
import com.example.neo4j.entry.GroupNode; 
import org.springframework.data.neo4j.repository.Neo4jRepository; 
 
public interface GroupRepository extends Neo4jRepository<GroupNode,Long> { 
} 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

最后完成编写我们的 Controller

package com.example.neo4j.controller; 
 
import com.example.neo4j.entry.*; 
import com.example.neo4j.repository.GroupRepository; 
import com.example.neo4j.repository.StudentRepository; 
import lombok.extern.slf4j.Slf4j; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 
 
 
@RestController 
@RequestMapping("/node") 
@Slf4j 
public class GroupController { 
 
    @Autowired 
    private StudentRepository studentRepository; 
    @Autowired 
    private GroupRepository groupRepository; 
 
    @GetMapping(value = "/create") 
    public void createNodeRelation() { 
        StudentNode studentNode1 = new StudentNode(); 
        studentNode1.setName("Alen"); 
        studentNode1.setSex("男"); 
        StudentNode studentNode2 = new StudentNode(); 
        studentNode2.setName("Kai"); 
        studentNode2.setSex("女"); 
        studentRepository.save(studentNode1); 
        studentRepository.save(studentNode2); 
 
        GroupNode groupNode = new GroupNode(); 
        groupNode.setName("火箭班"); 
        groupNode.setNum("298"); 
        // 增加关系 
        groupNode.addRelation(studentNode1, "includes"); 
        groupNode.addRelation(studentNode2, "includes"); 
        groupRepository.save(groupNode); 
    } 
} 
  • 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.

启动之后,访问http://localhost:8080/node/create

我们再去图谱数据库看看。

怎么样,使用Java 操作是不是也是非常简单的呢?这样的图谱数据库你会选择么?

 

责任编辑:武晓燕 来源: Java极客技术
相关推荐

2022-11-18 17:53:03

Neo4j

2011-07-26 12:48:52

neo4j图数据库

2017-07-28 15:12:28

Neo4j图数据库

2022-04-13 11:32:45

Neo4j图数据库

2021-12-03 20:33:08

计算

2018-05-16 08:26:39

知识图谱Neo4j

2024-08-08 08:31:32

SpringNeo4j优化

2024-06-03 10:53:18

LLMRAGGraphRAG

2011-09-22 16:46:02

Neo4j图形数据库数据库

2022-01-17 17:10:18

Neo4j 图数据库

2022-01-17 14:34:59

数据平台数据数字化

2018-05-03 15:40:33

2017-11-28 15:29:04

iPhone X网页适配

2021-05-24 10:50:10

Git命令Linux

2015-09-28 08:57:06

Ruby APPNeo4j

2017-04-24 20:30:47

数据库工具导入数据

2021-10-27 17:20:23

图数据数据库

2020-06-16 10:57:20

搭建

2024-08-28 08:42:21

API接口限流

2022-02-15 08:22:28

Neo4jSpring数据库
点赞
收藏

51CTO技术栈公众号