簡體   English   中英

Titan與Cassandra作為后端:在Java中創建,存儲和遍歷圖形

[英]Titan With Cassandra as Backend : creating , storing and traversing the graph in java

我對Titan還是陌生的,當我開始對其進行研究時,我感到困惑,因為它擁有大量的新事物,例如Gremlin,tinkerpop和rexter等。

我想要的是java中的一個示例,該示例利用titan和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();

因此,當我運行此命令時,我觀察了cassandra日志,並創建了一個名為titan的鍵空間和以下表格:

  • titan_ids
  • 邊緣商店
  • 圖形索引
  • 系統屬性
  • 系統日志
  • 日志
  • edgestore_lock_
  • graphindex_lock_
  • system_properties_lock_

我不知道這些表的用途以及它們如何存儲數據。

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

我有以下問題:

  1. 圖如何存儲在cassandra中?

  2. 現在,我已經將此圖說“ x”存儲在cassandra中。 假設我創建了另一個圖形“ y”並將其存儲。 如何能夠檢索和遍歷任何特定圖形? 因為在普通的cql查詢中,您知道要查詢的表和列。 我將如何分別識別“ x”和“ y”。

  3. 任何人都可以幫助在Java中發布示例代碼以使用一些示例csv數據創建圖形。 存儲在Cassandra中以及同一圖的一些遍歷示例。 因為沒有這樣的例子是可以理解的,將會很有幫助。

您在那里有幾個問題,所以我將盡力回答。

問題1:

如果您對如何將數據持久保存到數據庫感興趣,則應在此處查看它詳細描述了titan數據模型。 我不確定它轉換為提交日志和表的程度如何,但這只是一個開始。

問題2:

因此,您之所以選擇了名為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);

當然,您可以在此處概述的同一按鍵中創建多個斷開連接的圖

問題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")));

上面的示例是Titan的簡單用法。 我建議您瀏覽[tinkerpop]文檔,以便您熟悉使用它。 最終,您可以通過Tinkerpop API與titan進行交互。

希望對您有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM