前言
系列文章:
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);}}