栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

ElasticSearch6.X版本自动添加时间戳

ElasticSearch6.X版本自动添加时间戳

需求:根据时间提取es数据
解决:为es的记录添加时间戳

1、方法

配置时间戳 pipeline

PUT _ingest/pipeline/my_timestamp_pipeline
{
  "description": "Adds a field to a document with the time of ingestion",
  "processors": [
    {
      "set": {
        "field": "@timestamp",
        "value": "{{_ingest.timestamp}}"
      }
    }
  ]
}
2、添加索引
#先创建索引
#语法:PUT /索引名
PUT /transactionmonitor-2021.12.15
{
    "settings": {
        "index": {
            "number_of_shards": 1,
            "number_of_replicas": 0
        }
    }
}

#在设置字段类型(es6.x版本)
#语法:PUT /索引库名/_mapping/类型名称
PUT /transactionmonitor-2021.12.15/_mapping/doc
{
    "properties": {
        "code": {
            "type": "text", 
            "fields": {
                "keyword": {
                    "type": "keyword"
                }
            }
        }, 
        "costTime": {
            "type": "long"
        }, 
        "endTime": {
            "type": "text", 
            "fields": {
                "keyword": {
                    "type": "keyword"
                }
            }
        }, 
        "host": {
            "type": "text", 
            "fields": {
                "keyword": {
                    "type": "keyword"
                }
            }
        }, 
        "isSuccess": {
            "type": "text", 
            "fields": {
                "keyword": {
                    "type": "keyword"
                }
            }
        }, 
        "isSyn": {
            "type": "text", 
            "fields": {
                "keyword": {
                    "type": "keyword"
                }
            }
        }, 
        "serviceName": {
            "type": "text", 
            "fields": {
                "keyword": {
                    "type": "keyword"
                }
            }
        }, 
        "startTime": {
            "type": "text", 
            "fields": {
                "keyword": {
                    "type": "keyword"
                }
            }
        }, 
        "transCode": {
            "type": "text", 
            "fields": {
                "keyword": {
                    "type": "keyword"
                }
            }
        }
    }
}
3、使用时间戳(添加数据)

POST /transactionmonitor-2021.12.15/doc?pipeline=my_timestamp_pipeline

POST /transactionmonitor-2021.12.15/doc?pipeline=my_timestamp_pipeline
{
    "msg": null, 
    "isSyn": "true", 
    "code": "0", 
    "serviceName": "wfservice",
    "costTime": 62, 
    "host": "127.0.0.1:8080", 
    "startTime": "2021-11-21 00:00:36.593", 
    "transCode": "GG02I023", 
    "endTime": "2021-11-21 00:00:36.655", 
    "isSuccess": "Y"
}
4、查询验证结果
GET /transactionmonitor-2021.12.15/doc/_search
{"query": {"match_all": {}}}

返回结果
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "transactionmonitor-2021.12.15",
        "_type": "doc",
        "_id": "gLEDvX0BdLj7MTacoc1Y",
        "_score": 1,
        "_source": {
          "msg": null,
          "isSyn": "true",
          "code": "0",
          "serviceName": "wfservice",
          "@timestamp": "2021-12-15T07:34:31.496Z",
          "costTime": 62,
          "host": "127.0.0.1:8080",
          "startTime": "2021-11-21 00:00:36.593",
          "transCode": "GG02I023",
          "endTime": "2021-11-21 00:00:36.655",
          "isSuccess": "Y"
        }
      }
    ]
  }
}

自动为数据添加上了@timestamp时间戳字段

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/672200.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号