Nebula Graph-06-NebulaGraph Java 使用 和SpringBoot集成Nebula Graph

2024-03-26 01:20

本文主要是介绍Nebula Graph-06-NebulaGraph Java 使用 和SpringBoot集成Nebula Graph,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

系列文章:
Nebula Graph-01-Nebula Graph简介和安装以及客户端连接
Nebula Graph-02-NebulaGraph高阶配置、用户管理、日志
Nebula Graph-03-NebulaGraph Studio-可视化web工具安装和使用
Nebula Graph-04-NebulaGraph nGQL的介绍和使用
Nebula Graph-05-NebulaGraph nGQL和SQL区别
Nebula Graph-06-NebulaGraph Java 使用 和SpringBoot集成Nebula Graph


Nebula Graph Java

NebulaGraph Java官网地址:https://docs.nebula-graph.com.cn/3.6.0/14.client/1.nebula-client/
官网说明及版本对比:https://github.com/vesoft-inc/nebula-java/tree/release-3.6

导包

如果使用 Maven 管理您的项目,请将以下依赖项添加到您的文件中。 替换为适当的 Nebula Java 版本。

<dependency><groupId>com.vesoft</groupId><artifactId>client</artifactId><version>3.0-SNAPSHOT</version>
</dependency>

客户端版本和Nebula Graph 版本对应
在这里插入图片描述

连接

Java调用主要是三部分, 创建连接池, 创建会话, 执行查询

创建 NebulaPool 连接池

NebulaPool pool = new NebulaPool();
try {NebulaPoolConfig nebulaPoolConfig = new NebulaPoolConfig();nebulaPoolConfig.setMaxConnSize(100);List<HostAddress> addresses = Arrays.asList(new HostAddress("127.0.0.1", 9669));Boolean initResult = pool.init(addresses, nebulaPoolConfig);if (!initResult) {log.error("pool init failed.");return;}
} catch ()
//...

创建 Session 会话

Session session = pool.getSession("root", "nebula", false);

执行查询

创建一个SPACE, 然后使用这个SPACE, 创建一个TAG person, 创建一个EDGE like

String createSchema = "CREATE SPACE IF NOT EXISTS test(vid_type=fixed_string(20)); "+ "USE test;"+ "CREATE TAG IF NOT EXISTS person(name string, age int);"+ "CREATE EDGE IF NOT EXISTS like(likeness double)";
ResultSet resp = session.execute(createSchema);
if (!resp.isSucceeded()) {log.error(String.format("Execute: `%s', failed: %s",createSchema, resp.getErrorMessage()));System.exit(1);
}

简单使用-helloWorld

package com.example.demo;import com.vesoft.nebula.client.graph.NebulaPoolConfig;
import com.vesoft.nebula.client.graph.data.HostAddress;
import com.vesoft.nebula.client.graph.data.ResultSet;
import com.vesoft.nebula.client.graph.net.NebulaPool;
import com.vesoft.nebula.client.graph.net.Session;import java.util.Arrays;
import java.util.List;/*** @author wangkanglu* @version 1.0* @description* @date 2024-03-25 14:10*/
public class TestNebulaGraph {public static void main(String[] args) {try {NebulaPoolConfig nebulaPoolConfig=new NebulaPoolConfig();nebulaPoolConfig.setMaxConnSize(10);List<HostAddress> addresses= Arrays.asList(new HostAddress("192.168.217.129",9669));NebulaPool pool=new NebulaPool();Boolean initResult = pool.init(addresses,nebulaPoolConfig);//创建 Session 会话,创建会话时需要用户名和密码//参数:账号/密码/是否断开后重试Session session=pool.getSession("root","nebula",false);ResultSet resultSet = session.execute("SHOW HOSTS;");//通知服务器不再需要该会话,并将连接返回到池,该连接将被重用。如果用户不再使用会话,则调用此函数。session.release();pool.close();} catch (Exception e) {e.printStackTrace();}}

NebulaGraph Java Client API

地址:https://vesoft-inc.github.io/nebula-java/release-3.6/annotated.html

在 SpringBoot 项目中使用 Nebula Graph

导包

<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及其以上版本

配置文件

nebula:address[0]:host: 192.168.247.130port: 9669username: rootpassword: 123456reconnect: falsespace: my_space
  • address[0]: 因为是开发, 所以配置为单机的, 等部署生产的话, 可以增加address[1-…]来完成集群的配置
  • space: 图空间

配置连接

1:声明 NebulaProperties 接收上面的配置信息:

package com.example.demo.nebula;import lombok.Data;@Data
public class NebulaAddress {private String host;private Integer port;
}
package com.example.demo.nebula;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;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;
}

2:声明 NebulaConfig ,初始化 NebulaPool ,及声明 Session 的获取方式:

package com.example.demo.nebula;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;/*** @author wangkanglu* @version 1.0* @description* @date 2024-03-25 15:35*/
@Slf4j
@Configuration
public class NebulaConfig {@Beanpublic 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;}}

定义Nebula 常量和返回结果

1:定义常量

package com.example.demo.nebula;import lombok.AllArgsConstructor;
import lombok.Getter;/*** @author wangkanglu* @version 1.0* @description* @date 2024-03-25 15:36*/
public class NebulaConstant {public static final String USE = "USE ";public static final String SEMICOLON = "; ";public static final String ERROR_CODE = "-1";@Getter@AllArgsConstructorpublic enum NebulaJson{ERRORS("errors"),CODE("code"),MESSAGE("message"),RESULTS("results"),COLUMNS("columns"),DATA("data"),ROW("row");private String key;}
}

2:为了方便对结果的解析,再声明一个 NebulaResult 用来接收结果

package com.example.demo.nebula;import lombok.Data;import java.util.List;/*** @author wangkanglu* @version 1.0* @description* @date 2024-03-25 15:37*/
@Data
public class NebulaResult<T>{private Integer code;private String message;private List<T> data;public boolean isSuccessed(){return code == 0;}
}

封装一个 NebulaTemplate

package com.example.demo.nebula;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.vesoft.nebula.client.graph.net.Session;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;/*** @author wangkanglu* @version 1.0* @description* @date 2024-03-25 15:37*/
@Slf4j
@Component
public class NebulaTemplate {@ResourceSession 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;}}

