2017-04-12 44 views
1

我是泰坦新的死亡,當我開始研究它時,我感到困惑,因爲它有像gremlin,tinkerpop和引擎蓋下的新東西過多rexter等泰坦與卡桑德拉作爲後端:創建,存儲和遍歷圖中的java

我想要的是一個在java中使用Cassandra作爲後端的例子。我想創建一個圖形,存儲在cassandra中,將其恢復並遍歷它。一個非常簡單也會很有幫助。

我在java中運行了一個基本的例子。

BaseConfiguration baseConfiguration = new BaseConfiguration(); 
    baseConfiguration.setProperty("storage.backend", "cassandra"); 
    baseConfiguration.setProperty("storage.hostname", "192.168.3.82"); 

    TitanGraph titanGraph = TitanFactory.open(baseConfiguration); 

    Vertex rash = titanGraph.addVertex(null); 
     rash.setProperty("userId", 1); 
     rash.setProperty("username", "rash"); 
     rash.setProperty("firstName", "Rahul"); 
     rash.setProperty("lastName", "Chaudhary"); 
     rash.setProperty("birthday", 101); 

     Vertex honey = titanGraph.addVertex(null); 
     honey.setProperty("userId", 2); 
     honey.setProperty("username", "honey"); 
     honey.setProperty("firstName", "Honey"); 
     honey.setProperty("lastName", "Anant"); 
     honey.setProperty("birthday", 201); 

     Edge frnd = titanGraph.addEdge(null, rash, honey, "FRIEND"); 
     frnd.setProperty("since", 2011); 

     titanGraph.shutdown(); 

所以,當我運行此,我觀察到的卡桑德拉日誌,它創建了一個密鑰空間名爲Titan和下表:

  • titan_ids
  • edgestore
  • graphindex
  • system_properties
  • systemlog
  • txlog
  • edgestore_lock_
  • graphindex_lock_
  • system_properties_lock_

我不知道是用來做什麼的這些表和它們是如何存儲數據。

運行該程序後,該程序創建了2個頂點的圖形以及它們之間的邊。我查詢了這些表,並在每個表中找到了一些十六進制值。

我有以下問題:

  1. 如何在圖表存儲在卡桑德拉?

  2. 現在,我有這個圖表說'x'存儲在卡桑德拉。說我創建了另一個圖表'y'並存儲它。如何能夠檢索和遍歷任何特定的圖形?因爲在正常的cql查詢中,您知道要查詢的表和列。我將如何分別識別'x'和'y'。

  3. 任何人都可以幫助在java中發佈示例代碼來使用一些示例csv數據創建圖形。存儲在Cassandra和一些遍歷相同圖形的例子。由於沒有這樣的例子可以理解,所以會很有幫助。

+0

你真的需要泰坦嗎? Datastax在Cassandra上有圖表。 http://www.datastax.com/dse-graph-campaign/index.html –

+0

@ cricket_007只有我相信DSE纔是商業產品。所以如果你不能把錢留給DSE,那麼使用泰坦是一個很好的選擇。 [JanusGraph](https://github.com/JanusGraph/janusgraph)也是一個不錯的免費選擇。 –

+0

@FilipeTeixeira我註冊了一個免費帳戶,並在 –

回答

6

你有幾個問題在那裏,所以我會盡量回答我的問題。

問題1:

如果您感興趣的數據是如何堅持到數據庫,那麼你應該看看here它詳細描述了巨人的數據模型。我不確定它如何轉化爲提交日誌和表格,但這是一個開始。

問題2:

所以你結束了一個keysoace之所以稱爲titan是因爲你沒有提供你自己的。通常,在創建與彼此無關的不同圖形時,可以將這些圖存儲在不同的密鑰空間中。這是做如下:

BaseConfiguration baseConfiguration = new BaseConfiguration(); 
baseConfiguration.setProperty("storage.backend", "cassandra"); 
baseConfiguration.setProperty("storage.hostname", "192.168.3.82"); 

//First Graph 
baseConfiguration.setProperty("storage.cassandra.keyspace", "keyspace1"); 
TitanGraph titanGraph1 = TitanFactory.open(baseConfiguration); 

//Second Graph 
baseConfiguration.setProperty("storage.cassandra.keyspace", "keyspace2"); 
TitanGraph titanGraph2 = TitanFactory.open(baseConfiguration); 

當然你也可以在同一個keysoace創建多個非圖形所概述here

問題3:

就是有點加載的問題問了解CSV遷移的示例。我想說退後一步,問自己,你想要建模什麼。

比方說,你想存儲產品清單和購買這些產品的人的列表。還有的,你可以模擬這種方式多種多樣,但現在,我們只說,人與產品是頂點和邊緣之間則代表購買:

//Initliase graph 
BaseConfiguration baseConfiguration = new BaseConfiguration(); 
baseConfiguration.setProperty("storage.backend", "cassandra"); 
baseConfiguration.setProperty("storage.hostname", "192.168.3.82"); 
baseConfiguration.setProperty("storage.cassandra.keyspace", "mycustomerdata"); 
TitanGraph graph = TitanFactory.open(baseConfiguration); 

//---------------- Adding Data ------------------- 
//Create some customers 
Vertex alice = graph.addVertex("customer"); 
alice.property("name", "Alice Mc Alice"); 
alice.property("birthdat", "100000 BC"); 

Vertex bob = graph.addVertex("customer"); 
bob.property("name", "Bob Mc Bob"); 
bob.property("birthdat", "1000 BC"); 

//Create Some Products 
Vertex meat = graph.addVertex("product"); 
meat.property("name", "Meat"); 
meat.property("description", "Delicious Meat"); 

Vertex lettuce = graph.addVertex("product"); 
lettuce.property("name", "Lettuce"); 
lettuce.property("description", "Delicious Lettuce which is green"); 

//Alice Bought some meat: 
alice.addEdge("bought", meat); 
//Bob Bought some meat and lettuce: 
bob.addEdge("bought", meat, lettuce); 

//---------------- Querying (aka traversing whcih is what you do in graph dbs) Data ------------------- 
//Now who has bought meat? 
graph.traversal().V().has("name", "meat").in("bought").forEachRemaining(v -> System.out.println(v.value("name"))); 

//Who are all our customers 
graph.traversal().V().hasLabel("customer").forEachRemaining(v -> System.out.println(v.value("name"))); 

//What products do we have 
graph.traversal().V().hasLabel("customer").forEachRemaining(v -> System.out.println(v.value("name"))); 

上面的例子是一個簡單的使用泰坦的。我會推薦使用[tinkerpop]文檔,以便您熟悉如何使用它。在一天結束的時候,你通過Tinkerpop API與Titan進行交互。

我希望這有助於你