how to save data in aws neptune db using node js?

旧时模样 提交于 2021-01-20 09:08:59

问题


Is there a way to save the data in amazon aws neptune db using node js? I am running this code on a lambda.

I made the connection to neptune db using the below code.

const gremlin = require('gremlin');

const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;

dc = new DriverRemoteConnection('endpoint',{});

const graph = new Graph();
const g = graph.traversal().withRemote(dc);

回答1:


Here's a JavaScript Lambda function that writes data to Neptune (and wraps the write in a retry block in case of concurrent modifications). The function gets the Neptune endpoint and port from environment variables. The write query is in the query() method. It's a simple upsert example that tries to create a vertex using a randomly generated ID. If a vertex with that ID already exists, the query returns that vertex rather than creating a new one.

This example creates a single connection that persists for the lifetime of the Lambda container (rather than per invocation). There's some error checking in the retry code that recreates the connection in the case of an untoward network issue.

const gremlin = require('gremlin');
const async = require('async');

const traversal = gremlin.process.AnonymousTraversalSource.traversal;
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;

let conn = createRemoteConnection();
let g = createGraphTraversalSource(conn);
const t = gremlin.process.t;
const __ = gremlin.process.statics;

async function query(id) {
    return g.V(id)
        .fold()
        .coalesce(
            __.unfold(), 
            __.addV('User').property(t.id, id)
        )
        .id().next();
}


exports.handler = async (event, context) => {

    const id = Math.floor(Math.random() * 10000).toString();
    
    return async.retry(
        { 
            times: 5,
            interval: 1000,
            errorFilter: function (err) { 
                
                // Add filters here to determine whether error can be retried
                console.warn('Determining whether retriable error: ' + err.message);
                
                // Check for connection issues
                if (err.message.startsWith('WebSocket is not open')){
                    console.warn('Reopening connection');
                    conn.close();
                    conn = createRemoteConnection();
                    g = createGraphTraversalSource(conn);
                    return true;
                }
                
                // Check for ConcurrentModificationException
                if (err.message.includes('ConcurrentModificationException')){
                    console.warn('Retrying query because of ConcurrentModificationException');
                    return true;
                }
                
                return false; 
            }
            
        }, 
        async function (cb) { 
            let result = await query(id); 
            return result['value'];
        });
};

function createRemoteConnection() {
        
    return new DriverRemoteConnection(
        connectionString(), 
        { 
            mimeType: 'application/vnd.gremlin-v2.0+json', 
            pingEnabled: false 
        });
}

function createGraphTraversalSource(conn) {
    return traversal().withRemote(conn);
}

function connectionString() {
    return 'wss://' + 
        process.env['neptuneEndpoint'] + 
        ':' + 
        process.env['neptunePort'] + 
        '/gremlin';
}



回答2:


Simple demo based off the TinkerPop documentation

const handler = async (event) => {
    // add person vertex with a property name and value stephen.
    await g.addV('person').property('name','stephen').next();

    // fetch all vertex' and get the name properties.
    const result = await g.V().values('name').toList();
    console.log(result);

    return {
        statusCode: 201,
        body: JSON.stringify({message:"Testing Gremlin!", data:result}),
    };
}


来源:https://stackoverflow.com/questions/63899124/how-to-save-data-in-aws-neptune-db-using-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!