测试

 package com.example.demo;import com.example.demo.nebula.NebulaResult;
import com.example.demo.nebula.NebulaTemplate;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class DemoApplicationTests {@AutowiredNebulaTemplate nebulaTemplate;@Testpublic void test1(){String sql = "SHOW HOSTS;";NebulaResult nebulaResult = nebulaTemplate.executeObject(sql);System.out.println(nebulaResult);}}

在这里插入图片描述

这篇关于Nebula Graph-06-NebulaGraph Java 使用 和SpringBoot集成Nebula Graph的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/846871

相关文章

SpringBoot集成图片验证码框架easy-captcha的详细过程

《SpringBoot集成图片验证码框架easy-captcha的详细过程》本文介绍了如何将Easy-Captcha框架集成到SpringBoot项目中,实现图片验证码功能,Easy-Captcha是... 目录SpringBoot集成图片验证码框架easy-captcha一、引言二、依赖三、代码1. Ea

Springboot控制反转与Bean对象的方法

《Springboot控制反转与Bean对象的方法》文章介绍了SpringBoot中的控制反转(IoC)概念,描述了IoC容器如何管理Bean的生命周期和依赖关系,它详细讲解了Bean的注册过程,包括... 目录1 控制反转1.1 什么是控制反转1.2 SpringBoot中的控制反转2 Ioc容器对Bea

如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件

《如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件》本文介绍了如何使用Docker部署FTP服务器和Nginx,并通过HTTP访问FTP中的文件,通过将FTP数据目录挂载到N... 目录docker部署FTP和Nginx并通过HTTP访问FTP里的文件1. 部署 FTP 服务器 (

MySQL 日期时间格式化函数 DATE_FORMAT() 的使用示例详解

《MySQL日期时间格式化函数DATE_FORMAT()的使用示例详解》`DATE_FORMAT()`是MySQL中用于格式化日期时间的函数,本文详细介绍了其语法、格式化字符串的含义以及常见日期... 目录一、DATE_FORMAT()语法二、格式化字符串详解三、常见日期时间格式组合四、业务场景五、总结一、

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

Spring Cloud Hystrix原理与注意事项小结

《SpringCloudHystrix原理与注意事项小结》本文介绍了Hystrix的基本概念、工作原理以及其在实际开发中的应用方式,通过对Hystrix的深入学习,开发者可以在分布式系统中实现精细... 目录一、Spring Cloud Hystrix概述和设计目标(一)Spring Cloud Hystr

Python中配置文件的全面解析与使用

《Python中配置文件的全面解析与使用》在Python开发中,配置文件扮演着举足轻重的角色,它们允许开发者在不修改代码的情况下调整应用程序的行为,下面我们就来看看常见Python配置文件格式的使用吧... 目录一、INI配置文件二、YAML配置文件三、jsON配置文件四、TOML配置文件五、XML配置文件

Go使用pprof进行CPU,内存和阻塞情况分析

《Go使用pprof进行CPU,内存和阻塞情况分析》Go语言提供了强大的pprof工具,用于分析CPU、内存、Goroutine阻塞等性能问题,帮助开发者优化程序,提高运行效率,下面我们就来深入了解下... 目录1. pprof 介绍2. 快速上手:启用 pprof3. CPU Profiling:分析 C

MySQL InnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据

《MySQLInnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据》mysql的ibdata文件被误删、被恶意修改,没有从库和备份数据的情况下的数据恢复,不能保证数据库所有表数据... 参考:mysql Innodb表空间卸载、迁移、装载的使用方法注意!此方法只适用于innodb_fi

Spring Boot整合消息队列RabbitMQ的实现示例

《SpringBoot整合消息队列RabbitMQ的实现示例》本文主要介绍了SpringBoot整合消息队列RabbitMQ的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录RabbitMQ 简介与安装1. RabbitMQ 简介2. RabbitMQ 安装Spring