如何使用节点js将数据保存在AWS neptune db中? [英] how to save data in aws neptune db using node js?

查看:47
本文介绍了如何使用节点js将数据保存在AWS neptune db中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以使用Node JS将数据保存在Amazon AWS Neptune db中?我正在lambda上运行此代码.

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

我使用以下代码建立了与neptune db的连接.

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

const gremlin = require('gremlin');

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

推荐答案

这是一个JavaScript Lambda函数,可将数据写入Neptune(并发修改时将其包装在retry块中).该函数从环境变量获取Neptune端点和端口.写查询在 query()方法中.这是一个简单的upsert示例,尝试使用随机生成的ID创建顶点.如果具有该ID的顶点已经存在,则查询将返回该顶点,而不是创建一个新顶点.

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.

此示例创建一个单一连接,该连接在Lambda容器的生存期内保持不变(而不是每次调用).如果网络出现问题,重试代码中会进行一些错误检查,这些错误会重新创建连接.

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

这篇关于如何使用节点js将数据保存在AWS neptune db中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