[英]Send groovy script as a file to the Gremlin Server REST API
Here it is explained how I can interact with a Gremlin Server using its REST API. 此处说明了如何使用其REST API与Gremlin Server进行交互。 With the following command I execute the rather simple 100-1
script. 使用以下命令,我将执行非常简单的100-1
脚本。
curl -X POST -d "{\"gremlin\":\"100-1\"}" "http://localhost:8182"
What I want is to instead of using an inline script, define it in say, script.groovy
. 我想要的不是使用内联脚本,而是在script.groovy
定义它。
I can make it work for this case defining a variable with the entire script: 在这种情况下,我可以使用整个脚本定义一个变量:
GROOVY_LOAD_DATA_SCRIPT=$(<script.groovy)
curl -X POST -d "{\"gremlin\":\"${GROOVY_LOAD_DATA_SCRIPT}\"}" "http://localhost:8182"
But as soon as I start moving beyond one-liners that command breaks: 但是,一旦我开始超越单线,该命令就会中断:
{
"message": "body could not be parsed"
}
I created a file called send.groovy
that contains: 我创建了一个名为send.groovy
的文件,其中包含:
{
"gremlin": "x=1+1;x+3"
}
I then send it via curl with: 然后,我通过curl发送它:
$ curl -X POST --data-binary @send.groovy http://localhost:8182/gremlin
{"requestId":"6c0e7f3a-a16c-4fc1-a636-d462dc02b832","status":{"message":"","code":200,"attributes":{}},"result":{"data":[5],"meta":{}}}
If you want multi-line in the script itself, then you have encode the contents so that it remains valid JSON (ie change your line breaks to "\\n" since JSON doesn't allow line-breaks). 如果您希望脚本本身包含多行,那么您已经对内容进行了编码,以使其保持有效的JSON(即,由于JSON不允许换行,请将换行符更改为“ \\ n”)。
Note that you can use tools like Python to turn the file contents into valid JSON: 请注意,您可以使用Python之类的工具将文件内容转换为有效的JSON:
$ cat /tmp/foo
println "Hello " + 'World!'
1+2
$ echo "{\"gremlin\":$(python -c 'import json, sys; print(json.dumps(sys.stdin.read()))' < /tmp/foo)}"
{"gremlin":"println \"Hello \" + 'World!'\n1+2\n"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.