當前位置:網站首頁>HugeGraph客戶端APP開發(一)

HugeGraph客戶端APP開發(一)

2022-05-14 09:36:4051CTO

一、簡介

本文測試項目努力實現從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 >
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

提示:在線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();
}
}
  • 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.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.

後面再專門寫文分析上述代碼中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開發(一)_apache


六、主要參考


版權聲明
本文為[51CTO]所創,轉載請帶上原文鏈接,感謝
https://cht.chowdera.com/2022/134/202205140843303824.html

隨機推薦