當前位置:網站首頁>HugeGraph客戶端APP開發(一)
HugeGraph客戶端APP開發(一)
2022-05-14 09:36:40【51CTO】
一、簡介
本文測試項目努力實現從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依賴項:
< 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]
然後,構建並運行應用程序即可。
五:觀察運行結果
命令行輸出如下:
六、主要參考
- 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
版權聲明
本文為[51CTO]所創,轉載請帶上原文鏈接,感謝
https://cht.chowdera.com/2022/134/202205140843303824.html
邊欄推薦
- 【服務器數據恢複】硬盤壞道和不穩定扇區導致服務器崩潰的數據恢複案例
- 性能測試報告編寫技巧
- ASP.NET對Cookie的操作方法有哪些
- SAP UI5 應用開發教程之八十七 - 如何讓 SAP UI5 Mock 服務器支持自定義 url 參數試讀版
- Redis基礎之溫故
- 神經網絡中的反向傳播&&參數更新
- 深度學習基礎知識點(一)CNN卷積神經網絡——1.卷積方面的原理
- 從PlatEMO中提取真實PF前沿
- G020-OP-INS-RHEL-02 RedHat OpenStack 發放雲主機(命令行)
- 解决報錯: AttributeError: module ‘distutils‘ has no attribute ‘version‘,親測有效
猜你喜歡
隨機推薦
- c# 獲取枚舉描述的擴展方法
- 應如何認定解除合同通知的效力?
- 遊戲行業實戰案例5:玩家在線分布
- 【LeetCode】Day59-醜數 & 不同的二叉搜索樹
- CTFSHOW MISC入門
- 【國產免費】分布式作業批量處理平臺TASKCTL驗證的不同方式
- 數學建模學習(66):支持向量機 (SVM)案例實戰
- Thanos Sidecar組件
- Meta AI 宣布對人腦和語言處理進行長期研究
- 檢討書範文生成微信小程序工具源碼-支持流量主
- 元組類型(C# 參考)
- PTC:元宇宙引發醫療設備研發重大變革
- 面試題 01.05. 一次編輯 / 劍指 Offer II 041. 滑動窗口的平均值
- 華為機試第十一題:HJ11 數字顛倒
- Pascal VOC2012數據集
- unzip命令
- flink(scala版)學習一之常用的source
- [電路]7-實際電源模型和等效變換
- Go語言type自定義類型哦
- Pytorch和GPU有關操作(CUDA)
- 不均衡樣本集的重采樣
- uni-app技術分享| uni-app轉小程序-實時消息
- SQL中某個字段大於等於且不等於某值該如何寫
- 【Leetcode】442. 數組中重複的數據
- 2022年為什麼降薪也要跳槽?機會比漲薪很重要?
- 工作流結合動態錶單的工作流程
- 為什麼要使用.NET5?.NET5是未來!
- (pycharm)安裝nltk包
- 安裝Apache
- 利用循環輸入輸出數組(簡便易學)利用循環設置函數
- 雲原生時代的搜索服務算力管理
- 證券投資基金的監管
- ArrayList循環删除元素的常見問題及解决方法
- Stack Overflow 上最熱門的 10 個 Kotlin 問題
- 555 定時器的時間計算
- 二叉樹的最近公共祖先
- 模擬卷Leetcode【普通】931. 下降路徑最小和
- C語言 數組(一維數組 · 二維數組)
- NFC之華為AIPASS認證:測試用例簡介
- 622. 設計循環隊列