Skip to content

Commit

Permalink
Removed EmptyGraph usage in favor of AnonymousTraversalSource
Browse files Browse the repository at this point in the history
Prefer use of traversal() static method for remote usage #172
  • Loading branch information
spmallette committed Oct 18, 2023
1 parent 868f5c5 commit 2518c9a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
34 changes: 17 additions & 17 deletions book/Section-Introducing-Gremlin-Server.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -551,12 +551,13 @@ and retrieve the query results.
----
import org.apache.tinkerpop.gremlin.driver.Cluster;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph;
import org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection;
import org.apache.tinkerpop.gremlin.util.ser.GraphBinaryMessageSerializerV1;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal;
----

We can now define a small Java class that we will call 'RemoteClient' and setup the
Expand All @@ -582,35 +583,34 @@ public class RemoteClient
builder.serializer(new GraphBinaryMessageSerializerV1());
----

Once the Cluster.Builder instance has been setup we can use it to create our
Once the 'Cluster.Builder' instance has been setup we can use it to create our
'Cluster' instance.

[source,groovy]
----
Cluster cluster = builder.create();
----

Lastly, we need to setup a GraphTraversalSource object for the Gremlin Server hosted
graph that we will be working with. The TinkerPop documentation recommends that an
instance of an 'EmptyGraph' is used when creating the traversal. Having done that,
the 'withRemote' method can be called to establish the remote connection. Note that
the cluster instance that we just created is passed in as a parameter. While this
looks a little complicated it is really not a lot different than when we connect to a
local graph using the Gremlin Console. The only difference is that by setting up the
remote connection this way, when we start to issue queries against the graph, rather
than getting JSON objects back, the results will automatically be serialized into
Java variables for us. This makes our code a lot easier to write and essentially is
the same code from this point onwards that would also work with a local graph that we
are directly connected to.
Lastly, we need to setup a 'GraphTraversalSource' object for the Gremlin Server
hosted graph that we will be working with. This object is often named "g" by
convention and is typically created using the statically imported 'traversal()'
method which in turn allows the call to 'withRemote()' which is called to establish
the remote connection. Note that the cluster instance that we just created is passed
in as a parameter. While this looks a little complicated it is really not a lot
different than when we connect to a local graph using the Gremlin Console. The only
difference is that by setting up the remote connection this way, when we start to
issue queries against the graph, rather than getting JSON objects back, the results
will automatically be serialized into Java variables for us. This makes our code a
lot easier to write and essentially is the same code from this point onwards that
would also work with a local graph that we are directly connected to.

[source,groovy]
----
GraphTraversalSource g =
EmptyGraph.instance().traversal().
GraphTraversalSource g = traversal().
withRemote(DriverRemoteConnection.using(cluster));
----

We can now use our new graph traversal source object to issue a Gremlin query. The
We can now use our new 'GraphTraversalSource' object to issue a Gremlin query. The
results will be placed directly into the 'List' called 'vmaps'. The query finds the
first 10 airports with a region code of 'GB-ENG' which is short for Great Britain
- England.
Expand Down
6 changes: 3 additions & 3 deletions sample-code/RemoteClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

import org.apache.tinkerpop.gremlin.driver.Cluster;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph;
import org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection;
import org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal;

public class RemoteClient
{
public static void main( String[] args )
Expand All @@ -27,8 +28,7 @@ public static void main( String[] args )

Cluster cluster = builder.create();

GraphTraversalSource g =
EmptyGraph.instance().traversal().
GraphTraversalSource g = traversal().
withRemote(DriverRemoteConnection.using(cluster));

List <Map<Object,Object>> vmaps =
Expand Down

0 comments on commit 2518c9a

Please sign in to comment.