SpringBoot 集成 Nebula

工作需求,开始了解图数据库,经过工具选型,最终选择nebula graph,并集成到springboot,java 环境下如何对 Nebula Graph 进行操作,本文整理下过程。

1、首先引入 pom 依赖

java 复制代码
<dependency>
    <groupId>com.vesoft</groupId>
    <artifactId>client</artifactId>
    <version>3.0.0</version>
</dependency>

为方便解析 json ,这里把 fastjson 也引进来。

复制代码
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.78</version>
</dependency>

2、配置文件中配置 Nebula 的信息

java 复制代码
nebula:
  address[0]:
    host: 192.168.40.130
    port: 9669
  username: root
  password: root
  reconnect: false
  space: javatest

3、java配置过程,文件结构如下:

1)声明 NebulaProperties 接收上面的配置信息:
复制代码
@Data
public class NebulaAddress {
    private String host;
    private Integer port;
}

    
   
@Data
@Configuration
@ConfigurationProperties(prefix = "nebula")
public class NebulaProperties {
    private List<NebulaAddress> address;
    private String username;
    private String password;
    private boolean reconnect;
    private String space;
}
2)声明 NebulaConstant 把常用的字符串声明在这里:
复制代码
public class NebulaConstant {
    public static final String USE = "USE ";
    public static final String SEMICOLON = "; ";
    public static final String ERROR_CODE = "-1";

    @Getter
    @AllArgsConstructor
    public enum NebulaJson{
        ERRORS("errors"),
        CODE("code"),
        MESSAGE("message"),
        RESULTS("results"),
        COLUMNS("columns"),
        DATA("data"),
        ROW("row");
        private String key;
    }
}
3)声明 NebulaConfig ,初始化 NebulaPool ,及声明 Session 的获取方式:
  • 便对结果的解析,再声明一个 NebulaResult 用来接收结果:

    复制代码
    @Data
    public class NebulaResult<T> {
        private Integer code;
        private String message;
        private List<T> data;
    
        public boolean isSuccessed(){
            return code == 0;
        }
    }
4)为了方便对结果的解析,再声明一个 NebulaResult 用来接收结果:
复制代码
@Data
public class NebulaResult<T> {
    private Integer code;
    private String message;
    private List<T> data;

    public boolean isSuccessed(){
        return code == 0;
    }
}
5)每次都是使用 Session 未免太麻烦,这里封装一个 NebulaTemplate 返回上面 对象 :
复制代码
@Slf4j
@Component
public class NebulaTemplate {

    @Resource
    Session session;

    public <T> NebulaResult<T> queryObject(String stmt, Class<T> tClass) {
        NebulaResult<T> nebulaResult = executeObject(stmt);
        if (Objects.isNull(nebulaResult.getData())) {
            return nebulaResult;
        }
        Optional.ofNullable(nebulaResult.getData()).ifPresent(data -> nebulaResult.setData(data.stream().map(d -> JSONObject.toJavaObject(((JSONObject) d), tClass)).collect(Collectors.toList())));
        return nebulaResult;
    }

    public NebulaResult executeObject(String stmt) {
        JSONObject jsonObject = executeJson(stmt);
        return JSONObject.toJavaObject(jsonObject, NebulaResult.class);
    }

    public JSONObject executeJson(String stmt) {
        JSONObject restJson = new JSONObject();
        try {
            JSONObject jsonObject = JSON.parseObject(Objects.requireNonNull(session).executeJson(stmt));
            JSONObject errors = jsonObject.getJSONArray(NebulaConstant.NebulaJson.ERRORS.getKey()).getJSONObject(0);
            restJson.put(NebulaConstant.NebulaJson.CODE.getKey(), errors.getInteger(NebulaConstant.NebulaJson.CODE.getKey()));
            if (errors.getInteger(NebulaConstant.NebulaJson.CODE.getKey()) != 0) {
                restJson.put(NebulaConstant.NebulaJson.MESSAGE.getKey(), errors.getString(NebulaConstant.NebulaJson.MESSAGE.getKey()));
                return restJson;
            }
            JSONObject results = jsonObject.getJSONArray(NebulaConstant.NebulaJson.RESULTS.getKey()).getJSONObject(0);
            JSONArray columns = results.getJSONArray(NebulaConstant.NebulaJson.COLUMNS.getKey());
            if (Objects.isNull(columns)) {
                return restJson;
            }
            JSONArray data = results.getJSONArray(NebulaConstant.NebulaJson.DATA.getKey());
            if (Objects.isNull(data)) {
                return restJson;
            }
            List<JSONObject> resultList = new ArrayList<>();
            data.stream().map(d -> (JSONObject) d).forEach(d -> {
                JSONArray row = d.getJSONArray(NebulaConstant.NebulaJson.ROW.getKey());
                JSONObject map = new JSONObject();
                for (int i = 0; i < columns.size(); i++) {
                    map.put(columns.getString(i), row.get(i));
                }
                resultList.add(map);
            });
            restJson.put(NebulaConstant.NebulaJson.DATA.getKey(), resultList);
        } catch (Exception e) {
            restJson.put(NebulaConstant.NebulaJson.CODE.getKey(), NebulaConstant.ERROR_CODE);
            restJson.put(NebulaConstant.NebulaJson.MESSAGE.getKey(), e.toString());
            log.error("nebula execute err:", e);
        }
        return restJson;
    }
}

