欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot 集成 Nebula的操作過程

 更新時間:2024年05月25日 14:19:16   作者:一個高效工作的家伙  
這篇文章主要介紹了SpringBoot 集成 Nebula的操作過程,通過示例代碼介紹了java 環(huán)境下如何對 Nebula Graph 進行操作,感興趣的朋友跟隨小編一起看看吧

工作需求,開始了解圖數(shù)據(jù)庫,經(jīng)過工具選型,最終選擇nebula graph,并集成到springboot,java 環(huán)境下如何對 Nebula Graph 進行操作,本文整理下過程。

1、首先引入 pom 依賴

<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 的信息

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語句

//查看所有邊
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;
//由一點查看關聯(lián)的所有點
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元的東西的子節(jié)點" 
  YIELD properties($$) as obj;

到此這篇關于SpringBoot 集成 Nebula的文章就介紹到這了,更多相關SpringBoot 集成 Nebula內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • JavaSe和JavaEE的區(qū)別原理面試題精講

    JavaSe和JavaEE的區(qū)別原理面試題精講

    這篇文章主要為大家介紹了JavaSe和JavaEE的區(qū)別原理面試題精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • 分布式Netty源碼分析EventLoopGroup及介紹

    分布式Netty源碼分析EventLoopGroup及介紹

    這篇文章主要介紹了分布式Netty源碼分析EventLoopGroup及介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-03-03
  • Java子類調(diào)用父類的私有屬性的實現(xiàn)示例

    Java子類調(diào)用父類的私有屬性的實現(xiàn)示例

    在Java中,我們都知道子類不能直接訪問或修改父類的私有屬性,本文主要介紹了Java子類調(diào)用父類的私有屬性的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下
    2024-05-05
  • 很詳細的Log4j配置步驟

    很詳細的Log4j配置步驟

    Log4J的配置文件(Configuration File)就是用來設置記錄器的級別、存放器和布局的,它可接key=value格式的設置或xml格式的設置信息。通過配置,可以創(chuàng)建出Log4J的運行環(huán)境。
    2008-11-11
  • Java批量操作文件系統(tǒng)的實現(xiàn)示例

    Java批量操作文件系統(tǒng)的實現(xiàn)示例

    文件上傳和下載是java web中常見的操作,本文主要介紹了Java批量操作文件系統(tǒng)的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-03-03
  • Java對象流實例代碼

    Java對象流實例代碼

    這篇文章主要介紹了Java對象流實例代碼,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • SpringBoot中使用Swagger的最全方法詳解

    SpringBoot中使用Swagger的最全方法詳解

    Swagger是一個規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化Restful風格的Web服務,這篇文章主要給大家介紹了關于SpringBoot中使用Swagger的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-12-12
  • Java反射通過Getter方法獲取對象VO的屬性值過程解析

    Java反射通過Getter方法獲取對象VO的屬性值過程解析

    這篇文章主要介紹了Java反射通過Getter方法獲取對象VO的屬性值過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • Springboot?中的?Filter?實現(xiàn)超大響應?JSON?數(shù)據(jù)壓縮的方法

    Springboot?中的?Filter?實現(xiàn)超大響應?JSON?數(shù)據(jù)壓縮的方法

    這篇文章主要介紹了Springboot?中的?Filter?實現(xiàn)超大響應?JSON?數(shù)據(jù)壓縮,定義GzipFilter對輸出進行攔截,定義 Controller該 Controller 非常簡單,主要讀取一個大文本文件,作為輸出的內(nèi)容,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2022-10-10
  • 詳解Intellij IDEA 2017 debug斷點調(diào)試技巧(總結)

    詳解Intellij IDEA 2017 debug斷點調(diào)試技巧(總結)

    這篇文章主要介紹了詳解Intellij IDEA 2017 debug斷點調(diào)試技巧(總結),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11

最新評論