@@ -19,38 +19,46 @@ Licensed to the Apache Software Foundation (ASF) under one
1919
2020package examples ;
2121
22+ import org .apache .tinkerpop .gremlin .driver .Cluster ;
23+ import org .apache .tinkerpop .gremlin .driver .remote .DriverRemoteConnection ;
2224import org .apache .tinkerpop .gremlin .process .traversal .dsl .graph .GraphTraversalSource ;
23- import org .apache .tinkerpop .gremlin .structure .Graph ;
2425import org .apache .tinkerpop .gremlin .structure .Vertex ;
25- import org .apache .tinkerpop .gremlin .tinkergraph .structure .TinkerGraph ;
2626
2727import java .util .List ;
2828
2929import static org .apache .tinkerpop .gremlin .process .traversal .AnonymousTraversalSource .traversal ;
3030
3131public 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}
0 commit comments