How can I remotely connect to a JanusGraph server?

Viewed 7533

I want to use Java API to manipulate graph on a remote server, the server actually hosts in localhost. The code I use to connect server is:

JanusGraphFactory.Builder b = JanusGraphFactory.build();
b.set("hosts", "[localhost]");
JanusGraph graph = b.open();

But after I run the program, it throws exception like this:

Exception in thread "main" java.lang.IllegalStateException: Need to set configuration value: root.storage.backend

So how can I connect to a remote JanusGraph server using Java API?

6 Answers

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.

Most of the answers above are outdated. right now , to connect to janusgraph or any tinkerpop complaint graph database over remote we need to make cluster object. Using this cluster object, we can get graphTraversalSource. Both these objects need to be closed when the programs ends to release the connection pool.

    private static Cluster cluster;
    private static GraphTraversalSource gts;

    private static void init() {
        cluster = Cluster.build()
                .addContactPoint(uri)
                .port(port)
                .serializer(Serializers.GRYO_V3D0)
                .maxInProcessPerConnection(32)
                .maxSimultaneousUsagePerConnection(32)
                .maxContentLength(10000000)
                .maxWaitForConnection(10)
                .minConnectionPoolSize(poolSize)
                .maxConnectionPoolSize(poolSize+5)
                .create();

        gts = AnonymousTraversalSource
                .traversal()
                .withRemote(DriverRemoteConnection.using(cluster));
    }


    public GraphTraversalSource getConnection() {
        return gts;
    }

    public static void finalise() throws Exception {
        gts.close();
        cluster.close();
    }

GraphTraversalSource is a thread safe object and should ideally be a singelton. Each graphTraversalSource object keeps its connection pool within its context.

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]
   } 
 }

Use the following code:

        JanusGraphFactory.Builder config = JanusGraphFactory.build();
        config.set("storage.backend", "cassandrathrift");
        config.set("storage.cassandra.keyspace", "graph1");
        config.set("storage.hostname", "127.0.0.1");

        JanusGraph graph = config.open();
Related