Hugegraph环境配置及查询接口
一、整体介绍
hugegraph基于Linux环境,本文采用的是centos7的系统;主要运用了hugegraph当中的三个组件:server,hubble,loader;查询接口采用java语言编写,基于springboot框架,查询语句的语法是gremlin。
二、环境配置
首先要注意的是hugegraph只能基于Linux环境下配置,本文采用的是Centos7的系统,但对于不同的系统本文都适用。
1. 下载
1.1 下载地址
需要各组件都可以在hugegraph的官网HugeGraph (apache.org)下载。
1.2 所需的组件
- HugeGraph-Server
- HugeGraph-Hubble
- HugeGraph-Loader
1.3 下载方式
本文采用的方式是直接在官网上下载安装包并解压。
2. 各组件介绍
2.1 HugeGraph-Server
2.1.1概述:HugeGraph-Server 是 HugeGraph 项目的核心部分,包含 Core、Backend、API 等子模块。
2.1.2主要配置:(配置主要在conf文件中)
HugeGraphServer 内部集成了 GremlinServer 和 RestServer这两个server
-
gremlin-server.yaml:用于配置GremlinServer
其中默认 GremlinServer 是服务在 localhost:8182,如果需要修改,配置 host、port 即可。
-
rest-server.properties:用于配置RestServer
主要介绍restserver.url和graphs两个属性。
# bind url #RestServer提供服务的url,根据实际环境修改 restserver.url=http://127.0.0.1:8080 # gremlin server url, need to be consistent with host and port in gremlin-server.yaml #gremlinserver.url=http://127.0.0.1:8182 #RestServer启动时也需要打开图,该项为map结构,key是图的名字,value是该图的配置文件路径 graphs=./conf/graphs #...
-
hugegraph.properties:该文件用来配置与图存储和查询相关的参数。
store:图存储到后端使用的数据库名
2.1.3启动
- 首次启动:进入hugegraph-0.12.0目录下的bin目录启动init-store.sh。根据 gremlin-server.yaml 的 graphs 下的图进行初始化。
./init-store.sh
- 启动server:进入hugegraph-0.12.0目录下的bin目录启动start-hugegraph.sh。
./start-hugegraph.sh
- 启动后输出信息
你可以通过访问你配置的server地址来进一步确认是否成功启动server。访问后会出现以下界面,其中hugegraph对应的就是你的图名称。
2.2 HugeGraph-Hubble
2.2.1概述:是 HugeGraph 的一站式可视化分析平台。
2.2.2配置:主要配置文件是hugegraph-hubble.properties
如果需要外部访问HugeGraph-Hubble,请修改server.host配置项 (默认为127.0.0.1),修改成机器名或IP地址,或者直接配置为0.0.0.0,如下所示。
server.host=0.0.0.0
server.port=8088
gremlin.suffix_limit=250
gremlin.vertex_degree_limit=100
gremlin.edges_total_limit=500
gremlin.batch_query_ids=100
2.2.3启动:进入hugegraph-hubble-1.6.0目录下的bin目录启动start-hubble.sh
./start-hubble.sh
启动成功后会出现以下信息:
2.2.4图创建注意事项
图名称就是hugegraph.properties配置文件中store的名称,一般默认是hugegraph。
2.3 Hugegraph-Loader
2.3.1概述:HugeGraph-Loader 是 HugeGraph 的数据导入组件,能够将多种数据源的数据转化为图的顶点和边并批量导入到图数据库中。其中启动需依赖hugegraph-server服务。
2.3.2使用:
- 文件准备
- 数据文件:可以是txt、csv、json等格式
- schema文件:编写好的图模型,主要定义了一些属性、顶点类型、边类型等,文件一般命名为schema.groovy。
- 数据映射文件:映射文件用于描述如何将数据与图的顶点类型/边类型建立映射关系,以JSON格式组织。一般命名为struct.json。
- 执行命令导入
sh bin/hugegraph-loader.sh -g hugegraph -f example/file/struct.json -s example/file/schema.groovy
-g(–graph):图数据库空间,指代的是图数据库的名称,即hugegraph-hubble.properties下的store属性。
-f(–file):配置脚本的路径,即数据映射文件路径。
-s(–schema):schema文件路径。
三、查询接口
2.1 整体查询逻辑
2.2 代码详解
以查询顶点类型为例
2.2.1 创建springboot项目基本结构
以下目录可作为参考。
2.2.2 实体类
- Vertex类
package com.vanky.graph.pojo;
import lombok.Data;
import java.util.Map;
@Data
public class Vertex {
//顶点id
private String id;
//结点类型
private String label;
//结点属性
private Map<String,String> properties;
}
- 如何设置实体类中的属性
实体类中的属性是根据实际查询到的json格式数据来设置的。
观察查询得到的json数据中包括"id"、“label”、"properties"三个属性,其中"properties"还包括有属性,则可以将"properties"定义为map且其对应的key和value都是string类型(key和value的数据类型根据自己实际情况来定义)。
- Result类
以下的结果类型仅供参考:
package com.vanky.graph.pojo;
import lombok.Data;
@Data
public class Result<T> {
private Integer code;
private String msg;
private T data;
public static <T> Result<T> success() {
Result<T> result = new Result<T>();
result.code = 1;
return result;
}
public static <T> Result<T> success(T object) {
Result<T> result = new Result<T>();
result.data = object;
result.code = 1;
return result;
}
public static <T> Result<T> error(String msg) {
Result result = new Result();
result.msg = msg;
result.code = 0;
return result;
}
}
2.2.3 对应Service及其实现类
- VertexService类
package com.vanky.graph.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.vanky.graph.pojo.Vertex;
import java.util.List;
public interface VertexService {
public List<Vertex> getAllVertex() throws JsonProcessingException;
}
- VertexServiceImpl类
package com.vanky.graph.service.impl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vanky.graph.pojo.Vertex;
import com.vanky.graph.service.VertexService;
import groovy.util.logging.Slf4j;
import org.apache.hugegraph.driver.GremlinManager;
import org.apache.hugegraph.driver.HugeClient;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
@Slf4j
public class VertexServiceImpl implements VertexService {
@Override
public List<Vertex> getAllVertex() throws JsonProcessingException {
//1.连接到 HugeGraph 数据库,builder中前面是访问地址,后面是图名
HugeClient hugeClient=HugeClient.builder("http://localhost:8080","hugegraph")
.build();
//创建一个灵活的ObjectMapper对象,允许在反序列化时忽略目标类中不存在的属性。
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
//定义集合来接受查询到的数据
List<Vertex> vertices = new ArrayList<>();
//2.执行查询语句
/*这行代码创建了一个GremlinManager对象,它是与Gremlin语言交互的管理器。hugeClient是一个可能是HugeGraph的客户端对 象,通过它可以获取GremlinManager实例。*/
GremlinManager gremlin = hugeClient.gremlin();
/* 使用gremlin对象执行了一个图遍历查询,查询语句为g.V(),表示获取图数据库中的所有顶点。然后,通过 execute()方法执行这个查询,并通过data()方法获取查询结果,结果以List<Object>的形式返回。*/
List<Object> data = gremlin.gremlin("g.V()").execute().data();
//3.将查询到的数据修改为json格式
for (Object vertex : data) {
String jsonString = vertex.toString();
// 将字符串转换为带有冒号的格式
jsonString = jsonString
.replace("{", "{\"")
.replace("}", "\"}")
.replace("=", "\":\"")
.replace(", ", "\",\"")
.replace(":\"{",":{")
.replace("}\"}","}}");
vertices.add(objectMapper.readValue(jsonString, Vertex.class));
}
//关闭管理器
hugeClient.close();
//返回查询到的并修改为json格式的数据
return vertices;
}
}
2.2.4 Controller类
package com.vanky.graph.controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.io.JsonEOFException;
import com.vanky.graph.pojo.Result;
import com.vanky.graph.pojo.Vertex;
import com.vanky.graph.service.VertexService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/vertex")
public class VertexController {
@Resource
private VertexService vertexService;
@GetMapping("/all")
public Result<List<Vertex>> getAllVertex()throws JsonProcessingException{
List<Vertex> allVertex=vertexService.getAllVertex();
return Result.success(allVertex);
}
}
2.2.5 主类
package com.vanky.graph;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GraphApplication {
public static void main(String[] args) {
SpringApplication.run(GraphApplication.class);
}
}
2.2.6 测试类
仅供参考
package com.vanky.graph.test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.vanky.graph.pojo.Edge;
import com.vanky.graph.pojo.Vertex;
import com.vanky.graph.properties.HugeClientProperties;
import com.vanky.graph.service.EdgeService;
import com.vanky.graph.service.VertexService;
import com.vanky.graph.service.impl.EdgeServiceImpl;
import com.vanky.graph.service.impl.VertexServiceImpl;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class test01 {
@Test
public void VertexTest() throws JsonProcessingException {
VertexService vertexService=new VertexServiceImpl();
List<Vertex> allVertex=vertexService.getAllVertex();
System.out.println(allVertex);
}
}