我正在将 DynamoDB 行推送到 Elasticsearch 群集中。日期字段为 unix时间戳, Kibana 无法将其识别为日期。
我阅读了Elasticsearch mapping types并找到this post,但不知道在 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);
});
}
Elasticsearch文档
{
_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 Schema
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
(type:string,searchable,aggregateable) 我对N
和type: string
感到困惑。
有什么想法吗? 谢谢!
答案 0 :(得分:1)
好的结果是N
表示DynamoDB attribute type(即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中找到日期字段。