python elasticsearch加入@timestamp字段設置及時區設置


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() # 其實這有個問題,存入到es后,kibana顯示的是當前時間+8,所以做了以下處理
    # data_local['@timestamp'] = ((datetime.datetime.now() - \
                        datetime.timedelta(hours=8))).isoformat() # 能解決kibana顯示問題,但實際入庫的timestamp時間早8小時

    data_local['@timestamp'] = datetime.datetime.now().isoformat() + "+0800" # 手動+0800,相當於加入時區,我們處於東八區,kibana顯示正確

    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一樣可以
        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顯示結果:

加入時區處理后


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM