04-SpringBoot集成Nebula Graph

本文详细介绍了如何在SpringBoot项目中集成Nebula Graph,包括创建独立模块、添加Maven依赖、配置YAML文件、创建配置类、初始化会话与连接池、设计工具类以及使用示例。通过引入SqlBuild和SqlBuildUtils,简化了图数据库的操作,提供了类和字段注解以提高开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot集成Nebula

建议模块

在这里给大家推荐一种方式, 每引入一种新的技术,建议新建一个模块, 来适配这种技术,对外提供接口,在调用的地方应用就可以, 不用搞的到处都是, 防止如果后续替换这种技术, 还要到处修改, 这样的话, 只需要增加一个模块, 对外提供一样的接口, 并替换依赖就可以

添加Maven依赖

<dependency>
  <groupId>com.vesoft</groupId>
  <artifactId>client</artifactId>
  <version>3.0.0</version>
</dependency>
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.83</version>
</dependency>

配合FastJson一起用比较方便, 为了解决fastjson漏洞问题, 使用1.2.83及其以上版本

增加yml配置

nebula:
  address[0]:
    host: 192.168.247.130
    port: 9669
  username: root
  password: 123456
  reconnect: false
  space: knowledge_extraction_platform

address[0]: 因为是开发, 所以配置为单机的, 等部署生产的话, 可以增加address[1-..]来完成集群的配置

space: 图空间

新增配置类

主机和端口

package com.jd.knowledgeextractionplatform.nebulagraph.config;

import lombok.Data;

@Data
public class NebulaAddress {
    private String host;
    private Integer port;
}

解析配置文件的模型

package com.jd.knowledgeextractionplatform.nebulagraph.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.List;

@Data
@Configuration
@ConfigurationProperties(prefix = "nebula")
public class NebulaProperties {
    private List<NebulaAddress> address;
    private String username;
    private String password;
    private boolean reconnect;
    private String space;
}

基于配置初始化会话和连接池

package com.jd.knowledgeextractionplatform.nebulagraph.config;

import com.jd.knowledgeextractionplatform.nebulagraph.constant.NebulaConstant;
import com.sun.javafx.binding.StringFormatter;
import com.vesoft.nebula.client.graph.NebulaPoolConfig;
import com.vesoft.nebula.client.graph.data.HostAddress;
import com.vesoft.nebula.client.graph.net.NebulaPool;
import com.vesoft.nebula.client.graph.net.Session;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;

import java.util.stream.Collectors;

@Slf4j
@Configuration
public class NebulaConfig {
    
    @Bean
    public NebulaPool nebulaPool(NebulaProperties nebulaProperties) throws Exception {
        NebulaPool pool = new NebulaPool();
        NebulaPoolConfig nebulaPoolConfig = new NebulaPoolConfig();
        nebulaPoolConfig.setMaxConnSize(1000);
        boolean init = pool.init(nebulaProperties.getAddress().stream().map(d -> new HostAddress(d.getHost(), d.getPort())).collect(Collectors.toList()), nebulaPoolConfig);
        if (!init){
            throw new RuntimeException("NebulaGraph init err !");
        }else {
            log.info("NebulaGraph init Success !");
        }
        return pool;
    }
    
    @Bean
    @Scope(scopeName = "prototype",proxyMode = ScopedProxyMode.TARGET_CLASS)
    public Session session(NebulaPool nebulaPool, NebulaProperties nebulaProperties) {
        try {
            Session session = nebulaPool.getSession(nebulaProperties.getUsername(), nebulaProperties.getPassword(), nebulaProperties.isReconnect());
            session.execute(StringFormatter.concat(NebulaConstant.USE, nebulaProperties.getSpace(), NebulaConstant.SEMICOLON).getValue());
            return session;
        } catch (Exception e) {
            log.error("get nebula session err , {} ", e.toString());
        }
        return null;
    }
}
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值