Nebula Graph - SpringBoot 操作 Nebula
【摘要】
一、Nebula Graph
前面两篇文章,讲解了下 Nebula Graph 的安装,及 nGQL 的使用,本篇文章讲解下在 java 环境下如何对 Nebula Graph 进行操作,使用 Spri...
一、Nebula Graph
前面两篇文章,讲解了下 Nebula Graph 的安装,及 nGQL 的使用,本篇文章讲解下在 java 环境下如何对 Nebula Graph 进行操作,使用 SpringBoot 环境。
下面是上篇文章的地址:
二、SpringBoot 操作 Nebula Graph
-
首先引入 pom 依赖:
<dependency> <groupId>com.vesoft</groupId> <artifactId>client</artifactId> <version>3.0.0</version> </dependency>
- 1
- 2
- 3
- 4
- 5
为方便解析 json ,这里把 fastjson 也引进来。
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.78</version> </dependency>
- 1
- 2
- 3
- 4
- 5
-
配置文件中配置 Nebula 的信息,后面读取这里的配置信息。
nebula: address[0]: host: 192.168.40.130 port: 9669 username: root password: root reconnect: false space: javatest
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
-
声明 NebulaProperties 接收上面的配置信息:
@Data public class NebulaAddress { private String host; private Integer port; }
- 1
- 2
- 3
- 4
- 5
@Data @Configuration @ConfigurationProperties(prefix = "nebula") public class NebulaProperties { private List<NebulaAddress> address; private String username; private String password; private boolean reconnect; private String space; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
-
声明 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; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
-
声明 NebulaConfig ,初始化 NebulaPool ,及声明 Session 的获取方式:
@Slf4j @Configuration public class NebulaConfig { @Bean public NebulaPool nebulaPool(NebulaProperties properties) throws UnknownHostException { NebulaPool pool = new NebulaPool(); NebulaPoolConfig nebulaPoolConfig = new NebulaPoolConfig(); nebulaPoolConfig.setMaxConnSize(1000); boolean init = pool.init(properties.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 properties) { try { Session session = nebulaPool.getSession(properties.getUsername(), properties.getPassword(), properties.isReconnect()); session.execute(StringFormatter.concat(NebulaConstant.USE, properties.getSpace(), NebulaConstant.SEMICOLON).getValue()); return session; } catch (Exception e) { log.error("get nebula session err , {} ", e.toString()); } return null; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
-
为了方便对结果的解析,再声明一个 NebulaResult 用来接收结果:
@Data public class NebulaResult<T> { private Integer code; private String message; private List<T> data; public boolean isSuccessed(){ return code == 0; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
-
每次都是使用 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; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
-
测试
@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; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
文章来源: blog.csdn.net,作者:小毕超,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_43692950/article/details/124599111
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)