Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remotely connect to a JanusGraph server?

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?

like image 328
Gao Avatar asked Aug 14 '17 11:08

Gao


3 Answers

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.

like image 174
Nischal Kumar Avatar answered Oct 22 '22 00:10

Nischal Kumar


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.

like image 21
Niklas Avatar answered Oct 21 '22 22:10

Niklas


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]
   } 
 }
like image 27
Ali Aboud Avatar answered Oct 21 '22 23:10

Ali Aboud