Titan简介

Titan 是一个分布式的图形数据库,特别为存储和处理大规模图形而优化。

支持不同的分布式存储层

  • Cassandra 1.1
  • HBase 0.92
    内置实现 Blueprints graph API
  • Gremlin graph traversal language
  • Frames graph-to-object mapper
  • Rexster graph server

Java项目搭建

依赖Jar包:titan安装目录lib下jar文件
hbase:由于使用hbase作为底层存储,所以需要拷贝hbase的配置文件hbase-site.xml文件到classpath

代码实现

  
  
  1. import org.apache.commons.configuration.BaseConfiguration;
  2. import com.thinkaurelius.titan.core.TitanFactory;
  3. import com.thinkaurelius.titan.core.TitanGraph;
  4. import com.tinkerpop.blueprints.Edge;
  5. import com.tinkerpop.blueprints.Vertex;
  6. public class TitanDemo {
  7. public static void main(String args[]) {
  8. BaseConfiguration baseConfiguration = new BaseConfiguration();
  9. baseConfiguration.setProperty("storage.backend", "hbase");
  10. baseConfiguration.setProperty("storage.hostname", "192.168.0.150");
  11. baseConfiguration.setProperty("storage.tablename","test");
  12. TitanGraph titanGraph = TitanFactory.open(baseConfiguration);
  13. Vertex rash = titanGraph.addVertex(null);
  14. rash.setProperty("userId", 1);
  15. rash.setProperty("username", "rash");
  16. rash.setProperty("firstName", "Rahul");
  17. rash.setProperty("lastName", "Chaudhary");
  18. rash.setProperty("birthday", 101);
  19. Vertex honey = titanGraph.addVertex(null);
  20. honey.setProperty("userId", 2);
  21. honey.setProperty("username", "honey");
  22. honey.setProperty("firstName", "Honey");
  23. honey.setProperty("lastName", "Anant");
  24. honey.setProperty("birthday", 201);
  25. Edge frnd = titanGraph.addEdge(null, rash, honey, "FRIEND");
  26. frnd.setProperty("since", 2011);
  27. titanGraph.commit();
  28. Iterable<Vertex> results = rash.query().labels("FRIEND")
  29. .has("since", 2011).vertices();
  30. for (Vertex result : results) {
  31. System.out.println("Id: " + result.getProperty("userId"));
  32. System.out.println("Username: " + result.getProperty("username"));
  33. System.out.println("Name: " + result.getProperty("firstName") + " "
  34. + result.getProperty("lastName"));
  35. }
  36. titanGraph.shutdown();
  37. }
  38. }

可以参考JAVA API:http://thinkaurelius.github.io/titan/javadoc/current/

https://github.com/thinkaurelius/titan