Sye*_*afa 1 java cassandra graph-databases graph-traversal 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();
Run Code Online (Sandbox Code Playgroud)
所以当我运行它时,我观察了cassandra日志并创建了一个名为titan的键空间和下表:
我不知道这些表的用途以及它们如何存储数据.
运行程序后,会创建一个包含2个顶点和它们之间边缘的图形.我查询了表格,并在每个表格中找到了一些十六进制值.
我有以下问题:
如何将图存储在cassandra中?
现在我有这个图表说'x'存储在cassandra中.假设我创建了另一个图形'y'并存储它.如何检索和遍历任何特定图表?因为在正常的cql查询中,您知道要查询的表和列.我将如何分别识别'x'和'y'.
任何人都可以帮助在java中发布示例代码,以使用一些示例csv数据创建图形.存储在Cassandra和一些相同图形的遍历示例.会有很多帮助,因为没有可用的例子是可以理解的.
你有几个问题,所以我会尽量回答.
问题1:
如果您对数据如何持久保存到数据库感兴趣,那么您应该看一下它详细描述了泰坦数据模型.我不确定它转换为提交日志和表的好坏,但它是一个开始.
问题2:
所以你最终得到一个被称为keyoace的原因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);
Run Code Online (Sandbox Code Playgroud)
当然,您可以在此处概述的相同按键中创建多个断开连接的图形
问题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")));
Run Code Online (Sandbox Code Playgroud)
上面的例子是Titan的简单使用.我建议您浏览[tinkerpop]文档,以便熟悉使用它.在一天结束时,您通过Tinkerpop API与titan接口.
我希望这会对你有所帮助