![](/img/trans.png)
[英]How do I access data from ElasticSearch in AWS without Kibana?
[英]How to format a timestamp from DynamoDB for Elasticsearch/Kibana?
我正在將DynamoDB行推送到Elasticsearch集群中。 日期字段是unix 時間戳, Kibana無法將其識別為日期。
我閱讀了Elasticsearch 映射類型並找到了這篇文章,但不知道在哪里在我的Lambda腳本中實現映射:
/* ... requires and config ... */
exports.handler = (event, context, callback) => {
event.Records.forEach((record) => {
var dbRecord = JSON.stringify(record.dynamodb);
postToES(dbRecord, context, callback);
});
};
function postToES(doc, context, lambdaCallback) {
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;
// Maybe here?
var signer = new AWS.Signers.V4(req , 'es');
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) {
lambdaCallback(null,'Lambda added document ' + doc);
});
}, function(err) {
console.log('Error: ' + err);
lambdaCallback('Lambda failed with error ' + err);
});
}
彈性搜索文檔
{
_index: "posts",
_type: "post",
_id: "6YKF2AAV06RSSRrzv6R-",
_version: 1,
found: true,
_source: {
ApproximateCreationDateTime: 1499922960,
Keys: {
id: {
S: "7asda8b0-628a-11e7-9e5e-25xyc7179dx7"
}
},
NewImage: {
posted_at: {
N: "1499922995401"
},
id: {
S: "7asda8b0-628a-11e7-9e5e-25xyc7179dx7"
}
},
SequenceNumber: "2442423900000000003279639454",
SizeBytes: 221,
StreamViewType: "NEW_AND_OLD_IMAGES"
}
}
Dynamoose 架構
var Schema = dynamoose.Schema;
var s = new Schema({
id: {
type: String,
hashKey: true,
required: true
},
posted_at: {
type: Date,
required: true
}
});
module.exports = dynamoose.model('posts', s);
示例:在我的 DynamoDB 表中,我有posted_at
字段。 內容是一個unix時間戳。 在 Kiabana 中,它被索引為
NewImage.posted_at.N
(類型:字符串、可搜索、分析)和NewImage.posted_at.N.keyword
(類型:字符串、可搜索、可聚合) 我對N
和type: string
感到困惑。
有任何想法嗎? 謝謝!
好的,結果證明N
表示DynamoDB 屬性類型(即N
代表Number
)。
問題是數字被字符串化,因此在 ES 中被索引為字符串(即您當前在映射中看到的內容)。
我們可以使用動態模板定義來解決這個問題。 首先刪除你在 ES 中的索引和 Kibana 中對應的索引模式。 然后運行這個命令:
curl -XPUT localhost:9200/_template/post_template -d '{
"template": "posts",
"mappings": {
"post": {
"dynamic_templates": [
{
"dates": {
"path_match": "NewImage.posted_at.N",
"mapping": {
"type": "date"
}
}
},
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "text",
"fields": {
"raw": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
]
}
}
}'
最后,您可以通過 Dynamoose 重新索引您的數據,之后您應該能夠在 Kibana 中找到日期字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.