SpringBoot 集成 Nebula的操作過(guò)程
工作需求,開始了解圖數(shù)據(jù)庫(kù),經(jīng)過(guò)工具選型,最終選擇nebula graph,并集成到springboot,java 環(huán)境下如何對(duì) Nebula Graph 進(jìn)行操作,本文整理下過(guò)程。
1、首先引入 pom 依賴
<dependency>
<groupId>com.vesoft</groupId>
<artifactId>client</artifactId>
<version>3.0.0</version>
</dependency>為方便解析 json ,這里把 fastjson 也引進(jìn)來(lái)。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>2、配置文件中配置 Nebula 的信息
nebula:
address[0]:
host: 192.168.40.130
port: 9669
username: root
password: root
reconnect: false
space: javatest3、java配置過(guò)程,文件結(jié)構(gòu)如下:

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 的獲取方式:
便對(duì)結(jié)果的解析,再聲明一個(gè) NebulaResult 用來(lái)接收結(jié)果:
@Data
public class NebulaResult<T> {
private Integer code;
private String message;
private List<T> data;
public boolean isSuccessed(){
return code == 0;
}
}
4)為了方便對(duì)結(jié)果的解析,再聲明一個(gè) NebulaResult 用來(lái)接收結(jié)果:
@Data
public class NebulaResult<T> {
private Integer code;
private String message;
private List<T> data;
public boolean isSuccessed(){
return code == 0;
}
}5)每次都是使用 Session 未免太麻煩,這里封裝一個(gè) NebulaTemplate 返回上面 對(duì)象 :
@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、測(cè)試
@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語(yǔ)句
//查看所有邊
LOOKUP ON follow YIELD edge AS e;
//搜索點(diǎn)
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;
//由一點(diǎn)查看關(guān)聯(lián)的所有點(diǎn)
GO FROM "20元的東西" OVER follow BIDIRECT YIELD dst(edge) AS destination;
//查全部點(diǎn)
MATCH (v) RETURN v limit 100;
//以下是刪除所有的邊
LOOKUP ON follow YIELD src(edge) AS src, dst(edge) AS dst
| DELETE EDGE follow $-.src -> $-.dst @0;
//從一點(diǎn)批量刪除邊
GO FROM "20元的東西" OVER * BIDIRECT
YIELD src(edge) AS src, dst(edge) AS dst
| DELETE EDGE follow $-.src -> $-.dst @0;
//批量刪除點(diǎn)
LOOKUP ON entity YIELD id(vertex) as id
| DELETE VERTEX $-.id;
//復(fù)合查詢
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元的東西的子節(jié)點(diǎn)"
YIELD properties($$) as obj;到此這篇關(guān)于SpringBoot 集成 Nebula的文章就介紹到這了,更多相關(guān)SpringBoot 集成 Nebula內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Spring-IOC容器與Bean管理之基于注解的方式案例詳解
這篇文章主要介紹了Java Spring-IOC容器與Bean管理之基于注解的方式案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Java自定義注解實(shí)現(xiàn)Redis自動(dòng)緩存的方法
本篇文章主要介紹了Java自定義注解實(shí)現(xiàn)Redis自動(dòng)緩存的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
Maven忽略單元測(cè)試及打包到Nexus的實(shí)現(xiàn)
我們的工程在打包發(fā)布時(shí)候,通常都需要忽略單元測(cè)試,以免因環(huán)境原因,無(wú)法通過(guò)單元測(cè)試而影響發(fā)布,本文主要介紹了Maven忽略單元測(cè)試及打包到Nexus的實(shí)現(xiàn),感興趣的可以了解一下2024-04-04
Spring boot整合shiro+jwt實(shí)現(xiàn)前后端分離
這篇文章主要為大家詳細(xì)介紹了Spring boot整合shiro+jwt實(shí)現(xiàn)前后端分離,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
Java Cache詳解及簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了 Java Cache詳解及簡(jiǎn)單實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-02-02
java數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)順序表示例
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)順序表示例,需要的朋友可以參考下2014-03-03
一個(gè)簡(jiǎn)單的java學(xué)生寢室查詢系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了一個(gè)簡(jiǎn)單的java學(xué)生寢室查詢系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Java8的Stream()與ParallelStream()的區(qū)別說(shuō)明
這篇文章主要介紹了Java8的Stream()與ParallelStream()的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringBoot可以同時(shí)處理多少請(qǐng)求流程分析
SpringBoot默認(rèn)的內(nèi)嵌容器是Tomcat,也就是我們的程序?qū)嶋H上是運(yùn)行在Tomcat里的,所以與其說(shuō)SpringBoot可以處理多少請(qǐng)求,到不如說(shuō)Tomcat可以處理多少請(qǐng)求,這篇文章主要介紹了SpringBoot可以同時(shí)處理多少請(qǐng)求,需要的朋友可以參考下2023-02-02

