当前位置: 首页 > 工具软件 > Titan Graph > 使用案例 >

Titan -GraphDatabase 配置(二)--Java 连接

微生智刚
2023-12-01

Titan -GraphDatabase 配置

System:Ubuntu 16.04

 

参考博文链接:

https://blog.csdn.net/u010960155/article/details/81069112

https://blog.csdn.net/wyc199273/article/details/51559442

 

一、通过Java读写Titan数据

启动相关服务脚本

sudo /opt/hadoop-2.6.5/sbin/start-dfs.sh
sudo /opt/hadoop-2.6.5/sbin/start-yarn.sh
sudo /opt/hbase-1.2.8/bin/start-hbase.sh
# sudo /opt/hbase-0.98.24-hadoop2/bin/start-hbase.sh
sudo /opt/elasticsearch-1.5.2/bin/elasticsearch -d
sudo /opt/titan-1.0.0-hadoop2/bin/titan.sh start

停止相关服务脚本

sudo /opt/hadoop-2.6.5/sbin/stop-dfs.sh
sudo /opt/hadoop-2.6.5/sbin/stop-yarn.sh
sudo /opt/hbase-1.2.8/bin/stop-hbase.sh
# sudo /opt/hbase-0.98.24-hadoop2/bin/stop-hbase.sh
sudo /opt/titan-1.0.0-hadoop2/bin/titan.sh stop
# 如果部分服务无法关闭,通过jps查找对应id,然后使用 kill -9 serId关闭

Java连接数据库代码

    public static class TestGetTitan {
        public static final String INDEX_NAME = "search";

        public static void main(String[] args) {

             System.out.println("======== config opening ====");
            //需要先在gremlin中加载数据库然后才能读取到
            String filePath = "/opt/titan-1.0.0-hadoop2/conf/titan-cassandra-es.properties";
            TitanGraph graph = TitanFactory.open(filePath);
            System.out.println("======== config opened ====");

            System.out.println("======== graph opening ====");
            GraphTraversalSource g = graph.traversal();
            System.out.println("======== graph opened ====");
//           第一次需要先load这个  将数据导入到本地的hbase中
//            GraphOfTheGodsFactory.load(g);
            //对应gremlin内的 g.V().has('name','hercules').next().value('name')
            System.out.println("======" + g.V().has("name", "hercules").next().value("name"));
            //g.V().has('name','hercules').next().values('name','age')
            System.out.println("======" + g.V().has("name", "hercules").next().values("name", "age"));
            Iterator<?> iterator = g.V().has("name","hercules").next().values("name", "age");
            while(iterator.hasNext()){
                Object o  = iterator.next();
                System.out.println("======"+o);
            }

             Vertex saturn = g.V().has("name","saturn").next();

            System.out.println("======"+saturn);

            //g.V(vertex).in("father").in("father").next()  得到 saturn的孙子节点
            System.out.println("======" + g.V(saturn).in("father").in("father").next().value("age"));

            GraphTraversal<Edge, Edge> a =  g.E().has("place", P.eq(Geoshape.point(38.1f, 23.7f)));
            System.out.println("======"+a);
            while(a.hasNext()){
                Edge e = a.next();
                System.out.println("======"+e.keys());
                System.out.println("======"+e.label());
                System.out.println("======"+e.outVertex().value("name"));
                System.out.println("======"+e.inVertex().value("name"));
                System.out.println("======"+e.value("time")+"  :  "+e.value("place"));
            }

            Vertex hercules = g.V().has("name","hercules").next();
            System.out.println("======"+g.V(hercules).out("mother","father").values("name"));
            GraphTraversal<Vertex,Vertex> mF = g.V(hercules).out("mother", "father");
            while(mF.hasNext()){
                Vertex v = mF.next();
                System.out.println("======"+ v.label()+"  :  "+v.value("name"));
            }

            System.out.println("======" + g.V(saturn).repeat(__.in("father")).times(2).next().value("name"));

            GraphTraversal<Vertex,Vertex> monsters = g.V(hercules).out("battled");
            while(monsters.hasNext()){
                Vertex monster = monsters.next();
                System.out.println("======"+monster.label()+"  :  "+monster.value("name"));
            }

            monsters = g.V(hercules).outE("battled").has("time",P.eq(1)).inV();
            while(monsters.hasNext()){
                Vertex monster = monsters.next();
                System.out.println("======"+monster.label()+"  :  "+monster.value("name"));
            }
            Vertex pluto = g.V().has("name","pluto").next();
            //通过out得到住的地方的节点,在in得到所有链接到这个地方的节点,从而得到所有住在这个地方的节点  out 边出去的条件  in  边进来的条件

            GraphTraversal<Vertex,Vertex> liveInTartarusVertex = g.V(pluto).out("lives").in("lives");
            while(liveInTartarusVertex.hasNext()){
                Vertex vertex = liveInTartarusVertex.next();
                System.out.println("======"+vertex.value("name"));
            }



            GraphTraversal<Vertex,Vertex> liveInTartarusVertexNo = g.V(pluto).out("lives").in("lives").where(
                    __.is(P.neq(pluto)));
            while(liveInTartarusVertexNo.hasNext()){
                Vertex vertex = liveInTartarusVertexNo.next();
                System.out.println("======"+vertex.value("name"));
            }


            GraphTraversal<Vertex,Vertex> liveInTartarusVertexNot = g.V(pluto).as("x").out("lives").in("lives").where(P.neq(
                    "x"));
            while(liveInTartarusVertexNot.hasNext()){
                Vertex vertex = liveInTartarusVertexNot.next();
                System.out.println("======"+vertex.value("name"));
            }

            GraphTraversal<Vertex,Map<String, Vertex>> brothers = g.V(pluto).out("brother").as("god").out("lives").as("place").select("god","place");
            while(brothers.hasNext()){
                Map<String,Vertex> map = brothers.next();
                System.out.println("======"+map);
                for(Map.Entry<String,Vertex> entry:map.entrySet()){
                    System.out.println(entry.getKey()+" : "+entry.getValue().value("name"));
                }
            } 

            System.out.println("======"+g.V(pluto).outE("lives").next().value("reason"));


            /*GraphTraversal<Edge,Object> reasons = g.E().has("reason").values("reason").is(Text.textContains("loves"));
            System.out.println(reasons);
            while(reasons.hasNext()){
                Object e = reasons.next();
                System.out.println("======"+e);

            }*/

            GraphTraversal<Edge,Edge> reasons = g.E().has("reason").as("r").values("reason").is(Text.textContains("loves")).select("r");
            System.out.println(reasons);
            while(reasons.hasNext()){
                Edge e = reasons.next();
                System.out.println("======"+e.keys());
                System.out.println("======"+e.label());
                System.out.println("======"+e.value("reason"));
            }

            GraphTraversal<Edge,Map<String,Object>> reasons2 = g.E().has("reason").as("source").values("reason").is(Text.textContains("loves")).as("reason").select("source")
                    .outV().values("name").as("god").select("source").inV().values("name").as("thing").select("god","reason","thing");

            while(reasons2.hasNext()){
                Map<String,Object> map = reasons2.next();
                System.out.println("======"+map);
                for(Map.Entry<String,Object> entry:map.entrySet()){
                    System.out.println(entry.getKey()+" :  "+entry.getValue());
                }
            }
//            System.out.println("======"+);

//            System.out.println("======"+g.V(pluto).out("lives").in("lives"));
            graph.close();
        }
    }

