目录

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

代码实现

import org.apache.commons.configuration.BaseConfiguration;

import com.thinkaurelius.titan.core.TitanFactory;
import com.thinkaurelius.titan.core.TitanGraph;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;

public class TitanDemo {
    public static void main(String args[]) {
        BaseConfiguration baseConfiguration = new BaseConfiguration();
        baseConfiguration.setProperty("storage.backend", "hbase");
        baseConfiguration.setProperty("storage.hostname", "192.168.0.150");
        baseConfiguration.setProperty("storage.tablename","test");
        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.commit();
        
        Iterable<Vertex> results = rash.query().labels("FRIEND")
                .has("since", 2011).vertices();

        for (Vertex result : results) {
            System.out.println("Id: " + result.getProperty("userId"));
            System.out.println("Username: " + result.getProperty("username"));
            System.out.println("Name: " + result.getProperty("firstName") + " "
                    + result.getProperty("lastName"));
        }
        titanGraph.shutdown();
    }
}

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