【问题标题】:Is there a good way to run a single gremlin query from the command line?有没有从命令行运行单个 gremlin 查询的好方法?
【发布时间】:2021-01-31 06:48:58
【问题描述】:

我想从命令行针对特定服务器运行单个 gremlin 查询。 (来自我的 unix shell,而不是 Gremlin 控制台内部)

我正在寻找类似于 postgres 的 psql(例如 psql -c 'select * from "foo"')的东西。

速写

这是我想要以交互方式使用Gremlin Console 执行的示例:

gremlin> cluster = Cluster.build('my-db-host').create()
==>my-db-host/127.0.0.1:8182
gremlin> :remote connect tinkerpop.server cluster
==>Configured my-db-host/127.0.0.1:8182
gremlin> :remote console
==>All scripts will now be sent to Gremlin Server - [my-db-host/127.0.0.1:8182] - type ':remote console' to return to local mode
gremlin> g.V().count()
==>1

Gremlin 控制台-e?

希望缩短这个时间,我首先查看的是 Gremlin Console 的 -e,您可以在其中提供一个 groovy 脚本来运行。

我从上面的命令开始,但很快意识到-e下的“:remote”命令don't work

然后我开始构建java client code 以从 groovy 脚本的 args[0] 中运行查询并打印结果,但这似乎比需要的复杂得多。

我是否遗漏了一些明显的东西?相当于psql -c 的生态系统在哪里?

是否有任何第三方客户可以提供我所追求的东西?

更新:

我继续沿着 gremlin 控制台路径,并把它拼凑在一起 (gist):

query.groovy:

println "Server: "+args[0]

cluster = Cluster.build(args[0]).create()
client = cluster.connect()

println "Query: "+args[1]

r = client.submit(args[1]).toList()

println "Result:"

r.each { println it }

gremlin-query.sh:

#! /usr/bin/env bash
SERVER=$1
QUERY=$2
if [ "$#" -ne 2 ]; then
    echo "Usage:"
    echo "./gremlin-query.sh localhost 'g.V().elementMap()'"
    exit 1
fi
./apache-tinkerpop-gremlin-console-3.4.10/bin/gremlin.sh -e query.groovy "$SERVER" "$QUERY"

输出如下所示:

> ./gremlin-query.sh localhost "g.V().elementMap()"
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass (file:/path/to/apache-tinkerpop-gremlin-console-3.4.10/lib/groovy-2.5.14-indy.jar) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Server: localhost
Query: g.V().elementMap()
Result:
result{object={id=0, label=foobar} class=java.util.LinkedHashMap}
>

我不会将此称为运行单个查询的好方法,所以我将保留这个问题,希望将来会出现更像psql 的东西。

【问题讨论】:

    标签: gremlin tinkerpop


    【解决方案1】:

    正如您所发现的,您在使用控制台命令时不能使用-e,但您可以使用-i 进行一些技巧。通常您使用-i 将控制台初始化为某种状态(例如,建立您的远程连接,以便在您登录时您已准备好开始编写将转到服务器的 Gremlin)。 -i 命令虽然不会从您的命令中写入输出。当然,您可以将脚本写入明确的println() 结果,然后像-e 那样自动退出。例如try.groovy:

    :remote connect tinkerpop.server conf/remote.yaml
    :> g.V().count()
    println result
    :x
    

    产生这个输出:

    $ bin/gremlin.sh -i try.groovy
    
             \,,,/
             (o o)
    -----oOOo-(3)-oOOo-----
    plugin activated: tinkerpop.server
    plugin activated: tinkerpop.utilities
    plugin activated: tinkerpop.neo4j
    plugin activated: tinkerpop.tinkergraph
    [result{object=6 class=java.lang.String}]
    

    这对于您的用例可能已经足够了。除此之外,我认为你会坚持使用 -e 并直接使用 Cluster 对象。

    【讨论】:

    • 感谢您的完整性检查。我继续沿着-e / Cluster 路径前进,并将其添加到上面的问题中。您认为 TinkerPop 将来会提供更密集的 CLI 工具吗?
    • 这是我所知道的第一个问题。我认为如果提出这个想法,社区会接受这个想法。
    猜你喜欢
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-21
    • 1970-01-01
    相关资源
    最近更新 更多