• NebulaGraph学习笔记-自定义池连接


    最近项目需要连接NebulaGraph图数据库获取部分数据,于是查看了一些相关资料,发现可以通过类似数据库连接池NebulaPool方式连接。主要也是以下几个部分:创建连接池,、创建会话、执行查询、解析结果。下面是一个简单的DEMO记录。
    组件项目
    • 相关依赖包
    <!-- SpringBoot依赖包 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
    
    <!-- Client依赖包 -->
    <dependency>
        <groupId>com.vesoft</groupId>
        <artifactId>client</artifactId>
        <version>3.6.1</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • NebulaGraph连接属性类
    @Data
    @ConfigurationProperties(prefix = "nebula-graph")
    public class NebulaGraphProperties {
    
    	/** 是否开启 **/
    	private Boolean enable = false;
    
    	/** 集群节点 */
    	private String[] clusterNodes = null;
    
    	/** Max Connect Size */
    	private int maxConnectSize = 10;
    
    	/** 用户名 */
    	private String username;
    
    	/** 密码 */
    	private String password;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • NebulaGraph连接池类
    public class NebulaGraphFactoryBean implements FactoryBean, DisposableBean {
    
        private NebulaGraphProperties nebulaGraphProperties;
    
        private NebulaPool nebulaPool;
    
        public NebulaGraphFactoryBean(NebulaGraphProperties nebulaGraphProperties) {
            this.nebulaGraphProperties = nebulaGraphProperties;
            String[] clusterNodes = nebulaGraphProperties.getClusterNodes();
            if (null == clusterNodes || clusterNodes.length == 0) {
                return;
            }
            List<HostAddress> hostAddresses = new ArrayList<>();
            for (int i = 0, len = clusterNodes.length; i < len; i++) {
                String clusterNode = clusterNodes[i];
                if (!clusterNode.contains(":")) {
                    continue;
                }
                String[] ipAndPort = clusterNode.split(":");
                if (ipAndPort.length != 2 || !ipAndPort[1].matches("\\d+")) {
                    throw new RuntimeException("Invalid Nebula Graph Node " + clusterNode);
                }
                hostAddresses.add(new HostAddress(ipAndPort[0], Integer.parseInt(ipAndPort[1])));
            }
            NebulaPoolConfig nebulaPoolConfig = new NebulaPoolConfig();
            nebulaPoolConfig.setMaxConnSize(nebulaGraphProperties.getMaxConnectSize());
            nebulaPool = new NebulaPool();
            try {
                nebulaPool.init(hostAddresses, nebulaPoolConfig);
            } catch (UnknownHostException e) {
                throw new RuntimeException("Unknown Nebula Graph Host");
            }
        }
    
        @Override
        public Object getObject() {
            try {
                return nebulaPool.getSession(nebulaGraphProperties.getUsername(), nebulaGraphProperties.getPassword(), false);
            } catch (NotValidConnectionException | IOErrorException | AuthFailedException | ClientServerIncompatibleException e) {
                throw new RuntimeException("Nebula graph session exception", e);
            }
        }
    
        @Override
        public Class<?> getObjectType() {
            return Session.class;
        }
    
        public Session getSession() {
            return (Session) getObject();
        }
    
        @Override
        public void destroy() throws Exception {
            nebulaPool.close();
        }
    
    }
    
    • 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
    • SpringBoot自动配置
    @EnableConfigurationProperties({ NebulaGraphProperties.class })
    @Configuration
    public class NebulaGraphAutoConfiguration {
    
        @ConditionalOnProperty(name = "nebula-graph.enable", havingValue = "true", matchIfMissing = false)
        @Bean
        public NebulaGraphFactoryBean nebulaGraphFactoryBean(NebulaGraphProperties nebulaGraphProperties) {
            return new NebulaGraphFactoryBean(nebulaGraphProperties);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • spring.factories文件开启自动配置
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
       com.component.nebula.graph.config.NebulaGraphAutoConfiguration
    
    • 1
    • 2
    业务项目
    • 引入组件项目
    <!--ComponentNebulaGraph依赖包-->
    <dependency>
        <groupId>com.component</groupId>
        <artifactId>component-nebula-graph</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 项目引入配置
    nebula-graph:
      enable: false
      cluster-nodes:
        - 192.168.0.1:9559
        - 192.168.0.1:9669
      max-connect-size: 10
      username: root
      password: 123456
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 项目引入部分代码
    @Slf4j
    @Service("nebulaGraphService")
    public class NebulaGraphServiceImpl implements NebulaGraphService {
    
        private static final String SPACE_QL = "USE %s";
    
        @Autowired
        private NebulaGraphFactoryBean nebulaGraphFactoryBean;
    
        public NGResultV1DTO execute(String space, String ngql, Map<String, Object> parameterMap) throws IOErrorException {
            Session session = nebulaGraphFactoryBean.getSession();
            NGResultV1DTO ngResultV1DTO = JsonUtils.json(session.executeJson(String.format(SPACE_QL, space)), NGResultV1DTO.class);
            if (!ngResultV1DTO.isSuccess()) {
                return ngResultV1DTO;
            }
            String result = null == parameterMap ? session.executeJson(ngql) : session.executeJsonWithParameter(ngql, parameterMap);
            log.info("execute result {}", result);
            ngResultV1DTO = JsonUtils.json(result, NGResultV1DTO.class);
            return ngResultV1DTO;
        }
    
        @Override
        public <T> ResultDTO<T> executeOne(String space, String ngql, Map<String, Object> parameterMap, Class<T> clazz) throws IOErrorException {
            return buildResultDTO(execute(space, ngql, parameterMap), clazz, true);
        }
    
        @Override
        public <T> ResultDTO<List<T>> execute(String space, String ngql, Map<String, Object> parameterMap, Class<T> clazz) throws IOErrorException {
            return buildResultDTO(execute(space, ngql, parameterMap), clazz, false);
        }
    
        private <T> ResultDTO buildResultDTO(NGResultV1DTO ngResultV1DTO, Class<T> clazz, boolean isSingleResult) throws IOErrorException {
            if (!ngResultV1DTO.isSuccess()) {
                NGResultV1DTO.Error error = ngResultV1DTO.getErrors().get(0);
                return ResultDTO.fail(error.getCode(), error.getMessage());
            }
            List<T> resultList = parse(ngResultV1DTO, clazz);
            return ResultDTO.success(!ObjectUtil.isEmpty(resultList) && isSingleResult ? resultList.get(0) : resultList);
        }
    
        private <T> List<T> parse(NGResultV1DTO ngResultV1DTO, Class<T> clazz) {
            List<NGResultV1DTO.Result> results = ngResultV1DTO.getResults();
            if (null == results || results.isEmpty()) {
                return null;
            }
            NGResultV1DTO.Result result = results.get(0);
            List<NGResultV1DTO.Data> datas = result.getDatas();
            if (null == datas || datas.isEmpty()) {
                return null;
            }
            boolean needColumns = false;
            List<String> columns = result.getColumns();
            List<T> targetList = new ArrayList<>();
            for (int i = 0, len = datas.size(); i < len; i++) {
                NGResultV1DTO.Data data = datas.get(i);
                List<?> rows = data.getRows();
                if (null == rows || rows.isEmpty()) {
                    continue;
                }
                if (i == 0) {
                    List<?> metas = data.getMetas();
                    if (null == metas || null == metas.get(0)) {
                        needColumns = true;
                    }
                }
                Object row = rows.get(0);
                Map<String, Object> dataMap = new HashMap<>();
                if (needColumns) {
                    Object[] rowArray = (Object[]) row;
                    for (int j = 0, jLen = rowArray.length; j < jLen; j++) {
                        dataMap.put(columns.get(j), rowArray[j]);
                    }
                } else {
                    ((Map<String, Object>) row).forEach((key, value) -> {
                        if (key.contains(".")) {
                            String[] keyArray = key.split(".");
                            dataMap.put(keyArray[keyArray.length - 1], value);
                        } else {
                            dataMap.put(key, value);
                        }
                    });
                }
                targetList.add(ReflectUtils.convertMapToObject(dataMap, clazz));
            }
            return targetList;
        }
    
    }
    
    • 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
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    总体来说,跟普通的数据库连接还是很相似的,上手也是比较容易的。
  • 相关阅读:
    【80天学习完《深入理解计算机系统》】第十四天 复习第三章
    51单片机多机通信
    原型模式(Prototype Pattern)
    专利转让的具体流程和步骤
    java-php-net-python-基于的相册软件的设计与实现计算机毕业设计程序
    第二篇 如何设计一个RBAC权限系统
    三 android tinyalsa 理解
    【go】defer的使用
    MySQL存储过程入门了解
    Webpack5基础笔记一
  • 原文地址:https://blog.csdn.net/wulinshishen/article/details/136071383