Gremlin-Console
Gremlin-Console 是由 Tinkerpop 自己开发的一个交互式客户端,用户可以使用该客户端对 Graph 做各种操作,主要有两种使用模式:
- 单机离线调用模式
- Client/Server 请求模式
注:Gremlin-Console 只是便于用户快速上手体验,不建议在生产环境中使用。
1 单机离线调用模式
由于 lib 目录下已经包含了 HugeCore 的 jar 包,且 HugeGraph-Server 已经作为插件注册到 Gremlin-Console 中,用户可以直接写 Groovy 脚本调用 HugeGraph-Core 的代码,然后交由 Gremlin-Console 内的解析引擎执行,就能在不启动 Server 的情况下操作图。
这里提供一个示例,首先修改 hugegraph.properties 配置使用 Memory 后端 (使用其他后端可能会出现一些初始化问题):
1. backend=memory
2. serializer=text
然后输入下述命令:
1. > ./bin/gremlin-console.sh -- -i scripts/example.groovy
3. \,,,/
4. (o o)
5. -----oOOo-(3)-oOOo-----
6. plugin activated: HugeGraph
7. plugin activated: tinkerpop.server
8. plugin activated: tinkerpop.utilities
9. plugin activated: tinkerpop.tinkergraph
10. main dict load finished, time elapsed 644 ms
11. model load finished, time elapsed 35 ms.
12. >>>> query all vertices: size=6
13. >>>> query all edges: size=6
14. gremlin>
这里的
--会被 getopts 解析为最后一个 option,这样后面的 options 就可以传入 Gremlin-Console 进行处理了。-i代表Execute the specified script and leave the console open on completion,更多的选项可以参考 Gremlin-Console 的源代码。
其中 example.groovy 是 scripts 目录下的一个示例脚本,该脚本插入了一些数据,并在最后查询图中顶点和边的数量。
此时还可以继续输入 Gremlin 语句对图进行操作:
1. gremlin> g.V()
2. ==>v[2:lop]
3. ==>v[1:josh]
4. ==>v[1:marko]
5. ==>v[1:peter]
6. ==>v[1:vadas]
7. ==>v[2:ripple]
8. gremlin> g.E()
9. ==>e[S1:josh>2>>S2:lop][1:josh-created->2:lop]
10. ==>e[S1:josh>2>>S2:ripple][1:josh-created->2:ripple]
11. ==>e[S1:marko>1>>S1:josh][1:marko-knows->1:josh]
12. ==>e[S1:marko>1>>S1:vadas][1:marko-knows->1:vadas]
13. ==>e[S1:marko>2>>S2:lop][1:marko-created->2:lop]
14. ==>e[S1:peter>2>>S2:lop][1:peter-created->2:lop]
15. gremlin>
更多的 Gremlin 语句请参考 Tinkerpop 官网。
2 Client/Server 请求模式
因为 Gremlin-Console 只能通过 WebSocket 连接 HugeGraph-Server,默认 HugeGraph-Server 是对外提供 HTTP 连接的,所以先修改 gremlin-server 的配置。
注意:将连接方式修改为 WebSocket 后,HugeGraph-Client、HugeGraph-Loader、HugeGraph-Hubble 等配套工具都不能使用了。
1. # vim conf/gremlin-server.yaml
2. # ......
3. # If you want to start gremlin-server for gremlin-console (web-socket),
4. # please change `HttpChannelizer` to `WebSocketChannelizer` or comment this line.
5. channelizer: org.apache.tinkerpop.gremlin.server.channel.HttpChannelizer
6. # ......
将 channelizer: org.apache.tinkerpop.gremlin.server.channel.HttpChannelizer 修改成 channelizer: org.apache.tinkerpop.gremlin.server.channel.WebSocketChannelizer 或直接注释,然后按照步骤启动 HugeGraph-Server。
下面进入 Gremlin-Console:
1. > ./bin/gremlin-console.sh
3. \,,,/
4. (o o)
5. -----oOOo-(3)-oOOo-----
6. plugin activated: HugeGraph
7. plugin activated: tinkerpop.server
8. plugin activated: tinkerpop.utilities
9. plugin activated: tinkerpop.tinkergraph
连接 Server,需在配置文件中指定连接参数,在 conf 目录下有一个默认的 remote.yaml:
1. # cat conf/remote.yaml
2. hosts: [localhost]
3. port: 8182
4. serializer: {
5. className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV1d0,
6. config: {
7. serializeResultToString: false,
8. ioRegistries: [org.apache.hugegraph.io.HugeGraphIoRegistry]
9. }
10. }
1. gremlin> :remote connect tinkerpop.server conf/remote.yaml
2. ==>Configured localhost/127.0.0.1:8182
连接成功之后,如果在启动 HugeGraph-Server 的过程中导入了示例图,就可以在 Gremlin-Console 中直接进行查询:
1. gremlin> :> hugegraph.traversal().V()
2. ==>[id:2:lop,label:software,type:vertex,properties:[name:lop,lang:java,price:328]]
3. ==>[id:1:josh,label:person,type:vertex,properties:[name:josh,age:32,city:Beijing]]
4. ==>[id:1:marko,label:person,type:vertex,properties:[name:marko,age:29,city:Beijing]]
5. ==>[id:1:peter,label:person,type:vertex,properties:[name:peter,age:35,city:Shanghai]]
6. ==>[id:1:vadas,label:person,type:vertex,properties:[name:vadas,age:27,city:Hongkong]]
7. ==>[id:2:ripple,label:software,type:vertex,properties:[name:ripple,lang:java,price:199]]
注意:在 Client/Server 模式下,所有和 Server 有关的操作都要加上
:>,如果不加,表示在 console 本地操作。
还可以把多条语句放在一个字符串变量中,然后一次性发给 Server:
1. gremlin> script = """
2. ......1> graph = hugegraph;
3. ......2> g = graph.traversal();
4. ......3> g.V().toList().size();
5. ......4> """
6. ==>
7. graph = hugegraph;
8. g = graph.traversal();
9. g.V().toList().size();
11. gremlin> :> @script
12. ==>6
13. gremlin>
更多关于 Gremlin-Console 的使用,请参考 Tinkerpop 官网。
Last modified April 15, 2024: chore: improve docs for gremlin console (#354) (a72b7ece)
