本文主要是介绍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>
- 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;}
- 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");}}@Overridepublic 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);}}@Overridepublic Class<?> getObjectType() {return Session.class;}public Session getSession() {return (Session) getObject();}@Overridepublic void destroy() throws Exception {nebulaPool.close();}}
- SpringBoot自动配置
@EnableConfigurationProperties({ NebulaGraphProperties.class })
@Configuration
public class NebulaGraphAutoConfiguration {@ConditionalOnProperty(name = "nebula-graph.enable", havingValue = "true", matchIfMissing = false)@Beanpublic NebulaGraphFactoryBean nebulaGraphFactoryBean(NebulaGraphProperties nebulaGraphProperties) {return new NebulaGraphFactoryBean(nebulaGraphProperties);}}
- spring.factories文件开启自动配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\com.component.nebula.graph.config.NebulaGraphAutoConfiguration
业务项目
- 引入组件项目
<!--ComponentNebulaGraph依赖包-->
<dependency><groupId>com.component</groupId><artifactId>component-nebula-graph</artifactId><version>1.0.0-SNAPSHOT</version>
</dependency>
- 项目引入配置
nebula-graph:enable: falsecluster-nodes:- 192.168.0.1:9559- 192.168.0.1:9669max-connect-size: 10username: rootpassword: 123456
- 项目引入部分代码
@Slf4j
@Service("nebulaGraphService")
public class NebulaGraphServiceImpl implements NebulaGraphService {private static final String SPACE_QL = "USE %s";@Autowiredprivate 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;}@Overridepublic <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);}@Overridepublic <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;}}
总体来说,跟普通的数据库连接还是很相似的,上手也是比较容易的。
这篇关于NebulaGraph学习笔记-自定义池连接的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!