代码之家  ›  专栏  ›  技术社区  ›  Mr. B.

如何为Elasticsearch/Kibana从DynamoDB格式化时间戳?

  •  0
  • Mr. B.  · 技术社区  · 7 年前

    发电机B 将行放入 弹性搜索 unix时间戳 未被识别 基巴纳 作为日期。

    我读到 Elasticsearch mapping types 并发现 this post ,但不知道在我的 λ

    /* ... 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"
        }
    }
    

    动力图式

    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 .

    有什么想法吗? 谢谢

    1 回复  |  直到 7 年前
        1
  •  1
  •   Val    7 年前

    好的,结果是 N DynamoDB attribute type (即。 代表 Number ).

    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
                    }
                  }
                }
              }
            }
          ]
        }
      }
    }'