调试的时候用了很多时间,主要问题体现在jar包依赖上。
需要特殊排除一些jar包,以下是maven的依赖

    <dependency>
            <groupId>com.thinkaurelius.titan</groupId>
            <artifactId>titan-core</artifactId>
            <version>${titan.version}</version>
        </dependency>
        <dependency>
            <groupId>com.thinkaurelius.titan</groupId>
            <artifactId>titan-hbase</artifactId>
            <version>${titan.version}</version>
        </dependency>
        <dependency>
            <groupId>com.thinkaurelius.titan</groupId>
            <artifactId>titan-es</artifactId>
            <version>${titan.version}</version>
        </dependency>
        <dependency>
            <groupId>com.thinkaurelius.titan</groupId>
            <artifactId>titan-hadoop</artifactId>
            <version>${titan.version}</version>
            <exclusions>
            <!--  这个jar包也是官网文件需要删除的 -->
                 <exclusion> 
                    <groupId>org.apache.hadoop</groupId>  
                    <artifactId>hadoop-core</artifactId>  
                </exclusion>  
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.tinkerpop</groupId>
            <artifactId>gremlin-driver</artifactId>
            <version>3.2.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>0.98.24-hadoop2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.thinkaurelius.titan/titan-berkeleyje -->
         <dependency>
            <groupId>com.thinkaurelius.titan</groupId>
            <artifactId>titan-berkeleyje</artifactId>
            <version>0.9.0-M2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.thinkaurelius.titan/titan-hadoop-core -->
     <dependency>
        <groupId>com.thinkaurelius.titan</groupId>
        <artifactId>titan-hadoop-core</artifactId>
        <version>1.0.0</version>
           <exclusions>
           <!--  这两个jar包无法下载 -->
                 <exclusion> 
                    <groupId>org.apache.httpcomponents</groupId>
                       <artifactId>httpcore</artifactId> 
                </exclusion>  
                <exclusion> 
                    <groupId>org.apache.httpcomponents</groupId>
                       <artifactId>httpclient</artifactId> 
                </exclusion> 
            </exclusions>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.thinkaurelius.titan/titan-hadoop -->
    <dependency>
        <groupId>com.thinkaurelius.titan</groupId>
        <artifactId>titan-hadoop</artifactId>
        <version>1.0.0</version>
        <type>pom</type>
    </dependency>

二、总结

人生苦短,我还是想选neo4j

 类似资料: