泰坦与卡桑德拉作为后端:创建,存储和遍历图中的java

问题描述:

我是泰坦新的死亡,当我开始研究它时,我感到困惑,因为它有像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我注册了一个免费帐户,并在 –

你有几个问题在那里,所以我会尽量回答我的问题。

问题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进行交互。

我希望这有助于你