This is an automated email from the ASF dual-hosted git repository. vgalaxies pushed a commit to branch fix-gremlin in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph-doc.git
commit e61b12b5c44da7706ce73838d694fdc75a82351f Author: VGalaxies <[email protected]> AuthorDate: Fri Apr 12 19:46:10 2024 +0800 improve --- content/cn/docs/clients/gremlin-console.md | 86 ++++++------------------------ 1 file changed, 17 insertions(+), 69 deletions(-) diff --git a/content/cn/docs/clients/gremlin-console.md b/content/cn/docs/clients/gremlin-console.md index 4a9992e0..bf4ebd69 100644 --- a/content/cn/docs/clients/gremlin-console.md +++ b/content/cn/docs/clients/gremlin-console.md @@ -9,74 +9,20 @@ Gremlin-Console 是由 Tinkerpop 自己开发的一个交互式客户端,用 - 单机离线调用模式 - Client/Server 请求模式 -### 1 单机离线调用模式 +**注:Gremlin-Console 只是便于用户快速上手体验,不建议在生产环境中使用。** -由于 lib 目录下已经包含了 HugeCore 的 jar 包,且 HugeGraph 已经作为插件注册到 Console 中,用户可以直接写 groovy 脚本调用 HugeGraph-Core 的代码,然后交由 Gremlin-Console 内的解析引擎执行,就能在不启动 Server 的情况下操作图。 +### 1 单机离线调用模式 -这种模式便于用户快速上手体验,但是不适合大量数据插入和查询的场景。下面给一个示例: +由于 lib 目录下已经包含了 HugeCore 的 jar 包,且 HugeGraph-Server 已经作为插件注册到 Gremlin-Console 中,用户可以直接写 Groovy 脚本调用 HugeGraph-Core 的代码,然后交由 Gremlin-Console 内的解析引擎执行,就能在**不启动** Server 的情况下操作图。 -在 script 目录下有一个示例脚本 `example.groovy`: +这里提供一个示例,首先修改 `hugegraph.properties` 配置使用 Memory 后端 (使用其他后端可能会出现一些初始化问题): -```groovy -import org.apache.hugegraph.HugeFactory -import org.apache.hugegraph.backend.id.IdGenerator -import org.apache.hugegraph.dist.RegisterUtil -import org.apache.hugegraph.type.define.NodeRole -import org.apache.tinkerpop.gremlin.structure.T - -RegisterUtil.registerRocksDB() - -conf = "conf/graphs/hugegraph.properties" -graph = HugeFactory.open(conf) -graph.serverStarted(IdGenerator.of("server-tinkerpop"), NodeRole.MASTER) -schema = graph.schema() - -schema.propertyKey("name").asText().ifNotExist().create() -schema.propertyKey("age").asInt().ifNotExist().create() -schema.propertyKey("city").asText().ifNotExist().create() -schema.propertyKey("weight").asDouble().ifNotExist().create() -schema.propertyKey("lang").asText().ifNotExist().create() -schema.propertyKey("date").asText().ifNotExist().create() -schema.propertyKey("price").asInt().ifNotExist().create() - -schema.vertexLabel("person").properties("name", "age", "city").primaryKeys("name").ifNotExist().create() -schema.vertexLabel("software").properties("name", "lang", "price").primaryKeys("name").ifNotExist().create() -schema.indexLabel("personByCity").onV("person").by("city").secondary().ifNotExist().create() -schema.indexLabel("personByAgeAndCity").onV("person").by("age", "city").secondary().ifNotExist().create() -schema.indexLabel("softwareByPrice").onV("software").by("price").range().ifNotExist().create() -schema.edgeLabel("knows").sourceLabel("person").targetLabel("person").properties("date", "weight").ifNotExist().create() -schema.edgeLabel("created").sourceLabel("person").targetLabel("software").properties("date", "weight").ifNotExist().create() -schema.indexLabel("createdByDate").onE("created").by("date").secondary().ifNotExist().create() -schema.indexLabel("createdByWeight").onE("created").by("weight").range().ifNotExist().create() -schema.indexLabel("knowsByWeight").onE("knows").by("weight").range().ifNotExist().create() - -marko = graph.addVertex(T.label, "person", "name", "marko", "age", 29, "city", "Beijing") -vadas = graph.addVertex(T.label, "person", "name", "vadas", "age", 27, "city", "Hongkong") -lop = graph.addVertex(T.label, "software", "name", "lop", "lang", "java", "price", 328) -josh = graph.addVertex(T.label, "person", "name", "josh", "age", 32, "city", "Beijing") -ripple = graph.addVertex(T.label, "software", "name", "ripple", "lang", "java", "price", 199) -peter = graph.addVertex(T.label, "person", "name", "peter", "age", 35, "city", "Shanghai") - -marko.addEdge("knows", vadas, "date", "20160110", "weight", 0.5) -marko.addEdge("knows", josh, "date", "20130220", "weight", 1.0) -marko.addEdge("created", lop, "date", "20171210", "weight", 0.4) -josh.addEdge("created", lop, "date", "20091111", "weight", 0.4) -josh.addEdge("created", ripple, "date", "20171210", "weight", 1.0) -peter.addEdge("created", lop, "date", "20170324", "weight", 0.2) - -graph.tx().commit() - -g = graph.traversal() - -System.out.println(">>>> query all vertices: size=" + g.V().toList().size()) -System.out.println(">>>> query all edges: size=" + g.E().toList().size()) +```properties +backend=memory +serializer=text ``` -其实这一段 groovy 脚本几乎就是 Java 代码,不同之处仅在于变量的定义可以不写类型声明,以及每一行末尾的分号可以去掉。 - -> `g.V()` 是获取所有的顶点,`g.E()` 是获取所有的边,`toList()` 是把结果存到一个 List 中,参考 [TinkerPop Terminal Steps](http://tinkerpop.apache.org/docs/current/reference/#terminal-steps)。 - -下面进入 gremlin-console,并传入该脚本令其执行: +然后输入下述命令: ```bash > ./bin/gremlin-console.sh -- -i scripts/example.groovy @@ -97,7 +43,9 @@ gremlin> > 这里的 `--` 会被 getopts 解析为最后一个 option,这样后面的 options 就可以传入 Gremlin-Console > 进行处理了。`-i` 代表 `Execute the specified script and leave the console open on > completion`,更多的选项可以参考 Gremlin-Console > 的[源代码](https://github.com/apache/tinkerpop/blob/3.5.1/gremlin-console/src/main/groovy/org/apache/tinkerpop/gremlin/console/Console.groovy#L483)。 -可以看到,插入了 6 个顶点、6 条边,并查询出来了。进入 console 之后,还可继续输入 groovy 语句对图做操作: +其中 [`example.groovy`](https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-server/hugegraph-dist/src/assembly/static/scripts/example.groovy) 是 scripts 目录下的一个示例脚本,该脚本插入了一些数据,并在最后查询图中顶点和边的数量。 + +此时还可以继续输入 Gremlin 语句对图进行操作: ```groovy gremlin> g.V() @@ -123,7 +71,7 @@ gremlin> 因为 Gremlin-Console 只能通过 WebSocket 连接 HugeGraph-Server,默认 HugeGraph-Server 是对外提供 HTTP 连接的,所以先修改 gremlin-server 的配置。 -*注意:将连接方式修改为 WebSocket 后,HugeGraph-Client、HugeGraph-Loader、HugeGraph-Hubble 等配套工具都不能使用了。* +**注意:将连接方式修改为 WebSocket 后,HugeGraph-Client、HugeGraph-Loader、HugeGraph-Hubble 等配套工具都不能使用了。** ```yaml # vim conf/gremlin-server.yaml @@ -134,9 +82,9 @@ channelizer: org.apache.tinkerpop.gremlin.server.channel.HttpChannelizer # ...... ``` -将 `channelizer: org.apache.tinkerpop.gremlin.server.channel.HttpChannelizer` 修改成 `channelizer: org.apache.tinkerpop.gremlin.server.channel.WebSocketChannelizer` 或直接注释,然后按照[步骤](/docs/quickstart/hugegraph-server/)启动 HugegraphServer。 +将 `channelizer: org.apache.tinkerpop.gremlin.server.channel.HttpChannelizer` 修改成 `channelizer: org.apache.tinkerpop.gremlin.server.channel.WebSocketChannelizer` 或直接注释,然后按照[步骤](/docs/quickstart/hugegraph-server/)启动 HugeGraph-Server。 -下面进入 gremlin-console: +下面进入 Gremlin-Console: ```bash > ./bin/gremlin-console.sh @@ -150,7 +98,7 @@ plugin activated: tinkerpop.utilities plugin activated: tinkerpop.tinkergraph ``` -连接 server,需在配置文件中指定连接参数,在 conf 目录下有一个默认的 `remote.yaml`: +连接 Server,需在配置文件中指定连接参数,在 conf 目录下有一个默认的 `remote.yaml`: ```yaml # cat conf/remote.yaml @@ -170,7 +118,7 @@ gremlin> :remote connect tinkerpop.server conf/remote.yaml ==>Configured localhost/127.0.0.1:8182 ``` -连接成功之后,如果在启动 HugeGraphServer 的过程中导入了示例图 `example.groovy`,就可以在 console 中直接进行查询 +连接成功之后,如果在启动 HugeGraph-Server 的过程中导入了示例图,就可以在 Gremlin-Console 中直接进行查询: ```groovy gremlin> :> hugegraph.traversal().V() @@ -202,4 +150,4 @@ gremlin> :> @script gremlin> ``` -更多关于 gremlin-console 的使用,请参考 [Tinkerpop 官网](http://tinkerpop.apache.org/docs/current/reference/) +更多关于 Gremlin-Console 的使用,请参考 [Tinkerpop 官网](http://tinkerpop.apache.org/docs/current/reference/)。
