【问题标题】:how to save data in aws neptune db using node js?如何使用节点 js 在 aws neptune db 中保存数据?
【发布时间】:2020-09-15 09:38:56
【问题描述】:

有没有办法使用节点 js 将数据保存在 amazon aws neptune db 中? 我在 lambda 上运行此代码。

我使用以下代码连接到海王星数据库。

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

【问题讨论】:

    标签: node.js gremlin amazon-neptune


    【解决方案1】:

    这是一个将数据写入 Neptune 的 JavaScript Lambda 函数(并在并发修改的情况下将写入包装在重试块中)。该函数从环境变量中获取 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';
    }
    

    【讨论】:

    • 我用过这个。现在我收到如下错误错误:找不到模块“异步”。我应该添加任何额外的进口吗?
    • 这在执行 npm install 后得到解决。您上面提到的以下代码没有返回任何内容。 return g.V(id) .fold() .coalesce( __.unfold(), __.addV('User').property(t.id, id) ) .id().next();你能帮我解决这个问题吗?
    • 使用 Node v12.18.2 我使用以下依赖项构建了我的示例:async 3.2.0、async-limiter 1.0.1、gremlin 3.4.7 和 ws 6.2.1。确保 Lambda 函数与您的 Neptune 集群在同一 VPC 中运行,并且具有可以访问 Neptune 的安全组。
    • 我想知道在哪里可以检查 aws 控制台中的上传,我在其中创建了一个新的笔记本来查看数据
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 2020-02-01
      • 2018-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多