Titan -GraphDatabase 配置(二)--Java 連線
阿新 • • 發佈:2018-11-19
Titan -GraphDatabase 配置
System:Ubuntu 16.04
參考博文連結:
https://blog.csdn.net/u010960155/article/details/81069112
https://blog.csdn.net/wyc199273/article/details/51559442
一、通過Java讀寫Titan資料
啟動相關服務指令碼
sudo /opt/hadoop-2.6.5/sbin/start-dfs.sh sudo /opt/hadoop-2.6.5/sbin/start-yarn.sh sudo /opt/hbase-1.2.8/bin/start-hbase.sh # sudo /opt/hbase-0.98.24-hadoop2/bin/start-hbase.sh sudo /opt/elasticsearch-1.5.2/bin/elasticsearch -d sudo /opt/titan-1.0.0-hadoop2/bin/titan.sh start
停止相關服務指令碼
sudo /opt/hadoop-2.6.5/sbin/stop-dfs.sh
sudo /opt/hadoop-2.6.5/sbin/stop-yarn.sh
sudo /opt/hbase-1.2.8/bin/stop-hbase.sh
# sudo /opt/hbase-0.98.24-hadoop2/bin/stop-hbase.sh
sudo /opt/titan-1.0.0-hadoop2/bin/titan.sh stop
# 如果部分服務無法關閉,通過jps查詢對應id,然後使用 kill -9 serId關閉
Java連線資料庫程式碼
public static class TestGetTitan { public static final String INDEX_NAME = "search"; public static void main(String[] args) { System.out.println("======== config opening ===="); //需要現在gremlin中載入資料庫然後才能讀取到 String filePath = "/opt/titan-1.0.0-hadoop2/conf/titan-cassandra-es.properties"; TitanGraph graph = TitanFactory.open(filePath); System.out.println("======== config opened ===="); System.out.println("======== graph opening ===="); GraphTraversalSource g = graph.traversal(); System.out.println("======== graph opened ===="); // 第一次需要先load這個 將資料匯入到本地的hbase中 // 下面的例子可以結合剛才文件中chapter 3 中的例子看 // GraphOfTheGodsFactory.load(g); //對應gremlin內的 g.V().has('name','hercules').next().value('name') System.out.println("======" + g.V().has("name", "hercules").next().value("name")); //g.V().has('name','hercules').next().values('name','age') System.out.println("======" + g.V().has("name", "hercules").next().values("name", "age")); Iterator<?> iterator = g.V().has("name","hercules").next().values("name", "age"); while(iterator.hasNext()){ Object o = iterator.next(); System.out.println("======"+o); } Vertex saturn = g.V().has("name","saturn").next(); System.out.println("======"+saturn); //g.V(vertex).in("father").in("father").next() 得到 saturn的孫子節點 System.out.println("======" + g.V(saturn).in("father").in("father").next().value("age")); GraphTraversal<Edge, Edge> a = g.E().has("place", P.eq(Geoshape.point(38.1f, 23.7f))); System.out.println("======"+a); while(a.hasNext()){ Edge e = a.next(); System.out.println("======"+e.keys()); System.out.println("======"+e.label()); System.out.println("======"+e.outVertex().value("name")); System.out.println("======"+e.inVertex().value("name")); System.out.println("======"+e.value("time")+" : "+e.value("place")); } Vertex hercules = g.V().has("name","hercules").next(); System.out.println("======"+g.V(hercules).out("mother","father").values("name")); GraphTraversal<Vertex,Vertex> mF = g.V(hercules).out("mother", "father"); while(mF.hasNext()){ Vertex v = mF.next(); System.out.println("======"+ v.label()+" : "+v.value("name")); } System.out.println("======" + g.V(saturn).repeat(__.in("father")).times(2).next().value("name")); GraphTraversal<Vertex,Vertex> monsters = g.V(hercules).out("battled"); while(monsters.hasNext()){ Vertex monster = monsters.next(); System.out.println("======"+monster.label()+" : "+monster.value("name")); } monsters = g.V(hercules).outE("battled").has("time",P.eq(1)).inV(); while(monsters.hasNext()){ Vertex monster = monsters.next(); System.out.println("======"+monster.label()+" : "+monster.value("name")); } Vertex pluto = g.V().has("name","pluto").next(); //通過out得到住的地方的節點,在in得到所有連結到這個地方的節點,從而得到所有住在這個地方的節點 out 邊出去的條件 in 邊進來的條件 GraphTraversal<Vertex,Vertex> liveInTartarusVertex = g.V(pluto).out("lives").in("lives"); while(liveInTartarusVertex.hasNext()){ Vertex vertex = liveInTartarusVertex.next(); System.out.println("======"+vertex.value("name")); } GraphTraversal<Vertex,Vertex> liveInTartarusVertexNo = g.V(pluto).out("lives").in("lives").where( __.is(P.neq(pluto))); while(liveInTartarusVertexNo.hasNext()){ Vertex vertex = liveInTartarusVertexNo.next(); System.out.println("======"+vertex.value("name")); } GraphTraversal<Vertex,Vertex> liveInTartarusVertexNot = g.V(pluto).as("x").out("lives").in("lives").where(P.neq( "x")); while(liveInTartarusVertexNot.hasNext()){ Vertex vertex = liveInTartarusVertexNot.next(); System.out.println("======"+vertex.value("name")); } GraphTraversal<Vertex,Map<String, Vertex>> brothers = g.V(pluto).out("brother").as("god").out("lives").as("place").select("god","place"); while(brothers.hasNext()){ Map<String,Vertex> map = brothers.next(); System.out.println("======"+map); for(Map.Entry<String,Vertex> entry:map.entrySet()){ System.out.println(entry.getKey()+" : "+entry.getValue().value("name")); } } System.out.println("======"+g.V(pluto).outE("lives").next().value("reason")); /*GraphTraversal<Edge,Object> reasons = g.E().has("reason").values("reason").is(Text.textContains("loves")); System.out.println(reasons); while(reasons.hasNext()){ Object e = reasons.next(); System.out.println("======"+e); }*/ GraphTraversal<Edge,Edge> reasons = g.E().has("reason").as("r").values("reason").is(Text.textContains("loves")).select("r"); System.out.println(reasons); while(reasons.hasNext()){ Edge e = reasons.next(); System.out.println("======"+e.keys()); System.out.println("======"+e.label()); System.out.println("======"+e.value("reason")); } GraphTraversal<Edge,Map<String,Object>> reasons2 = g.E().has("reason").as("source").values("reason").is(Text.textContains("loves")).as("reason").select("source") .outV().values("name").as("god").select("source").inV().values("name").as("thing").select("god","reason","thing"); while(reasons2.hasNext()){ Map<String,Object> map = reasons2.next(); System.out.println("======"+map); for(Map.Entry<String,Object> entry:map.entrySet()){ System.out.println(entry.getKey()+" : "+entry.getValue()); } } // System.out.println("======"+); // System.out.println("======"+g.V(pluto).out("lives").in("lives")); graph.close(); } }
除錯的時候用了很多時間,主要問題體現在jar包依賴上。
需要特殊排除一些jar包,以下是maven的依賴
<dependency> <groupId>com.thinkaurelius.titan</groupId> <artifactId>titan-core</artifactId> <version>${titan.version}</version> </dependency> <dependency> <groupId>com.thinkaurelius.titan</groupId> <artifactId>titan-hbase</artifactId> <version>${titan.version}</version> </dependency> <dependency> <groupId>com.thinkaurelius.titan</groupId> <artifactId>titan-es</artifactId> <version>${titan.version}</version> </dependency> <dependency> <groupId>com.thinkaurelius.titan</groupId> <artifactId>titan-hadoop</artifactId> <version>${titan.version}</version> <exclusions> <!-- 這個jar包也是官網檔案需要刪除的 --> <exclusion> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.tinkerpop</groupId> <artifactId>gremlin-driver</artifactId> <version>3.2.6</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>0.98.24-hadoop2</version> </dependency> <!-- https://mvnrepository.com/artifact/com.thinkaurelius.titan/titan-berkeleyje --> <dependency> <groupId>com.thinkaurelius.titan</groupId> <artifactId>titan-berkeleyje</artifactId> <version>0.9.0-M2</version> </dependency> <!-- https://mvnrepository.com/artifact/com.thinkaurelius.titan/titan-hadoop-core --> <dependency> <groupId>com.thinkaurelius.titan</groupId> <artifactId>titan-hadoop-core</artifactId> <version>1.0.0</version> <exclusions> <!-- 這兩個jar包無法下載 --> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> </exclusion> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </exclusion> </exclusions> </dependency> <!-- https://mvnrepository.com/artifact/com.thinkaurelius.titan/titan-hadoop --> <dependency> <groupId>com.thinkaurelius.titan</groupId> <artifactId>titan-hadoop</artifactId> <version>1.0.0</version> <type>pom</type> </dependency>