【问题标题】:is there any way to connect amazon aws opensearch to nodejs?有什么方法可以将亚马逊 aws opensearch 连接到 nodejs?
【发布时间】:2022-02-06 22:43:53
【问题描述】:

嗨,我是 aws 的新手,不知道如何连接到 aws opensearch 服务以及如何使用 node 将数据放在那里,并在 opensearch 上创建了一个域,但想使用 node 放一些数据,请告诉我知道是否有人可以提供帮助 请以代码格式给出答案

【问题讨论】:

  • 我会先在搜索引擎中输入“Elasticsearch Nodejs”。

标签: node.js amazon-web-services elasticsearch aws-sdk-nodejs


【解决方案1】:

有多种方法可以将数据上传到 OpenSearch 服务,使用 Lambda 等服务、使用命令行或使用大多数编程语言开发服务。

使用 OpenSearch 服务,您可以向服务的域发送 HTTP 请求,因此像这样的 curl 请求对您有用:

curl -XPUT -u 'master-user:master-user-password' 'domain-endpoint/movies/_doc/1' -d '{"director": "Burton, Tim", "genre": ["Comedy","Sci-Fi"], "year": 1996, "actor": ["Jack Nicholson","Pierce Brosnan","Sarah Jessica Parker"], "title": "Mars Attacks!"}' -H 'Content-Type: application/json'

有关向 OpenSearch 发送 curl 请求的更多信息,请查看此doc

在 Node.js 中我们可以这样做:

var req = new AWS.HttpRequest(endpoint);

req.method = 'POST';
req.path = path.join('/', esDomain.index, esDomain.doctype);
req.region = esDomain.region;
req.headers['presigned-expires'] = false;
req.headers['Host'] = endpoint.host;
req.body = doc;

var signer = new AWS.Signers.V4(req , 'es');  // es: service code
signer.addAuthorization(creds, new Date());

var send = new AWS.NodeHttpClient();
send.handleRequest(req, null, function(httpResp) {
    var respBody = '';
    httpResp.on('data', function (chunk) {
        respBody += chunk;
    });
    httpResp.on('end', function (chunk) {
        console.log('Response: ' + respBody);
        context.succeed('Lambda added document ' + doc);
    });
}, function(err) {
    console.log('Error: ' + err);
    context.fail('Lambda failed with error ' + err);
});

更多示例可以在这里找到:aws-samples / amazon-elasticsearch-lambda-samples

【讨论】:

  • 按照您的代码后,我在输出中收到 400 BAD REQUEST
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
  • 1970-01-01
  • 2019-09-08
  • 2014-03-08
  • 1970-01-01
  • 2017-01-10
  • 1970-01-01
相关资源
最近更新 更多