首页 > 编程语言 > 详细

python elasticsearch加入@timestamp字段设置

时间:2021-04-13 16:50:30      阅读:55      评论:0      收藏:0      [点我收藏+]

es不常用,最近业务需要用到,因为kibana显示数据需要基于时间轴,临时学习一波,废话不多说,看以下代码:

# -*- coding: utf-8 -*-
# @Time    : 2021-04-13 09:51
# @Author  : xxx
# @FileName: demo_es.py
# @Software: PyCharm

from elasticsearch import Elasticsearch
import datetime
import time
import random
import string
import uuid

ES_URL = ‘http://172.255.12.15:9200/‘

def gen_data():
    """
    just generate test data
    :return: test data
    """
    field_1 = time.time()
    field_2 = random.choice(string.ascii_letters)
    data_local = locals()
    # 时间戳字符串, iso格式, 不增加isoformat()处理时为datetime类型
    data_local[‘@timestamp‘] = datetime.datetime.now().isoformat()
    return data_local


def gen_doc_id():
    return str(uuid.uuid4())


def insert_2_es(data):
    """
    insert data to es
    :param data:
    :return:
    """
    timestamp = datetime.date.today().strftime("%Y-%m-%d")
    index = "test-" + timestamp
    es_client = Elasticsearch(ES_URL)

    if not es_client.indices.exists(index):
        # setting mappings for index
        mapping = ‘‘‘
        {
            "mappings": {
                  "_default_": {
                    "_all": {
                      "enabled": true,
                      "norms": false
                    },
                    "dynamic_templates": [
                      {
                        "message_field": {
                          "path_match": "message",
                          "match_mapping_type": "string",
                          "mapping": {
                            "norms": false,
                            "type": "text"
                          }
                        }
                      },
                      {
                        "string_fields": {
                          "match": "*",
                          "match_mapping_type": "string",
                          "mapping": {
                            "fields": {
                              "keyword": {
                                "type": "keyword"
                              }
                            },
                            "norms": false,
                            "type": "text"
                          }
                        }
                      }
                    ],
                    "properties": {
                      "@timestamp": {
                        "type": "date",
                        "include_in_all": true
                      },
                      "@version": {
                        "type": "keyword",
                        "include_in_all": true
                      }
                    }
                  }
            }
        }
    ‘‘‘
        es_client.indices.create(index,ignore=400, body=mapping)
        print("create index successfully, index: {}".format(index))
    doc_id = gen_doc_id()
    es_client.index(index=index, doc_type=‘_doc‘, id=doc_id, body=data, op_type=‘create‘)
    print("insert to es successfully, doc_id: {}".format(doc_id))



if __name__ == "__main__":
    data = gen_data()
    print(data)
    print(gen_doc_id())
    insert_2_es(data)

以下是kibana显示结果:
技术分享图片

python elasticsearch加入@timestamp字段设置

原文:https://www.cnblogs.com/davis12/p/14652200.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!