结果不能直接返回。但是,您有两种选择:
- 让 ArangoDB 将 Pregel 结果写入文档
- 不要存储结果,而只是暂时保存在内存中
为了避免持久化结果,有一个option store你必须设置为false.你可以通过access the volatile result with AQL函数PREGEL_RESULT(<handle>)。
流程是这样的:
- 开始 Pregel 执行
- 检查状态并等待状态变为“完成”或“已取消”
- 如果 Pregel 成功,则执行 AQL 查询以访问结果
var pregel = require("@arangodb/pregel");
var params = {source: "Country/Algeria", store: false};
var handle = pregel.start("sssp", "CountryGraph", params);
while (!["done", "canceled"].includes(pregel.status(handle).state)) {
print("waiting for result");
require("internal").wait(0.5); // TODO: make this more clever
}
var status = pregel.status(handle);
print(status);
if (status.state == "done") {
var query = db._query("FOR doc IN PREGEL_RESULT(@handle) RETURN doc", {handle: handle});
print(query.toArray());
}
输出如下:
shell>arangosh --javascript.execute pregel.js
waiting for result
{
"state" : "done",
"gss" : 2,
"totalRuntime" : 0.00583648681640625,
"aggregators" : {
},
"sendCount" : 1,
"receivedCount" : 1,
"vertexCount" : 10,
"edgeCount" : 8,
"code" : 200
}
[
{
"_key" : "Germany",
"result" : 9223372036854776000
},
{
"_key" : "Switzerland",
"result" : 9223372036854776000
},
{
"_key" : "Brasil",
"result" : 9223372036854776000
},
{
"_key" : "Marocco",
"result" : 9223372036854776000
},
{
"_key" : "Argentina",
"result" : 9223372036854776000
},
{
"_key" : "Austria",
"result" : 9223372036854776000
},
{
"_key" : "Algeria",
"result" : 0
},
{
"_key" : "Uruguay",
"result" : 9223372036854776000
},
{
"_key" : "Tunesia",
"result" : 1
},
{
"_key" : "Australia",
"result" : 9223372036854776000
}
]