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

前端 未结 2 1654
[愿得一人]
[愿得一人] 2021-01-23 09:38

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.

2条回答
  •  深忆病人
    2021-01-23 10:42

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

提交回复
热议问题