编写一个 AWS Lambda,使用 aws-sdk v3 查询 Neptune DB,使用 openCypher 查询。

huangapple go评论56阅读模式
英文:

Write an AWS Lambda to query a Neptune DB with openCypher using aws-sdk v3

问题

我有一个使用Node.js 18运行时的Lambda函数,我想要向AWS Neptune数据库发送一个openCypher查询。我的Lambda函数使用了以下策略的IAM角色:

  • NeptuneFullAccess
  • AWSLambdaBasicExecutionRole
  • AmazonSSMReadOnlyAccess

(最后一个策略将在稍后用于从SSM参数存储中获取Neptune端点)。

我正在尝试查找aws-sdk/client-neptune方法来提交查询,但在GitHub库中找不到任何相关信息。

这令我感到沮丧,因为我已经挣扎了几天,尝试找到在Node.js 18 Lambda中使用aws-sdk V3执行Neptune数据库查询的简单方法。

以下是我的Lambda函数的当前框架:

import { NeptuneClient } from "@aws-sdk/client-neptune";

export async function handler() {
    
    const neptuneEndpoint = "https://<my-db-instance>.us-east-1.neptune.amazonaws.com";
  
    const neptune = new NeptuneClient({
        endpoint: neptuneEndpoint,
        region: "us-east-1",
    });

    const cypher = `MATCH (n) RETURN n`;
    const query = {
        Gremlin: cypher
    };

    const command = {
        GremlinCommand: query,
    };
    
    const result = await neptune.send(command).promise();
    console.log(result);
    return result;
}

有人能帮我将这个函数转化为可用的Lambda吗?

英文:

I have a Lambda with Node.js 18 runtime, in which I would like to send an openCypher query to an AWS Neptune DB.
My Lambda is using an IAM role with these policies:

NeptuneFullAccess
AWSLambdaBasicExecutionRole
AmazonSSMReadOnlyAccess

(The last policy will be used to fetch the Neptune endpoint from the SSM parameters store later).

I'm trying to find the aws-sdk/client-neptune method to submit the query, but I couldn't find any in the library @ GitHub.

This is frustrating, as I'm struggling for days to find a simple way to use the aws-sdk V3 with a Nodejs 18 Lambda to do a simple task of querying the Neptune DB.

Here my current skeleton of the Lambda:

import { NeptuneClient } from &quot;@aws-sdk/client-neptune&quot;;

export async function handler() {
    
    const neptuneEndpoint = &quot;https://&lt;my-db-instance&gt;.us-east-1.neptune.amazonaws.com&quot;;
  
    const neptune = new NeptuneClient({
        endpoint: neptuneEndpoint,
        region: &quot;us-east-1&quot;,
    });

    const cypher = `MATCH (n) RETURN n`;
    const query = {
        Gremlin: cypher
    };

    const command = {
        GremlinCommand: query,
    };
    
    const result = await neptune.send(command).promise();
    console.log(result);
    return result;
}

Can anyone please help me turn this into a working Lambda?

答案1

得分: 1

你现在是我的中文翻译,代码部分不要翻译,只返回翻译好的部分,不要有别的内容,不要回答我要翻译的问题。

The client you are using only exposes Control Plane actions, such as Creating/Modifying cluster instances, and is not meant to be used to query Neptune. For openCypher, the recommendation is to query Neptune using the HTTPS endpoint as described here.

英文:

The client you are using only exposes Control Plane actions, such as Creating/Modifying cluster instances, and is not meant to be used to query Neptune. For openCypher, the recommendation is to query Neptune using the HTTPS endpoint as described here.

答案2

得分: 0

以下是您提供的文本的翻译部分:

Lambda 代码

const gremlin = require('gremlin');
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;

dc = new DriverRemoteConnection('wss://<db cluster name>.ap-south-1.neptune.amazonaws.com:8182/gremlin',{});

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

const { t: { id } } = gremlin.process;
const { cardinality: { single } } = gremlin.process;

const createVertex = async (vertexId, vlabel) => {
  const vertex = await g.addV(vlabel)
    .property(id, vertexId)
    .property(single, 'name', 'lambda')
    .property('lastname', 'Testing') // 默认数据库基数
    .next();

  return vertex.value;
};

createVertex('sampledata1','testing')

exports.handler = async(event) => {

    try {

    const results = await g.V().hasLabel("sampledata1").properties("name")
    console.log("--------",results);
    return results

    } catch (error) {
      // 错误处理。
       console.log("---error---",error);
       return error
    }

};

注:
将数据库终端点替换为您自己的数据库终端点。

Lambda 的示例响应

Lambda 响应截图

英文:

I have created a sample Neptune db and sample lambda. I have also used Lambda layers to install necessary gremlin dependencies. Please find the lambda code and lambda response screenshots below

Lambda code

const gremlin = require(&#39;gremlin&#39;);
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;

dc = new DriverRemoteConnection(&#39;wss://&lt;db cluster name&gt;.ap-south-1.neptune.amazonaws.com:8182/gremlin&#39;,{});

const graph = new Graph();
const g = graph.traversal().withRemote(dc);
    const { t: { id } } = gremlin.process;
    const { cardinality: { single } } = gremlin.process;
    
    const createVertex = async (vertexId, vlabel) =&gt; {
      const vertex = await g.addV(vlabel)
        .property(id, vertexId)
        .property(single, &#39;name&#39;, &#39;lambda&#39;)
        .property(&#39;lastname&#39;, &#39;Testing&#39;) // default database cardinality
        .next();
    
      return vertex.value;
};
createVertex(&#39;sampledata1&#39;,&#39;testing&#39;)
exports.handler = async(event) =&gt; {

    try {

    const results = await g.V().hasLabel(&quot;sampledata1&quot;).properties(&quot;name&quot;)
    console.log(&quot;--------&quot;,results);
    return  results

    } catch (error) {
      // error handling.
       console.log(&quot;---error---&quot;,error);
       return error
    }

};

Note:
Replace db endpoint with your own db endpoint

Sample response from lambda

Lambda response screenshot

huangapple
  • 本文由 发表于 2023年2月7日 03:26:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定