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顯示結果:
加入時區處理后