JanusGraph Java API 远程增删改查图形数据

原创 janusgraph

JanusGraph 是一个开源的分布式图数据库。它具有很好的扩展性,通过多机集群可支持存储和查询数百亿的顶点和边的图数据。JanusGraph 是一个事务数据库,支持大量用户高并发地执行复杂的实时图遍历。

代码说明

分享的测试代码演示了如何使用 Java API 远程连接 JanusGraph 集群,单元测试代码提供了插入图形数据包括顶点和关系的功能,统计数据和查询数据的 API 使用。

集群使用 Hadoop 环境,存储端使用了 HBase,索引端 ElasticSearch。

API 连接代码:

JanusGraphFactory.Builder builder = JanusGraphFactory.build()
        .set("storage.hostname", "Hadoop0, Hadoop1, Hadoop2")
        .set("storage.backend", "hbase")
        .set("storage.hbase.table", "janusgraph")
        .set("index.search.backend", "elasticsearch")
        .set("index.search.hostname", "Hadoop0");
JanusGraph graph = builder.open();
boolean open = graph.isOpen();
System.out.println("is open:" + open);

创建连接后最好判断以下连接状态。

不论是查询还是插入数据,需要开启 JanusGraph 事务,才能保证运行状况和预期的一致。

JanusGraphTransaction tx = graph.newTransaction();
// ...
tx.commit();
tx.close();

工程说明

使用 maven 构建的单元测试。

janus-p1.jpg

运行截图

跑了一个遍历图数据的接口示例。

janus-r1.jpg

希望对大家有帮助。

文件名:janusgraph-test.zip,文件大小:052.00 KB
  • /janusgraph-test/
    • /janusgraph-test/janusgraph-test
      • /janusgraph-test/janusgraph-test/.idea
        • /janusgraph-test/janusgraph-test/.idea/thriftCompiler.xml
        • /janusgraph-test/janusgraph-test/.idea/libraries
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_codehaus_groovy_groovy_json_indy_2_4_15.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_slf4j_slf4j_api_1_7_12.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__com_codahale_metrics_metrics_graphite_3_0_1.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_mindrot_jbcrypt_0_4.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_apache_ivy_ivy_2_3_0.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_codehaus_groovy_groovy_templates_2_4_15.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__com_codahale_metrics_metrics_core_3_0_1.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_apache_tinkerpop_gremlin_groovy_3_3_3.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_slf4j_jcl_over_slf4j_1_7_21.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_apache_tinkerpop_gremlin_shaded_3_3_3.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_apache_httpcomponents_httpcore_nio_4_4_5.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__com_carrotsearch_hppc_0_7_1.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__com_jcabi_jcabi_manifests_1_1.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_yaml_snakeyaml_1_15.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_janusgraph_janusgraph_core_0_3_1.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__log4j_log4j_1_2_17.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__com_github_ben_manes_caffeine_caffeine_2_3_1.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_locationtech_spatial4j_spatial4j_0_7.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_glassfish_javax_json_1_0.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_noggit_noggit_0_6.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_codehaus_groovy_groovy_groovysh_indy_2_4_15.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__com_jcabi_jcabi_log_0_14.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_elasticsearch_client_elasticsearch_rest_client_6_0_1.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_javatuples_javatuples_1_2.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_apache_commons_commons_lang3_3_3_1.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__commons_io_commons_io_2_3.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_antlr_stringtemplate_3_2.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_apache_tinkerpop_gremlin_server_3_3_3.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__jline_jline_2_12.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__xml_apis_xml_apis_1_0_b2.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__com_github_stephenc_high_scale_lib_high_scale_lib_1_1_4.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__info_ganglia_gmetric4j_gmetric4j_1_0_3.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_apache_tinkerpop_gremlin_core_3_3_3.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__commons_configuration_commons_configuration_1_10.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__com_google_guava_guava_18_0.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__junit_junit_4_11.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_codehaus_groovy_groovy_console_2_4_15.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_codehaus_groovy_groovy_sql_indy_2_4_15.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__commons_lang_commons_lang_2_6.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__com_squareup_javapoet_1_8_0.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_apache_htrace_htrace_core_3_1_0_incubating.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__commons_collections_commons_collections_3_2_2.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_codehaus_groovy_groovy_indy_2_4_15.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__commons_codec_commons_codec_1_7.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_apache_httpcomponents_httpasyncclient_4_1_2.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_codehaus_groovy_groovy_jsr223_indy_2_4_15.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__commons_logging_commons_logging_1_2.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__com_codahale_metrics_metrics_ganglia_3_0_1.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_javassist_javassist_3_16_1_GA.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__com_github_stephenc_findbugs_findbugs_annotations_1_3_9_1.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_apache_httpcomponents_httpcore_4_4_5.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_codehaus_groovy_groovy_xml_2_4_15.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_codehaus_groovy_groovy_swing_2_4_15.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_janusgraph_janusgraph_es_0_3_1.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__net_objecthunter_exp4j_0_4_8.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_apache_tinkerpop_tinkergraph_gremlin_3_3_3.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_codehaus_groovy_groovy_2_4_15.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__ch_qos_logback_logback_classic_1_1_2.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_reflections_reflections_0_9_9_RC1.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__com_google_code_findbugs_jsr305_3_0_0.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_apache_commons_commons_text_1_0.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__ch_qos_logback_logback_core_1_1_2.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_apache_httpcomponents_httpclient_4_5_2.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__dom4j_dom4j_1_6_1.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_apache_tinkerpop_gremlin_driver_3_3_3.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_antlr_antlr_runtime_3_2.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_slf4j_slf4j_log4j12_1_6_1.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_locationtech_jts_jts_core_1_15_0.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__antlr_antlr_2_7_7.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_apache_hbase_hbase_shaded_client_1_2_6.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__org_janusgraph_janusgraph_hbase_0_3_1.xml
          • /janusgraph-test/janusgraph-test/.idea/libraries/Maven__io_netty_netty_all_4_0_56_Final.xml
          • /janusgraph-test/janusgraph-test/.idea/workspace.xml
          • /janusgraph-test/janusgraph-test/.idea/vcs.xml
          • /janusgraph-test/janusgraph-test/.idea/compiler.xml
          • /janusgraph-test/janusgraph-test/.idea/inspectionProfiles
            • /janusgraph-test/janusgraph-test/.idea/inspectionProfiles/Project_Default.xml
            • /janusgraph-test/janusgraph-test/.idea/checkstyle-idea.xml
            • /janusgraph-test/janusgraph-test/.idea/eclipseCodeFormatter.xml
            • /janusgraph-test/janusgraph-test/.idea/misc.xml
            • /janusgraph-test/janusgraph-test/.idea/modules.xml
            • /janusgraph-test/janusgraph-test/.idea/encodings.xml
            • /janusgraph-test/janusgraph-test/src
              • /janusgraph-test/janusgraph-test/src/main
                • /janusgraph-test/janusgraph-test/src/main/java
如果觉得这对你有用,请随意赞赏,给与作者支持
评论 2
最新评论
#1 楼 buhui 2019-11-19

大神 给一份代码啊

#2 楼 vaja 2019-05-30

代码呢,没贴全吧