我正在尝试在nodejs中为cosmos db编写流畅的gremlin查询,即使它们作为字符串提交。我已经阅读了文档,并且在一些 github 线程中看到虽然尚不支持字节码,但可以将其作为脚本提交。
到目前为止我拥有的代码:
配置客户端功能:
export const CosmosConn = async (): Promise<driver.Client> => {
try {
const cosmosKey: string = await GetSecret('cosmos-key');
const cosmosEndpoint: string = await GetSecret('cosmos-endpoint');
const authenticator: driver.auth.PlainTextSaslAuthenticator = new gremlin.driver.auth.PlainTextSaslAuthenticator(
'/dbs/main/colls/main',
cosmosKey
);
const client: driver.Client = new gremlin.driver.Client(cosmosEndpoint, {
authenticator,
traversalsource: 'g',
rejectUnauthorized: true,
mimeType: 'application/vnd.gremlin-v2.0+json'
});
return client;
} catch (err) {
console.error(err);
}
};
现在下面这两个是临时的,因为我会为每个查询等待几次 CosmosConn,但这是针对 Azure 函数的,所以我还没有优化:
export const Graph = async (query: gremlin.process.Bytecode): Promise<any> => {
const db = await CosmosConn();
const translator = new gremlin.process.Translator(
new gremlin.process.AnonymousTraversalSource()
);
return db.submit(translator.translate(query));
};
export const getGremlin = async () => {
const db = await CosmosConn();
return gremlin.process.traversal().withRemote(db);
};
现在当我尝试使用它时:
const g = await getGremlin();
const query = g
.V()
.hasLabel('client')
.getBytecode();
const test = await Graph(query);
这当然会抛出一个错误:
Gremlin Query Syntax Error: Script compile error: Unexpected token: 'Object'; in input: '[objectObject'. @ line 1, column 9.
最佳答案
您是否尝试在提交之前打印translator.translate(query)
?
根据我的经验,翻译器对重要查询的支持非常有限。
据微软称,他们计划在 12 月 19 日支持 Fluent API,所以最好等待官方支持。
关于node.js - Azure Cosmos + Gremlin NodeJS,如何以脚本形式提交流畅的查询(不是字节码——我知道它还不支持),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58815569/