Skip to content

Version 1.0 of the documentation is no longer actively maintained. The site that you are currently viewing is an archived snapshot. For up-to-date documentation, see the latest version.

Gremlin-Console

Gremlin-Console is an interactive client developed by TinkerPop. Users can use this client to perform various operations on Graph. There are two main usage modes:

  • Stand-alone offline mode;
  • Client/Server mode;

1 Stand-alone offline mode

Since the lib directory already contains the HugeCore jar package, and HugeGraph has been registered in the Console as a plug-in, the users can write a groovy script directly to call the code of HugeGraph-Core, and then hand it over to the parsing engine in Gremlin-Console for execution. As a result, the users can operate the graph without starting the Server.

This mode is convenient for users to get started quickly, but it is not suitable for scenarios where a large amount of data is inserted and queried. Here is an example:

There is a sample script in the script directory:example.groovy

 1import com.baidu.hugegraph.HugeFactory
 2import com.baidu.hugegraph.dist.RegisterUtil
 3import org.apache.tinkerpop.gremlin.structure.T
 4
 5RegisterUtil.registerCassandra();
 6RegisterUtil.registerScyllaDB();
 7
 8conf = "conf/hugegraph.properties"
 9graph = HugeFactory.open(conf);
10schema = graph.schema();
11
12schema.propertyKey("name").asText().ifNotExist().create();
13schema.propertyKey("age").asInt().ifNotExist().create();
14schema.propertyKey("city").asText().ifNotExist().create();
15schema.propertyKey("weight").asDouble().ifNotExist().create();
16schema.propertyKey("lang").asText().ifNotExist().create();
17schema.propertyKey("date").asText().ifNotExist().create();
18schema.propertyKey("price").asInt().ifNotExist().create();
19
20schema.vertexLabel("person").properties("name", "age", "city").primaryKeys("name").ifNotExist().create();
21schema.vertexLabel("software").properties("name", "lang", "price").primaryKeys("name").ifNotExist().create();
22schema.indexLabel("personByName").onV("person").by("name").secondary().ifNotExist().create();
23schema.indexLabel("personByCity").onV("person").by("city").secondary().ifNotExist().create();
24schema.indexLabel("personByAgeAndCity").onV("person").by("age", "city").secondary().ifNotExist().create();
25schema.indexLabel("softwareByPrice").onV("software").by("price").range().ifNotExist().create();
26schema.edgeLabel("knows").sourceLabel("person").targetLabel("person").properties("date", "weight").ifNotExist().create();
27schema.edgeLabel("created").sourceLabel("person").targetLabel("software").properties("date", "weight").ifNotExist().create();
28schema.indexLabel("createdByDate").onE("created").by("date").secondary().ifNotExist().create();
29schema.indexLabel("createdByWeight").onE("created").by("weight").range().ifNotExist().create();
30schema.indexLabel("knowsByWeight").onE("knows").by("weight").range().ifNotExist().create();
31
32marko = graph.addVertex(T.label, "person", "name", "marko", "age", 29, "city", "Beijing");
33vadas = graph.addVertex(T.label, "person", "name", "vadas", "age", 27, "city", "Hongkong");
34lop = graph.addVertex(T.label, "software", "name", "lop", "lang", "java", "price", 328);
35josh = graph.addVertex(T.label, "person", "name", "josh", "age", 32, "city", "Beijing");
36ripple = graph.addVertex(T.label, "software", "name", "ripple", "lang", "java", "price", 199);
37peter = graph.addVertex(T.label, "person", "name", "peter", "age", 35, "city", "Shanghai");
38
39marko.addEdge("knows", vadas, "date", "20160110", "weight", 0.5);
40marko.addEdge("knows", josh, "date", "20130220", "weight", 1.0);
41marko.addEdge("created", lop, "date", "20171210", "weight", 0.4);
42josh.addEdge("created", lop, "date", "20091111", "weight", 0.4);
43josh.addEdge("created", ripple, "date", "20171210", "weight", 1.0);
44peter.addEdge("created", lop, "date", "20170324", "weight", 0.2);
45
46graph.tx().commit();
47
48g = graph.traversal();
49
50System.out.println(">>>> query all vertices: size=" + g.V().toList().size());
51System.out.println(">>>> query all edges: size=" + g.E().toList().size());

In fact, this groovy script is almost Java code, the only difference is that the variable definition can be written without the type declaration, and the semicolon at the end of each line can be removed.

g.V() is to get all the vertices, g.E() is to get all the edges, toList() is to store the result in a List, refer toTinkerPop Terminal Steps。

