【问题标题】:Titan With Cassandra as Backend : creating , storing and traversing the graph in java以 Cassandra 作为后端的 Titan:在 java 中创建、存储和遍历图形
【发布时间】:2017-04-12 12:07:53
【问题描述】:

我对 Titan 完全陌生,当我开始研究它时,我感到很困惑,因为它有很多新东西,比如 Gremlin、tinkerpop 和 rexter 等。

我想要的是 java 中的一个示例,它使用以 Cassandra 作为后端的 Titan。我想创建一个图,存储在 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
  • 边缘存储
  • 图形索引
  • system_properties
  • 系统日志
  • txlog
  • edgestore_lock_
  • graphindex_lock_
  • system_properties_lock_

我不知道这些表的用途以及它们如何存储数据。

运行程序后,它会创建一个由 2 个顶点和它们之间的边组成的图形。我查询了这些表,并在每个表中发现了一些十六进制值。

我有以下问题:

  1. 图形是如何存储在 cassandra 中的?

  2. 现在我将这张图说“x”存储在 cassandra 中。假设我创建了另一个图形“y”并存储它。如何能够检索和遍历任何特定的图?因为在普通的 cql 查询中,您知道要查询的表和列。我将如何分别识别“x”和“y”。

  3. 任何人都可以帮助在 java 中发布示例代码以使用一些示例 csv 数据创建图形。存储在 Cassandra 和一些遍历示例中的同一张图。将很有帮助,因为没有这样可以理解的示例。

【问题讨论】:

  • 你真的需要泰坦吗? Datastax 在 Cassandra 上有图表。 datastax.com/dse-graph-campaign/index.html
  • @cricket_007 DSE 只是我认为的商业产品。因此,如果您无法为 DSE 省钱,使用 Titan 是一个不错的选择。 JanusGraph 也是一个不错的免费选择。
  • @FilipeTeixeira 我注册了一个免费帐户并且之前安装了 DSE
  • @cricket_007 会再看一遍,谢谢。我的印象是,要在大规模生产中部署 DSE Graph,您必须为此付费。
  • @FilipeTeixeira 我会澄清的。对于“非生产目的”,您可以免费使用它。 datastax.com/enterprise-terms

标签: java cassandra graph-databases titan graph-traversal


【解决方案1】:

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

问题 1:

如果您对如何将数据持久化到数据库中感兴趣,那么您应该查看here,它详细描述了 Titan 数据模型。我不确定它对提交日志和表格的转换效果如何,但这是一个开始。

问题 2:

所以你最终得到一个名为titan 的keyoace 的原因是你没有提供你自己的。通常,当创建彼此无关的不同图形时,您会将这些图形存储在不同的键空间中。这样做如下:

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);

当然,您可以在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")));

上面的例子是 Titan 的简单使用。我建议您浏览 [tinkerpop] 文档,以便您熟悉使用它。在一天结束时,您可以通过 Tinkerpop API 与 Titan 交互。

希望对你有所帮助

【讨论】:

    猜你喜欢
    • 2013-10-28
    • 2017-04-05
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    相关资源
    最近更新 更多