How can I remotely connect to a JanusGraph server?
Asked Answered
D

6

14

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?

Desertion answered 14/8, 2017 at 11:48 Comment(0)
G
9

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.

Gillie answered 17/8, 2017 at 11:27 Comment(3)
Can you give an example on how to pass the values for ioRegistries?Downright
basically there is a bug and ioRegistries value doesn't work if you provide it with a List of only 1 String, I'm currently working with a workarround with Arrays.asList("org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry","org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry"), but i did not tested fully this.Polygraph
how can I connect to particular graph, which is already available in server?Selves
M
5

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.

Maurinemaurise answered 28/10, 2017 at 3:16 Comment(3)
How would one go about doing "manual transaction management" in a "remote gremlin server and execute traversal/queries remotely way"? I can't seem to find any example anywhere. Would you please kindly show an example? thanksDumbstruck
Link is dead (404)Jacob
Hello, can you provide an example of how you make transaction with remote JanusGraph if you have, please, can't make work properly rollback on remoteCentaur
R
5

Most of the answers are now outdated.

To remote connect any TinkerPop3 graph database, we need to make cluster object.
Using this cluster object, we can get graphTraversalSource.
To release the connection pool, both objects need to close when the program ends.

    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 singleton.
Each graphTraversalSource object keeps its connection pool within its context.

Rumpus answered 25/9, 2020 at 13:5 Comment(1)
As of 0.6.2, I had to pass a full fledged object to the .serializer(...) argument like this: MessageSerializer<?> serializer = Serializers.GRAPHBINARY_V1D0.simpleInstance(); serializer.configure(Map.of("ioRegistries", List.of("org.janusgraph.graphdb.tinkerpop.JanusGraphIoRegistry")), Map.of());Forland
B
2

Try this:

JanusGraphFactory.Builder builder = JanusGraphFactory.build().
            set("storage.hostname", "localhost").
            set('storage.backend', 'cassandra') //or whatever you are using as backend
builder.open();
Behoove answered 14/8, 2017 at 12:51 Comment(3)
How can I connect to it using gremlin's standard API?Desertion
For that you need to configure your remote.yaml file and provide the following: hosts: [server-address] port: [port] serializer: { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0, config: { serializeResultToString: true }} Then you can use following code to connect: import org.apache.tinkerpop.gremlin.driver.Client; import org.apache.tinkerpop.gremlin.driver.Cluster; Cluster cluster = Cluster.open("conf/remote.yaml"); Client client = cluster.connect(); client.submit("graph.addVertex(T.label,'x','name','tom')");Behoove
Giving below error. Gremlin Server must be configured to use the JanusGraphManager.Selves
S
1

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]
   } 
 }
Sailboat answered 11/3, 2019 at 10:29 Comment(0)
I
0

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();
Impecunious answered 18/2, 2020 at 6:3 Comment(2)
Giving below error. Gremlin Server must be configured to use the JanusGraphManager.Selves
check the versions of janusgraph on your local pom.xml and on the remote server. Both should have the same versionImpecunious

© 2022 - 2024 — McMap. All rights reserved.