英文:
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 "@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;
}
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 的示例响应
英文:
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('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') // default database cardinality
.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) {
// error handling.
console.log("---error---",error);
return error
}
};
Note:
Replace db endpoint with your own db endpoint
Sample response from lambda
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论