Hugegragh安装和基本使用
版本:
hugeGraph server v 0.12.0
hugeGragh-hubble v 1.6.0
下述如有疑问可以进群探讨:QQ群:719751534
安装
下载
下载地址 Download · GitBook (hugegraph.github.io)
下载 hugeGraph server 和hugeGragh-hubble
上传至服务器
解压后如图
修改配置文件
# 默认使用rocksdb数据库
rocksdb.data_path=/opt/hugegragh/disk # 数据存放路径
rocksdb.wal_path=/opt/hugegragh/disk # 日志存放路径
如果是服务器上安装的 请修改为服务ip,方便hubble连接操作
启动
# 初始化
执行 init-store.sh
# 启动
执行 start-hugegraph.sh
# 关闭
执行 stop-hugegreaph.sh
启动图形管理界面
修改访问地址和端口
启动与关闭
# 启动
执行 start-hubble.sh
# 关闭
执行 stop-hubble.sh
访问 ip:8088
基本使用
创建图
创建失败:
检查 rest-server.properties 文件中的地址是否正确
关闭
stop-hugegreaph.sh
重新执行
init-store.sh
启动
start-hugegreaph.sh
重新创建
创建属性
创建顶点
创建边
添加顶点
# 添加一个顶点
graph.addVertex(T.label,"star",T.id,1,"starId","1",'starName',"小明")
graph.addVertex(T.label,"star",T.id,2,"starId","2",'starName',"小张")
# 查询所有顶点
g.V()
# 查询所有边
g.E()
# 根据id删除顶点
g.V('1').drop()
创建关系
from = g.V(1).next()
to = g.V(2).next()
from.addEdge("relation",to,"startRelation","朋友") # startRelation 跟视频中的starRelation 不同。小瑕疵不想重新建了 -.-
代码测试
初试client
下拉client
https://github.com/hugegraph/hugegraph-client.git
修改 com.baidu.hugegraph.BaseClientTest 类中的连接信息
public class MyTest {
@Before
public void before() {
BaseClientTest.open();
}
@Test
public void one() {
String gremlin = "g.V()";
GremlinRequest gremlinRequest = new GremlinRequest(gremlin);
ResultSet resultSet = BaseClientTest.gremlin().execute(gremlinRequest);
resultSet.iterator().forEachRemaining(item -> {
System.out.println(item.getString());
});
System.out.println("结束");
}
}
实战
json关系下载
ComplexNetwork/relation.json at master · xjtushilei/ComplexNetwork (github.com)
添加依赖
视频里面用的是fastjson,up用可以,我用就会出现classDefException等问题
所以改成了gson
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
编写代码
将relation.json放入resource目录下
/**
* 实战
*/
@Test
public void two() throws IOException {
String dataJson = FileUtils.readFileToString(new File("E:\\gitee\\hugegraph-client\\src\\test\\resources\\relation.json"), StandardCharsets.UTF_8);
Type type = new TypeToken<ArrayList<RelationVo>>() {
}.getType();
Gson gson = new Gson();
ArrayList<RelationVo> arrayList = gson.fromJson(dataJson, type);
String V = "graph.addVertex(T.label,\"star\",T.id,%s,\"starId\",\"%s\",'starName',\"%s\") ";
String R = "from = g.V(%s).next()\n" +
"to = g.V(%s).next()\n" +
"from.addEdge(\"relation\",to,\"startRelation\",\"%s\")";
arrayList.forEach(item -> {
int id1 = item.getId1();
int id2 = item.getId2();
String name1 = item.getName1();
String name2 = item.getName1();
String relationship = item.getRelationship();
String ev1 = String.format(V, id1, id1, name1);
String ev2 = String.format(V, id2, id2, name2);
String er = String.format(R, id1, id2, relationship);
// 创建顶点
executeGremlin(ev1);
executeGremlin(ev2);
// 添加关系
executeGremlin(er);
});
}
private void executeGremlin(String gremlin) {
// System.out.println(gremlin);
GremlinRequest gremlinRequest = new GremlinRequest(gremlin);
BaseClientTest.gremlin().execute(gremlinRequest);
}
执行后
部分查询语句
# 赵薇所有人际关系
g.V(139401).repeat(out()).until(out().count().is(0)).path()
# 查询周杰伦的关系
g.V(129156).out().path()