如何为 Elasticsearch/Kibana 格式化来自 DynamoDB 的时间戳?

How to format a timestamp from DynamoDB for Elasticsearch/Kibana?

我正在将 DynamoDB 行推送到 Elasticsearch 集群中。日期字段是 unix timestampsKibana 无法将其识别为日期。

我阅读了 Elasticsearch mapping types and found 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 架构

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 table 中,我有字段 posted_at。内容是一个 unix 时间戳。在 Kiabana 中,它被索引为

我对 Ntype: string 感到困惑。

有什么想法吗? 谢谢!

好吧,原来 N 是用来表示 DynamoDB attribute type(即 N 代表 Number)。

问题是数字被字符串化,因此在 ES 中被索引为字符串(即您当前在映射中看到的内容)。

我们可以使用动态模板定义来解决这个问题。先删除你在ES中的index和Kibana中对应的index pattern。然后 运行 这个命令:

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 中找到一个日期字段。