4、测试

复制代码
@RestController
public class TestController {

    @Resource
    NebulaTemplate nebulaTemplate;

    @GetMapping("/addVertex")
    public Object addJSON() throws IOErrorException {
        String sql = "insert vertex team(team_name, persion_num) values \"team_2\":(\"team_2\", 43);";
        NebulaResult nebulaResult = nebulaTemplate.executeObject(sql);
        return nebulaResult;
    }

    @GetMapping("/findVertex")
    public Object findJson2() throws IOErrorException {
        String sql = "lookup on team  yield id(vertex) AS id,properties(vertex).persion_num AS persion_num,properties(vertex).team_name AS team_name;";
        NebulaResult<Info> infoNebulaResult = nebulaTemplate.queryObject(sql, Info.class);
        return infoNebulaResult;
    }
}

5、常用的nSQL语句

sql 复制代码
//查看所有边
LOOKUP ON follow YIELD edge AS e;

//搜索点
LOOKUP ON entity WHERE entity.name == "20元的东西" YIELD properties(vertex).name as name, properties(vertex).cntt as cntt, properties(vertex).sid as sid, properties(vertex).syspath as syspath;

//由一点查看关联的所有点
GO  FROM "20元的东西" OVER follow BIDIRECT YIELD dst(edge) AS destination;

//查全部点
MATCH (v) RETURN v limit 100;

//以下是删除所有的边
LOOKUP ON follow YIELD src(edge) AS src, dst(edge) AS dst 
      |   DELETE EDGE follow $-.src -> $-.dst @0;

//从一点批量删除边
GO FROM "20元的东西" OVER * BIDIRECT 
        YIELD src(edge) AS src, dst(edge) AS dst 
        | DELETE EDGE follow $-.src -> $-.dst @0;

//批量删除点
LOOKUP ON entity YIELD id(vertex)  as id
    | DELETE VERTEX $-.id;

//复合查询
LOOKUP ON entity WHERE entity.name == "20元的东西" YIELD id(vertex) as id | 
  GO  FROM $-.id OVER follow 
  YIELD dst(edge) AS id | 
  GO FROM $-.id OVER follow 
  WHERE properties($$).name == "20元的东西的子节点" 
  YIELD properties($$) as obj;
相关推荐
我真的不会C14 分钟前
QT窗口相关控件及其属性
开发语言·qt
CodeCraft Studio14 分钟前
Excel处理控件Aspose.Cells教程:使用 Python 在 Excel 中进行数据验
开发语言·python·excel
.生产的驴18 分钟前
SpringBoot 封装统一API返回格式对象 标准化开发 请求封装 统一格式处理
java·数据库·spring boot·后端·spring·eclipse·maven
火柴盒zhang20 分钟前
websheet之 编辑器
开发语言·前端·javascript·编辑器·spreadsheet·websheet
猿周LV26 分钟前
JMeter 安装及使用 [软件测试工具]
java·测试工具·jmeter·单元测试·压力测试
景天科技苑27 分钟前
【Rust】Rust中的枚举与模式匹配,原理解析与应用实战
开发语言·后端·rust·match·enum·枚举与模式匹配·rust枚举与模式匹配
晨集28 分钟前
Uni-App 多端电子合同开源项目介绍
java·spring boot·uni-app·电子合同
时间之城30 分钟前
笔记:记一次使用EasyExcel重写convertToExcelData方法无法读取@ExcelDictFormat注解的问题(已解决)
java·spring boot·笔记·spring·excel
阿让啊32 分钟前
C语言中操作字节的某一位
c语言·开发语言·数据结构·单片机·算法
椰羊~王小美37 分钟前
LeetCode -- Flora -- edit 2025-04-25
java·开发语言