Enter the gremlin-console below and pass in the script to execute it:

 1bin/gremlin-console.sh scripts/example.groovy
 2objc[5038]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java (0x10137a4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x102bbb4e0). One of the two will be used. Which one is undefined.
 3
 4         \,,,/
 5         (o o)
 6-----oOOo-(3)-oOOo-----
 7plugin activated: com.baidu.hugegraph
 8plugin activated: tinkerpop.server
 9plugin activated: tinkerpop.utilities
10plugin activated: tinkerpop.tinkergraph
112018-01-15 14:36:19 7516  [main] [WARN ] com.baidu.hugegraph.config.HugeConfig [] - The config option 'rocksdb.data_path' is redundant, please ensure it has been registered
122018-01-15 14:36:19 7523  [main] [WARN ] com.baidu.hugegraph.config.HugeConfig [] - The config option 'rocksdb.wal_path' is redundant, please ensure it has been registered
132018-01-15 14:36:19 7604  [main] [INFO ] com.baidu.hugegraph.HugeGraph [] - Opening backend store 'cassandra' for graph 'hugegraph'
14>>>> query all vertices: size=6
15>>>> query all edges: size=6

As you can see, 6 vertices and 6 edges are inserted and queried. After entering the console, you can continue to enter groovy statements to operate on the graph:

 1gremlin> g.V()
 2==>v[2:ripple]
 3==>v[1:vadas]
 4==>v[1:peter]
 5==>v[1:josh]
 6==>v[1:marko]
 7==>v[2:lop]
 8gremlin> g.E()
 9==>e[S1:josh>2>>S2:ripple][1:josh-created->2:ripple]
10==>e[S1:marko>1>20160110>S1:vadas][1:marko-knows->1:vadas]
11==>e[S1:peter>2>>S2:lop][1:peter-created->2:lop]
12==>e[S1:josh>2>>S2:lop][1:josh-created->2:lop]
13==>e[S1:marko>1>20130220>S1:josh][1:marko-knows->1:josh]
14==>e[S1:marko>2>>S2:lop][1:marko-created->2:lop]

For more Gremlin statements, please refer to Tinkerpop Official Website

2 Client/Server mode

Because Gremlin-Console can only connect to HugeGraph-Server through WebSocket, HugeGraph-Server provides HTTP connections by default, so modify the configuration of gremlin-server first.

Note: After changing the connection method to WebSocket, HugeGraph-Client, HugeGraph-Loader, HugeGraph-Studio and other supporting tools cannot be used.

 1# vim conf/gremlin-server.yaml
 2host: 127.0.0.1
 3port: 8182
 4scriptEvaluationTimeout: 30000
 5# If you want to start gremlin-server for gremlin-console(web-socket),
 6# please change `HttpChannelizer` to `WebSocketChannelizer` or comment this line.
 7channelizer: org.apache.tinkerpop.gremlin.server.channel.HttpChannelizer
 8graphs: {
 9  hugegraph: conf/hugegraph.properties,
10  hugegraph1: conf/hugegraph1.properties
11}
12plugins:
13  - com.baidu.hugegraph
14scriptEngines: {
15  gremlin-groovy: {
16    imports: [java.lang.Math],
17    staticImports: [java.lang.Math.PI],
18    scripts: [scripts/empty-sample.groovy]
19  }
20}
21serializers:
22  - { className: org.apache.tinkerpop.gremlin.driver.ser.GryoLiteMessageSerializerV1d0,
23      config: {
24        serializeResultToString: false,
25        ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry]
26      }
27    }
28  - { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0,
29      config: {
30        serializeResultToString: true,
31        ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry]
32      }
33    }
34  - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV1d0,
35      config: {
36        serializeResultToString: false,
37        ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry]
38      }
39    }
40  - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV2d0,
41      config: {
42        serializeResultToString: false,
43        ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry]
44      }
45    }
46  - { className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV1d0,
47      config: {
48        serializeResultToString: false,
49        ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry]
50      }
51    }
52metrics: {
53  consoleReporter: {enabled: false, interval: 180000},
54  csvReporter: {enabled: true, interval: 180000, fileName: /tmp/gremlin-server-metrics.csv},
55  jmxReporter: {enabled: false},
56  slf4jReporter: {enabled: false, interval: 180000},
57  gangliaReporter: {enabled: false, interval: 180000, addressingMode: MULTICAST},
58  graphiteReporter: {enabled: false, interval: 180000}
59}
60maxInitialLineLength: 4096
61maxHeaderSize: 8192
62maxChunkSize: 8192
63maxContentLength: 65536
64maxAccumulationBufferComponents: 1024
65resultIterationBatchSize: 64
66writeBufferLowWaterMark: 32768
67writeBufferHighWaterMark: 65536
68ssl: {
69  enabled: false
70}

Modify channelizer: org.apache.tinkerpop.gremlin.server.channel.HttpChannelizer to channelizer: org.apache.tinkerpop.gremlin.server.channel.WebSocketChannelizer or comment directly, and then follow the steps to start the Server.

