简体   繁体   中英

Azure Cosmos + Gremlin NodeJS, how to submit fluent query as script (not bytecode — i know its not supported yet)

I'm trying to write fluent gremlin queries in nodejs for cosmos db, even though they get submitted as string. I've been through the documentation and I've seen it mentioned in a few github threads that although bytecode isn't yet supported, it is possible to submit it as scrip.

The code I have so far:

Configuring the client function:


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);
    }
};

Now these two below are temporary as i'll await that CosmosConn several times for every query, but this is for an Azure Function so i'm not optimizing yet:

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);
};

Now when I try to use it:

    const g = await getGremlin();
        const query = g
            .V()
            .hasLabel('client')
            .getBytecode();

        const test = await Graph(query);

This of course throws out an error:

Gremlin Query Syntax Error: Script compile error: Unexpected token: 'Object'; in input: '[objectObject'. @ line 1, column 9.

Have you tried to print the translator.translate(query) prior submitting?

From my experience, the translator is very limited in its support for non-trivial queries.

According to Microsoft, they plan to support fluent API on Dec 19', so probably better to wait for official support.

It was the types preventing me from initialising my translator in a way that works with CosmosDB.

    const translator = new gremlin.process.Translator('g' as any);

works.

Dawn below is an example of using Translator in TypeScript to convert bytecode query to string query for CosmosDB. I don't recommend this solution, like the other response pointed out: it's limited. Use AWS Neptune instead or wait until MS implements bytecode queries in CosmosDB.

async function test(): Promise<void> {
   // Connection:
   const traversal = Gremlin.process.AnonymousTraversalSource.traversal;
   const DriverRemoteConnection = Gremlin.driver.DriverRemoteConnection;
   const g = traversal().withRemote(new DriverRemoteConnection("ws://localhost:8182/gremlin"));

   // Create translator
   const translator = new Gremlin.process.Translator(g);

   // Convert bytecode query to string query for CosmosDB:
   console.log(translator.translate(g.V().hasLabel('person').values('name').getBytecode()))
}

test();

Here is the link to tests cases to get getbytecode translation working.

https://github.com/apache/tinkerpop/blob/master/gremlin-javascript/src/main/javascript/gremlin-javascript/test/unit/translator-test.js#L31

Edit:-

Here is sample test case from above link

it('should produce valid script representation from bytecode glv steps', function () {
      const g = new graph.Graph().traversal();
      const script = new Translator('g').translate(g.V().out('created').getBytecode());
      assert.ok(script);
      assert.strictEqual(script, 'g.V().out(\'created\')');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM