微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用节点js将数据保存在AWS neptune db中?

如何解决如何使用节点js将数据保存在AWS neptune db中?

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

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

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的顶点已经存在,则查询将返回该顶点,而不是创建一个新顶点。

此示例创建一个连接,该连接在Lambda容器的生存期内保持不变(而不是每次调用)。在网络故障的情况下,重试代码中会进行一些错误检查,以重新创建连接。

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

基于 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}),};
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。