How can I remotely connect to a JanusGraph server?

别等时光非礼了梦想. 提交于 2019-11-29 04:29:31

The documentation I've found suggests to create an EmtpyGraph and get a remote traversal from that one:

EmptyGraph.instance().traversal().withRemote(config);

where config is your a configuration object with the remote properties, e.g:

    config.setProperty("clusterConfiguration.hosts", HOST);
    config.setProperty("clusterConfiguration.port", PORT);
    config.setProperty("clusterConfiguration.serializer.className", "org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0");
    config.setProperty("clusterConfiguration.serializer.config.ioRegistries", ioRegistries); // (e.g. [ org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry) ]
    config.setProperty("gremlin.remote.remoteConnectionClass", "org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection");
    config.setProperty("gremlin.remote.driver.sourceName", "g");

However, I've run into issues using JanusGraph specific features (for example committing a transaction) with this since the graph instance is the EmptyGraph itself, not a JanusGraph. So, try:

GraphTraversalSource g = JanusGraphFactory.open("inmemory").traversal().withRemote(config);

This will give you a traversal source to the remote gremlin-server, and you can do things like g.addV("vertexLabel")....; , g.tx().commit(); and so on.

Here is a simple way:

graph = JanusGraphFactory.open("conf/janusgraph-cassandra-solr.properties") juno = graph.addVertex() //Automatically opens a new transaction juno.property("name", "juno") graph.tx().commit() //Commits transaction

Janus User Doc

Janus uses an existing storage solution like cassandra, hbase, berkelydb to store data.

You can connect by 2 ways: 1 - Connect to the remote gremlin server and execute traversal/queries remotely. This is by using the Cluster and EmptyGraph of tinkerpop gremlin 2 - Connect directly from your application using the technique i suggested int his post above.

Pros/Cons of connecting to the remote gremlin server

Pros

  • The server has much more control and all the queries are centralized.
  • Since every one is running traversal/queries via the remote gremlin server, all are transactionally protected. The remote gremlin server runs your traversal/queries by default in a transaction.
  • Central strategy management
  • Central schema management

Cons

  • Tough to do a manual transaction management
  • You have to use groovy script as string and send it to remove (Cluster submit) for transactional execution of your code.

BY connecting directly from your client code (avoiding the remote connection) you get much more control.

Also, you can use the JanusGraph instance directly in your code, which is still gremlin complaint, to take full advantage of the JanusGraph API's.

Try this:

JanusGraphFactory.Builder builder = JanusGraphFactory.build().
            set("storage.hostname", "localhost").
            set('storage.backend', 'cassandra') //or whatever you are using as backend
builder.open();

Check RemoteGraph in janusgraph examples.

You can find under Class RemoteGraphApp how you can connect to remote janusgraph server (Cluster).

 conf = new PropertiesConfiguration(propFileName);

    // using the remote driver for schema
    try {
        cluster = Cluster.open(conf.getString("gremlin.remote.driver.clusterFile"));
        client = cluster.connect();
    } catch (Exception e) {
        throw new ConfigurationException(e);
    }

    // using the remote graph for queries
    graph = EmptyGraph.instance();
    g = graph.traversal().withRemote(conf);

where the cluster config file contains:

 hosts: [127.0.0.1]
 port: 8182
 serializer: {
   className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV3d0,
   config: {
    ioRegistries: [org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry]
   } 
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!