Then enter gremlin-console

 1bin/gremlin-console.sh 
 2objc[5761]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java (0x10ec584c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10ecdc4e0). One of the two will be used. Which one is undefined.
 3
 4         \,,,/
 5         (o o)
 6-----oOOo-(3)-oOOo-----
 7plugin activated: com.baidu.hugegraph
 8plugin activated: tinkerpop.server
 9plugin activated: tinkerpop.utilities
10plugin activated: tinkerpop.tinkergraph

To connect to the server, you need to specify the connection parameters in the configuration file, and there is a default remote.yaml file in the conf directory

 1# cat conf/remote.yaml
 2hosts: [localhost]
 3port: 8182
 4serializer: {
 5  className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0,
 6  config: {
 7    serializeResultToString: true,
 8    ioRegistries: [com.baidu.hugegraph.io.HugeGraphIoRegistry]
 9  }
10}
1gremlin> :remote connect tinkerpop.server conf/remote.yaml
22018-01-15 15:30:31 11528 [main] [INFO ] org.apache.tinkerpop.gremlin.driver.Connection [] - Created new connection for ws://localhost:8182/gremlin
32018-01-15 15:30:31 11538 [main] [INFO ] org.apache.tinkerpop.gremlin.driver.Connection [] - Created new connection for ws://localhost:8182/gremlin
42018-01-15 15:30:31 11538 [main] [INFO ] org.apache.tinkerpop.gremlin.driver.ConnectionPool [] - Opening connection pool on Host{address=localhost/127.0.0.1:8182, hostUri=ws://localhost:8182/gremlin} with core size of 2
5==>Configured localhost/127.0.0.1:8182

After the connection is successful, the only variables that can be used in the context of the console are two graph objects, hugegraph and hugegraph1 (configured in gremlin-server.yaml), and if you want to have more variables, you can add them in scripts/empty-sample.groovy, such as:

 1import org.apache.tinkerpop.gremlin.server.util.LifeCycleHook
 2
 3// an init script that returns a Map allows explicit setting of global bindings.
 4def globals = [:]
 5
 6// defines a sample LifeCycleHook that prints some output to the Gremlin Server console.
 7// note that the name of the key in the "global" map is unimportant.
 8globals << [hook: [
 9        onStartUp : { ctx ->
10            ctx.logger.info("Executed once at startup of Gremlin Server.")
11        },
12        onShutDown: { ctx ->
13            ctx.logger.info("Executed once at shutdown of Gremlin Server.")
14        }
15] as LifeCycleHook]
16
17// define schema manger for hugegraph
18schema = hugegraph.schema()
19// define the default TraversalSource to bind queries to - this one will be named "g".
20g = hugegraph.traversal()

In this way, the two objects schema and g can be directly used in the console for metadata management and graph query.

It doesn’t matter if it is not defined, because all objects are available through the graph, for example:

1gremlin> :> hugegraph.traversal().V()
2==>v[2:ripple]
3==>v[1:vadas]
4==>v[1:peter]
5==>v[1:josh]
6==>v[1:marko]
7==>v[2:lop]

In Client/Server mode, all operations related to Server must be added with :> , if not, it means local operation in the console.

You can also put multiple statements in a string variable and send them to the server at once:

 1gremlin> script = """
 2graph = hugegraph;
 3marko = graph.addVertex(T.label, "person", "name", "marko", "age", 29, "city", "Beijing");
 4vadas = graph.addVertex(T.label, "person", "name", "vadas", "age", 27, "city", "Hongkong");
 5lop = graph.addVertex(T.label, "software", "name", "lop", "lang", "java", "price", 328);
 6josh = graph.addVertex(T.label, "person", "name", "josh", "age", 32, "city", "Beijing");
 7ripple = graph.addVertex(T.label, "software", "name", "ripple", "lang", "java", "price", 199);
 8peter = graph.addVertex(T.label, "person", "name", "peter", "age", 35, "city", "Shanghai");
 9
10marko.addEdge("knows", vadas, "date", "20160110", "weight", 0.5);
11marko.addEdge("knows", josh, "date", "20130220", "weight", 1.0);
12marko.addEdge("created", lop, "date", "20171210", "weight", 0.4);
13josh.addEdge("created", lop, "date", "20091111", "weight", 0.4);
14josh.addEdge("created", ripple, "date", "20171210", "weight", 1.0);
15peter.addEdge("created", lop, "date", "20170324", "weight", 0.2);
16
17graph.tx().commit();
18
19g = graph.traversal();
20g.V().toList().size();
21"""
22
23gremlin> :> @script
24==>6

For more information on the use of gremlin-console, please refer to Tinkerpop Official Website