从 Java 连接
虽然可以将 JanusGraph 作为库嵌入到 Java 应用程序中,然后直接连接到后端,但本节假设应用程序连接到 JanusGraph Server。有关如何嵌入 JanusGraph 的信息,请参阅 JanusGraph 示例项目。
本节仅介绍应用程序如何使用 GraphBinary 序列化连接到 JanusGraph Server。有关 Gremlin 的介绍和进一步资源的指针,请参阅 Gremlin 查询语言。
JanusGraph 和 Gremlin-Java 入门
Java 中 JanusGraph 入门
-
使用 Maven 创建应用程序
mvn archetype:generate -DgroupId=com.mycompany.project -DartifactId=gremlin-example -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -
在依赖管理器中添加对
janusgraph-driver和gremlin-driver的依赖<dependency> <groupId>org.janusgraph</groupId> <artifactId>janusgraph-driver</artifactId> <version>1.1.0</version> </dependency> <dependency> <groupId>org.apache.tinkerpop</groupId> <artifactId>gremlin-driver</artifactId> <version>3.7.3</version> </dependency>implementation "org.janusgraph:janusgraph-driver:1.1.0" implementation "org.apache.tinkerpop:gremlin-driver:3.7.3" -
添加两个配置文件,
conf/remote-graph.properties和conf/remote-objects.yamlgremlin.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"); // Reuse 'g' across the application // and close it on shut-down to close open connections with g.close() -
执行一个简单的遍历
Object herculesAge = g.V().has("name", "hercules").values("age").next(); System.out.println("Hercules is " + herculesAge + " years old.");next()是一个终止步骤,它将遍历提交到 Gremlin Server 并返回单个结果。
JanusGraph 特定类型和谓词
JanusGraph 特定类型和 谓词 可以通过依赖 janusgraph-driver 直接从 Java 应用程序中使用。
访问管理 API 的注意事项
注意
我们正在努力用一种与语言无关的解决方案取代管理 API,请参阅 管理系统
所描述的连接使用 GraphBinary 和 janusgraph-driver,这不允许访问内部 JanusGraph 组件,例如 ManagementSystem。要访问 ManagementSystem,您必须提交基于 Java 的脚本,请参阅 提交脚本,或通过本地打开 JanusGraph 实例直接访问 JanusGraph。