Skip to content

Commit cebd8e3

Browse files
Update Java GLV examples (#3295)
1 parent 274fc53 commit cebd8e3

6 files changed

Lines changed: 86 additions & 50 deletions

File tree

gremlin-driver/src/main/java/examples/BasicGremlin.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,46 @@ Licensed to the Apache Software Foundation (ASF) under one
1919

2020
package examples;
2121

22+
import org.apache.tinkerpop.gremlin.driver.Cluster;
23+
import org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection;
2224
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
23-
import org.apache.tinkerpop.gremlin.structure.Graph;
2425
import org.apache.tinkerpop.gremlin.structure.Vertex;
25-
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
2626

2727
import java.util.List;
2828

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

3131
public class BasicGremlin {
32-
public static void main(String[] args) {
33-
Graph graph = TinkerGraph.open();
34-
GraphTraversalSource g = traversal().withEmbedded(graph);
32+
static final String SERVER_HOST = System.getenv().getOrDefault("GREMLIN_SERVER_HOST", "localhost");
33+
static final int SERVER_PORT = Integer.parseInt(System.getenv().getOrDefault("GREMLIN_SERVER_PORT", "8182"));
34+
static final String VERTEX_LABEL = System.getenv().getOrDefault("VERTEX_LABEL", "person");
35+
36+
public static void main(String[] args) throws Exception {
37+
Cluster cluster = Cluster.build(SERVER_HOST).port(SERVER_PORT).create();
38+
GraphTraversalSource g = traversal().withRemote(DriverRemoteConnection.using(cluster, "g"));
3539

3640
// Basic Gremlin: adding and retrieving data
37-
Vertex v1 = g.addV("person").property("name","marko").next();
38-
Vertex v2 = g.addV("person").property("name","stephen").next();
39-
Vertex v3 = g.addV("person").property("name","vadas").next();
41+
Vertex v1 = g.addV(VERTEX_LABEL).property("name","marko").next();
42+
Vertex v2 = g.addV(VERTEX_LABEL).property("name","stephen").next();
43+
Vertex v3 = g.addV(VERTEX_LABEL).property("name","vadas").next();
4044

4145
// Be sure to use a terminating step like next() or iterate() so that the traversal "executes"
4246
// Iterate() does not return any data and is used to just generate side-effects (i.e. write data to the database)
4347
g.V(v1).addE("knows").to(v2).property("weight",0.75).iterate();
4448
g.V(v1).addE("knows").to(v3).property("weight",0.75).iterate();
4549

4650
// Retrieve the data from the "marko" vertex
47-
Object marko = g.V().has("person","name","marko").values("name").next();
51+
Object marko = g.V().has(VERTEX_LABEL,"name","marko").values("name").next();
4852
System.out.println("name: " + marko);
4953

5054
// Find the "marko" vertex and then traverse to the people he "knows" and return their data
51-
List<Object> peopleMarkoKnows = g.V().has("person","name","marko").out("knows").values("name").toList();
55+
List<Object> peopleMarkoKnows = g.V().has(VERTEX_LABEL,"name","marko").out("knows").values("name").toList();
5256
for (Object person : peopleMarkoKnows) {
5357
System.out.println("marko knows " + person);
5458
}
59+
60+
// Cleanup
61+
cluster.close();
62+
g.close();
5563
}
5664
}

gremlin-driver/src/main/java/examples/Connections.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ Licensed to the Apache Software Foundation (ASF) under one
3535
import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal;
3636

3737
public class Connections {
38+
static final String SERVER_HOST = System.getenv().getOrDefault("GREMLIN_SERVER_HOST", "localhost");
39+
static final int SERVER_PORT = Integer.parseInt(System.getenv().getOrDefault("GREMLIN_SERVER_PORT", "8182"));
40+
static final String VERTEX_LABEL = System.getenv().getOrDefault("VERTEX_LABEL", "connection");
41+
3842
public static void main(String[] args) throws Exception {
3943
withEmbedded();
4044
withRemote();
@@ -56,15 +60,12 @@ private static void withEmbedded() throws Exception {
5660

5761
// Connecting to the server
5862
private static void withRemote() throws Exception {
59-
Cluster cluster = Cluster.build("localhost").port(8182).create();
63+
Cluster cluster = Cluster.build(SERVER_HOST).port(SERVER_PORT).create();
6064
GraphTraversalSource g = traversal().withRemote(DriverRemoteConnection.using(cluster, "g"));
6165

62-
// Drop existing vertices
63-
g.V().drop().iterate();
64-
6566
// Simple query to verify connection
66-
g.addV().iterate();
67-
long count = g.V().count().next();
67+
g.addV(VERTEX_LABEL).iterate();
68+
long count = g.V().hasLabel(VERTEX_LABEL).count().next();
6869
System.out.println("Vertex count: " + count);
6970

7071
// Cleanup
@@ -75,12 +76,12 @@ private static void withRemote() throws Exception {
7576
// Connecting and customizing configurations with a cluster
7677
// See reference/#gremlin-java-configuration for full list of configurations
7778
private static void withCluster() throws Exception {
78-
Cluster cluster = Cluster.build("localhost").
79+
Cluster cluster = Cluster.build(SERVER_HOST).
7980
channelizer(Channelizer.WebSocketChannelizer.class).
8081
keepAliveInterval(180000).
8182
maxConnectionPoolSize(8).
8283
path("/gremlin").
83-
port(8182).
84+
port(SERVER_PORT).
8485
serializer(new GraphBinaryMessageSerializerV1()).
8586
create();
8687
GraphTraversalSource g = traversal().withRemote(DriverRemoteConnection.using(cluster, "g"));
@@ -98,7 +99,8 @@ private static void withSerializer() throws Exception {
9899
IoRegistry registry = new FakeIoRegistry(); // an IoRegistry instance exposed by a specific graph provider
99100
TypeSerializerRegistry typeSerializerRegistry = TypeSerializerRegistry.build().addRegistry(registry).create();
100101
MessageSerializer serializer = new GraphBinaryMessageSerializerV1(typeSerializerRegistry);
101-
Cluster cluster = Cluster.build("localhost").
102+
Cluster cluster = Cluster.build(SERVER_HOST).
103+
port(SERVER_PORT).
102104
serializer(serializer).
103105
create();
104106
Client client = cluster.connect();

gremlin-driver/src/main/java/examples/ModernTraversals.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ Licensed to the Apache Software Foundation (ASF) under one
1919

2020
package examples;
2121

22+
import org.apache.tinkerpop.gremlin.driver.Cluster;
23+
import org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection;
2224
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
2325
import org.apache.tinkerpop.gremlin.structure.Edge;
24-
import org.apache.tinkerpop.gremlin.structure.Graph;
2526
import org.apache.tinkerpop.gremlin.structure.Vertex;
26-
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory;
2727

2828
import java.util.List;
2929

@@ -33,10 +33,13 @@ Licensed to the Apache Software Foundation (ASF) under one
3333
import static org.apache.tinkerpop.gremlin.structure.T.id;
3434

3535
public class ModernTraversals {
36-
public static void main(String[] args) {
37-
// Performs basic traversals on the Modern toy graph which can be created using TinkerFactory
38-
Graph modern = TinkerFactory.createModern();
39-
GraphTraversalSource g = traversal().withEmbedded(modern);
36+
static final String SERVER_HOST = System.getenv().getOrDefault("GREMLIN_SERVER_HOST", "localhost");
37+
static final int SERVER_PORT = Integer.parseInt(System.getenv().getOrDefault("GREMLIN_SERVER_PORT", "8182"));
38+
39+
public static void main(String[] args) throws Exception {
40+
// Performs basic traversals on the Modern toy graph loaded on the server
41+
Cluster cluster = Cluster.build(SERVER_HOST).port(SERVER_PORT).create();
42+
GraphTraversalSource g = traversal().withRemote(DriverRemoteConnection.using(cluster, "g"));
4043

4144
List<Edge> e1 = g.V(1).bothE().toList(); // (1)
4245
List<Edge> e2 = g.V(1).bothE().where(otherV().hasId(2)).toList(); // (2)
@@ -54,6 +57,10 @@ public static void main(String[] args) {
5457
System.out.println("5: " + e5.toString());
5558
System.out.println("6: " + e6.toString());
5659

60+
// Cleanup
61+
cluster.close();
62+
g.close();
63+
5764
/*
5865
1. There are three edges from the vertex with the identifier of "1".
5966
2. Filter those three edges using the where()-step using the identifier of the vertex returned by otherV() to
@@ -69,3 +76,5 @@ steps of outE() and inV() since the schema allows it.
6976
*/
7077
}
7178
}
79+
80+

gremlin-examples/gremlin-java/BasicGremlin.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,46 @@ Licensed to the Apache Software Foundation (ASF) under one
1919

2020
package examples;
2121

22+
import org.apache.tinkerpop.gremlin.driver.Cluster;
23+
import org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection;
2224
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
23-
import org.apache.tinkerpop.gremlin.structure.Graph;
2425
import org.apache.tinkerpop.gremlin.structure.Vertex;
25-
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
2626

2727
import java.util.List;
2828

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

3131
public class BasicGremlin {
32-
public static void main(String[] args) {
33-
Graph graph = TinkerGraph.open();
34-
GraphTraversalSource g = traversal().withEmbedded(graph);
32+
static final String SERVER_HOST = "localhost";
33+
static final int SERVER_PORT = 8182;
34+
static final String VERTEX_LABEL = "person";
35+
36+
public static void main(String[] args) throws Exception {
37+
Cluster cluster = Cluster.build(SERVER_HOST).port(SERVER_PORT).create();
38+
GraphTraversalSource g = traversal().withRemote(DriverRemoteConnection.using(cluster, "g"));
3539

3640
// Basic Gremlin: adding and retrieving data
37-
Vertex v1 = g.addV("person").property("name","marko").next();
38-
Vertex v2 = g.addV("person").property("name","stephen").next();
39-
Vertex v3 = g.addV("person").property("name","vadas").next();
41+
Vertex v1 = g.addV(VERTEX_LABEL).property("name","marko").next();
42+
Vertex v2 = g.addV(VERTEX_LABEL).property("name","stephen").next();
43+
Vertex v3 = g.addV(VERTEX_LABEL).property("name","vadas").next();
4044

4145
// Be sure to use a terminating step like next() or iterate() so that the traversal "executes"
4246
// Iterate() does not return any data and is used to just generate side-effects (i.e. write data to the database)
4347
g.V(v1).addE("knows").to(v2).property("weight",0.75).iterate();
4448
g.V(v1).addE("knows").to(v3).property("weight",0.75).iterate();
4549

4650
// Retrieve the data from the "marko" vertex
47-
Object marko = g.V().has("person","name","marko").values("name").next();
51+
Object marko = g.V().has(VERTEX_LABEL,"name","marko").values("name").next();
4852
System.out.println("name: " + marko);
4953

5054
// Find the "marko" vertex and then traverse to the people he "knows" and return their data
51-
List<Object> peopleMarkoKnows = g.V().has("person","name","marko").out("knows").values("name").toList();
55+
List<Object> peopleMarkoKnows = g.V().has(VERTEX_LABEL,"name","marko").out("knows").values("name").toList();
5256
for (Object person : peopleMarkoKnows) {
5357
System.out.println("marko knows " + person);
5458
}
59+
60+
// Cleanup
61+
cluster.close();
62+
g.close();
5563
}
5664
}

gremlin-examples/gremlin-java/Connections.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ Licensed to the Apache Software Foundation (ASF) under one
3535
import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal;
3636

3737
public class Connections {
38+
static final String SERVER_HOST = "localhost";
39+
static final int SERVER_PORT = 8182;
40+
static final String VERTEX_LABEL = "connection";
41+
3842
public static void main(String[] args) throws Exception {
3943
withEmbedded();
4044
withRemote();
@@ -56,15 +60,12 @@ private static void withEmbedded() throws Exception {
5660

5761
// Connecting to the server
5862
private static void withRemote() throws Exception {
59-
Cluster cluster = Cluster.build("localhost").port(8182).create();
63+
Cluster cluster = Cluster.build(SERVER_HOST).port(SERVER_PORT).create();
6064
GraphTraversalSource g = traversal().withRemote(DriverRemoteConnection.using(cluster, "g"));
6165

62-
// Drop existing vertices
63-
g.V().drop().iterate();
64-
6566
// Simple query to verify connection
66-
g.addV().iterate();
67-
long count = g.V().count().next();
67+
g.addV(VERTEX_LABEL).iterate();
68+
long count = g.V().hasLabel(VERTEX_LABEL).count().next();
6869
System.out.println("Vertex count: " + count);
6970

7071
// Cleanup
@@ -75,12 +76,12 @@ private static void withRemote() throws Exception {
7576
// Connecting and customizing configurations with a cluster
7677
// See reference/#gremlin-java-configuration for full list of configurations
7778
private static void withCluster() throws Exception {
78-
Cluster cluster = Cluster.build("localhost").
79+
Cluster cluster = Cluster.build(SERVER_HOST).
7980
channelizer(Channelizer.WebSocketChannelizer.class).
8081
keepAliveInterval(180000).
8182
maxConnectionPoolSize(8).
8283
path("/gremlin").
83-
port(8182).
84+
port(SERVER_PORT).
8485
serializer(new GraphBinaryMessageSerializerV1()).
8586
create();
8687
GraphTraversalSource g = traversal().withRemote(DriverRemoteConnection.using(cluster, "g"));
@@ -98,7 +99,8 @@ private static void withSerializer() throws Exception {
9899
IoRegistry registry = new FakeIoRegistry(); // an IoRegistry instance exposed by a specific graph provider
99100
TypeSerializerRegistry typeSerializerRegistry = TypeSerializerRegistry.build().addRegistry(registry).create();
100101
MessageSerializer serializer = new GraphBinaryMessageSerializerV1(typeSerializerRegistry);
101-
Cluster cluster = Cluster.build("localhost").
102+
Cluster cluster = Cluster.build(SERVER_HOST).
103+
port(SERVER_PORT).
102104
serializer(serializer).
103105
create();
104106
Client client = cluster.connect();

gremlin-examples/gremlin-java/ModernTraversals.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ Licensed to the Apache Software Foundation (ASF) under one
1919

2020
package examples;
2121

22+
import org.apache.tinkerpop.gremlin.driver.Cluster;
23+
import org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection;
2224
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
2325
import org.apache.tinkerpop.gremlin.structure.Edge;
24-
import org.apache.tinkerpop.gremlin.structure.Graph;
2526
import org.apache.tinkerpop.gremlin.structure.Vertex;
26-
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerFactory;
2727

2828
import java.util.List;
2929

@@ -33,10 +33,13 @@ Licensed to the Apache Software Foundation (ASF) under one
3333
import static org.apache.tinkerpop.gremlin.structure.T.id;
3434

3535
public class ModernTraversals {
36-
public static void main(String[] args) {
37-
// Performs basic traversals on the Modern toy graph which can be created using TinkerFactory
38-
Graph modern = TinkerFactory.createModern();
39-
GraphTraversalSource g = traversal().withEmbedded(modern);
36+
static final String SERVER_HOST = "localhost";
37+
static final int SERVER_PORT = 8182;
38+
39+
public static void main(String[] args) throws Exception {
40+
// Performs basic traversals on the Modern toy graph loaded on the server
41+
Cluster cluster = Cluster.build(SERVER_HOST).port(SERVER_PORT).create();
42+
GraphTraversalSource g = traversal().withRemote(DriverRemoteConnection.using(cluster, "g"));
4043

4144
List<Edge> e1 = g.V(1).bothE().toList(); // (1)
4245
List<Edge> e2 = g.V(1).bothE().where(otherV().hasId(2)).toList(); // (2)
@@ -54,6 +57,10 @@ public static void main(String[] args) {
5457
System.out.println("5: " + e5.toString());
5558
System.out.println("6: " + e6.toString());
5659

60+
// Cleanup
61+
cluster.close();
62+
g.close();
63+
5764
/*
5865
1. There are three edges from the vertex with the identifier of "1".
5966
2. Filter those three edges using the where()-step using the identifier of the vertex returned by otherV() to

0 commit comments

Comments
 (0)