阅读 421

JanusGraph入门第一课和官方文档踩坑

入门第一课是在IDEA里创建一个项目,有些小曲折。这里运行的Demo是读取janusgraph示例的“神之图”(Graph of the Gods)数据并打印,采用Hbase+ES组合,JanusGraph版本是0.4.0,诸位可参考以下开始janusgraph的调用之旅~

官网Java依赖

<dependency>
    <groupId>org.janusgraph</groupId>
    <artifactId>janusgraph-core</artifactId>
    <version>0.4.0</version>
</dependency>
<dependency>
    <groupId>org.apache.tinkerpop</groupId>
    <artifactId>gremlin-driver</artifactId>
    <version>3.4.1</version>
</dependency>
复制代码

Demo代码

2019-09-14补充一下【Demo源码】注:使用JanusGraph Embedded访问方式,也就是嵌入式

import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.janusgraph.core.JanusGraph;
import org.janusgraph.core.JanusGraphFactory;

import java.util.List;

public class JanusConnect {
    public static void main(String[] args) throws Exception {
    
        //graph可类比Spark里的SparkSession对象,一个统一访问的入口
        JanusGraph graph = JanusGraphFactory.build()
                .set("storage.backend", "hbase")
                .set("storage.hostname", "10.110.120.1,10.110.120.2,10.110.120.3")
                .set("index.search.backend", "elasticsearch")
                .set("storage.hbase.table", "testgraph") //表中已有数据
                .set("index.search.hostname", "10.110.120.1,10.110.120.2,10.110.120.3")
                .open();

        GraphTraversalSource g = graph.traversal();

        //【next()检索顶点,第一个action操作自动启动事务,有点类似Spark的Action算子的作用了】如果末尾不加next就添加不成功
//        g.addV("person").property("name", "John").next();
//        g.addV("person").property("name", "Snow").next();
//        g.addV("person").property("name", "Erya").next();
//        g.tx().commit();

        System.out.println("Vertex count = " + g.V().count().next());
        System.out.println("Edges count = " + g.E().count().next());


        System.out.println("++++++++图结构+++++++++" + graph.openManagement().printSchema());

        List<Vertex> ls = g.V().has("name").toList();


        for (Vertex v : ls) {
            System.out.println("标签是======" + v.label() + "======名字是:" + v.value("name"));
        }

        g.close();
        graph.close();
    }
}

复制代码

执行Demo报错

Exception in thread "main" java.lang.IllegalArgumentException: Could not find implementation class: org.janusgraph.diskstorage.hbase.HBaseStoreManager
Caused by: java.lang.ClassNotFoundException: org.janusgraph.diskstorage.hbase.HBaseStoreManager
复制代码

原因:

我的demo里的存储后端是HBase,因janusgraph-core缺少相应的类包导致!

解决办法:

pom.xml添加:

    <dependency>
        <groupId>org.janusgraph</groupId>
        <artifactId>janusgraph-all</artifactId>
        <version>0.4.0</version>
    </dependency>
复制代码

若执行一会儿后报错

Exception in thread "main" java.lang.IllegalArgumentException: Could not instantiate implementation: org.janusgraph.diskstorage.es.ElasticSearchIndex
Caused by: java.lang.ClassNotFoundException: org.apache.http.concurrent.FutureCallback
复制代码

原因及解决办法

索引后端是ES,缺少http相关类,继续在pom.xml文件中添加:

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5</version>
    </dependency>
复制代码

继续执行demo,已经可以成功运行。

部分查询输出:

标签是============monster========名字是:hydra
10:45:51.997 [main] DEBUG o.j.d.hbase.HBaseKeyColumnValueStore - Generated HBase Filter ColumnRangeFilter [$, %)
标签是============god========名字是:jupiter
10:45:52.001 [main] DEBUG o.j.d.hbase.HBaseKeyColumnValueStore - Generated HBase Filter ColumnRangeFilter [$, %)
标签是============demigod========名字是:hercules
10:45:52.005 [main] DEBUG o.j.d.hbase.HBaseKeyColumnValueStore - Generated HBase Filter ColumnRangeFilter [$, %)
标签是============god========名字是:pluto
10:45:52.011 [main] DEBUG o.j.d.hbase.HBaseKeyColumnValueStore - Generated HBase Filter ColumnRangeFilter [$, %)
标签是============monster========名字是:cerberus
复制代码

归档:【JanusGraph学习笔记】

问题笔记

JanusGraph学习笔记-问题与解决(总集)

关注下面的标签,发现更多相似文章
评论