从Java连接
虽然可以将JanusGraph作为库嵌入到Java应用程序中并直接连接到后端,但本节假设应用程序连接到JanusGraph服务器。有关如何嵌入JanusGraph的信息,请参阅JanusGraph示例项目。
本节仅介绍应用程序如何使用GraphBinary序列化连接到JanusGraph服务器。有关Gremlin的介绍和进一步资源的指引,请参阅Gremlin Query Language。
JanusGraph 和 Gremlin-Java 入门指南
开始使用JanusGraph的Java版本:
-
使用Maven创建应用:
mvn archetype:generate -DgroupId=com.mycompany.project -DartifactId=gremlin-example -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -
在依赖管理器中添加对
janusgraph-driver和gremlin-driver的依赖:org.janusgraph janusgraph-driver 1.1.0 org.apache.tinkerpop gremlin-driver 3.7.3 implementation "org.janusgraph:janusgraph-driver:1.1.0" implementation "org.apache.tinkerpop:gremlin-driver:3.7.3" -
添加两个配置文件,
conf/remote-graph.properties和conf/remote-objects.yaml:gremlin.remote.remoteConnectionClass=org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection gremlin.remote.driver.clusterFile=conf/remote-objects.yaml gremlin.remote.driver.sourceName=ghosts: [localhost] port: 8182 serializer: { className: org.apache.tinkerpop.gremlin.util.ser.GraphBinaryMessageSerializerV1, config: { ioRegistries: [org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry] }} -
创建一个
GraphTraversalSource,这是所有Gremlin遍历的基础:import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal; GraphTraversalSource g = traversal().withRemote("conf/remote-graph.properties"); // 在整个应用程序中重用'g' // 并在关闭时通过g.close()关闭打开的连接 -
执行一个简单的遍历:
Object herculesAge = g.V().has("name", "hercules").values("age").next(); System.out.println("Hercules is " + herculesAge + " years old.");next()是一个终端步骤,它将遍历提交到Gremlin服务器并返回单个结果。
JanusGraph 特定类型和谓词
JanusGraph特定类型和predicates可以通过依赖项janusgraph-driver直接从Java应用程序中使用。
访问管理API的注意事项
注意
我们正在努力用一种语言无关的解决方案来替代管理API,请参见管理系统
描述的连接使用GraphBinary和janusgraph-driver,它不允许访问内部JanusGraph组件,例如ManagementSystem。要访问ManagementSystem,您必须提交基于Java的脚本,请参阅Submitting Scripts,或者通过本地打开JanusGraph实例直接访问JanusGraph。