0
点赞
收藏
分享

微信扫一扫

HugeGraph客户端APP开发(一)

cnlinkchina 2022-05-14 阅读 78

一、简介

本文测试项目努力实现从HugeGraph-Client向HugeGraph-Server发出HTTP请求,获取并解析Server的执行结果。

注意:目前官方仅提供了Java版嵌入式开发支持,即我们可以使用HugeGraph-Client编写Java代码操作HugeGraph,实现图元数据和图数据的增删改查操作,或者执行gremlin语句。

二、测试环境

  • MAC HIGH SIERRA 10.13.6
  • jdk1.8
  • Intelli IDEA 2021.2及内置Maven-3.6.3

根据官方资料,使用HugeGraph-Client的基本开发步骤如下:

  • 新建IDEA Maven项目;
  • 在pom文件中添加HugeGraph-Client依赖;
  • 创建类,调用HugeGraph-Client接口;

本文正是使用上述思路创建的示例。

三、示例项目

第一步:启动IDEA创建一个空的常规Maven项目,命名为Hello1HugeGraph

第二步:添加POM依赖项:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.hugegraph</groupId>
<artifactId>Hello1HugeGraph</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-client</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

</project>

提示:在线Maven仓库(​​https://search.maven.org​​)搜索时,最好搜索artifactId项,这样定位更准确。

第三步:编写客户端APP代码。

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import com.baidu.hugegraph.driver.GraphManager;
import com.baidu.hugegraph.driver.GremlinManager;
import com.baidu.hugegraph.driver.HugeClient;
import com.baidu.hugegraph.driver.SchemaManager;

import com.baidu.hugegraph.structure.constant.T;
import com.baidu.hugegraph.structure.graph.Edge;
import com.baidu.hugegraph.structure.graph.Path;
import com.baidu.hugegraph.structure.graph.Vertex;
import com.baidu.hugegraph.structure.gremlin.Result;
import com.baidu.hugegraph.structure.gremlin.ResultSet;

public class MainAPP {

public static void main(String[] args) throws IOException {
// If connect failed will throw a exception.
HugeClient hugeClient = HugeClient.builder("http://localhost:8080",
"hugegraph")
.build();

SchemaManager schema = hugeClient.schema();

schema.propertyKey("name").asText().ifNotExist().create();
schema.propertyKey("age").asInt().ifNotExist().create();
schema.propertyKey("city").asText().ifNotExist().create();
schema.propertyKey("weight").asDouble().ifNotExist().create();
schema.propertyKey("lang").asText().ifNotExist().create();
schema.propertyKey("date").asDate().ifNotExist().create();
schema.propertyKey("price").asInt().ifNotExist().create();

schema.vertexLabel("人物")
.properties("name", "age", "city")
.primaryKeys("name")
.ifNotExist()
.create();

schema.vertexLabel("软件")
.properties("name", "lang", "price")
.primaryKeys("name")
.ifNotExist()
.create();

schema.indexLabel("personByCity")
.onV("人物")
.by("city")
.secondary()
.ifNotExist()
.create();

schema.indexLabel("personByAgeAndCity")
.onV("人物")
.by("age", "city")
.secondary()
.ifNotExist()
.create();

schema.indexLabel("softwareByPrice")
.onV("软件")
.by("price")
.range()
.ifNotExist()
.create();

schema.edgeLabel("认识")
.sourceLabel("人物")
.targetLabel("人物")
.properties("date", "weight")
.ifNotExist()
.create();

schema.edgeLabel("开发")
.sourceLabel("人物").targetLabel("软件")
.properties("date", "weight")
.ifNotExist()
.create();

schema.indexLabel("createdByDate")
.onE("开发")
.by("date")
.secondary()
.ifNotExist()
.create();

schema.indexLabel("createdByWeight")
.onE("开发")
.by("weight")
.range()
.ifNotExist()
.create();

schema.indexLabel("knowsByWeight")
.onE("认识")
.by("weight")
.range()
.ifNotExist()
.create();

GraphManager graph = hugeClient.graph();
Vertex zhangsan = graph.addVertex(T.label, "人物", "name", "张三",
"age", 29, "city", "Beijing");
Vertex lisi = graph.addVertex(T.label, "人物", "name", "李四",
"age", 27, "city", "Hongkong");
Vertex lop = graph.addVertex(T.label, "软件", "name", "大话西游III",
"lang", "java", "price", 328);
Vertex wangwu = graph.addVertex(T.label, "人物", "name", "王五",
"age", 32, "city", "Beijing");
Vertex ripple = graph.addVertex(T.label, "软件", "name", "变脸秀APP",
"lang", "java", "price", 199);
Vertex maliu = graph.addVertex(T.label, "人物", "name", "麻六",
"age", 35, "city", "Shanghai");

zhangsan.addEdge("认识", lisi, "date", "2016-01-10", "weight", 0.5);
zhangsan.addEdge("认识", wangwu, "date", "2013-02-20", "weight", 1.0);
zhangsan.addEdge("开发", lop, "date", "2017-12-10", "weight", 0.4);
wangwu.addEdge("开发", lop, "date", "2009-11-11", "weight", 0.4);
wangwu.addEdge("开发", ripple, "date", "2017-12-10", "weight", 1.0);
maliu.addEdge("开发", lop, "date", "2017-03-24", "weight", 0.2);

GremlinManager gremlin = hugeClient.gremlin();

System.out.println("==== Path ====");
ResultSet resultSet = gremlin.gremlin("g.V().outE().path()").execute();

Iterator<Result> results = resultSet.iterator();
results.forEachRemaining(result -> {
System.out.println(result.getObject().getClass());
Object object = result.getObject();
if (object instanceof Vertex) {
System.out.println(((Vertex) object).id());
} else if (object instanceof Edge) {
System.out.println(((Edge) object).id());
} else if (object instanceof Path) {
List<Object> elements = ((Path) object).objects();
elements.forEach(element -> {
System.out.println(element.getClass());
System.out.println(element);
});
} else {
System.out.println(object);
}
});

hugeClient.close();
}
}

后面再专门写文分析上述代码中Gremlin脚本知识,在此省略。

四、构建并启动客户端APP

构建并运行上述程序前,需要先启动HugeGraph服务器:

$HG/bin/start-hugegraph.sh

正常启动服务器后的输出内容如下:

Starting HugeGraphServer...

Connecting to HugeGraphServer (http://127.0.0.1:8080/graphs)..............OK

Started [pid 7240]

然后,构建并运行应用程序即可。

五:观察运行结果

命令行输出如下:

HugeGraph客户端APP开发(一)_maven


六、主要参考

  • ​​https://hugegraph.apache.org/cn/docs/quickstart/hugegraph-client/​​
  • ​​https://tinkerpop.apache.org/docs/current/reference/​​
  • ​​https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-example/src/main/java/com/baidu/hugegraph/example/Example1.java​​


举报

相关推荐

0 条评论