Python批量寫入ES索引數(shù)據(jù)的示例代碼
背景
由于使用官方性能壓測工具esrally并不能隨心所欲地控制創(chuàng)建索引的內(nèi)容、索引的結構和數(shù)據(jù)量,無法創(chuàng)建指定的測試數(shù)據(jù)集,或者直接投入生產(chǎn)使用。使用java或者spark則需編譯使用,修改麻煩,人生苦短,我用python。本文介紹使用python腳本批量寫ES數(shù)據(jù),需要使用pip提前下載安裝es依賴庫。

在線安裝pip
這主要是為了安裝python依賴。
wget [https://bootstrap.pypa.io/pip/2.7/get-pip.py](https://bootstrap.pypa.io/pip/2.7/get-pip.py) python get-pip.py [root@manager data]# pip --version pip 20.3.4 from /usr/lib/python2.7/site-packages/pip (python 2.7) pip install elasticsearch
Python腳本代碼
數(shù)據(jù)實例如下圖的效果:

代碼全文如下(大多數(shù)字段的內(nèi)容都會隨機變化):
(注意,示例中大部分中文內(nèi)容是為了增加存儲使用量,否則100萬條數(shù)據(jù)僅85MB,當前百萬數(shù)據(jù)占用665MB,可酌情減少字段)
# coding: utf-8
from elasticsearch import Elasticsearch
from elasticsearch import helpers
import random
import time
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
es = Elasticsearch(hosts='http://10.180.249.94:9200')
# print(es)
names = ['劉一', '陳二', '張三', '李四', '王五', '趙六', '孫七', '周八', '吳九', '鄭十']
names2 = ['劉一沒有曾用名', '陳二沒有曾用名', '張三沒有曾用名', '李四也沒有曾用名啊', '王五沒有曾用名', '趙六也沒有曾用名']
sexs = ['男', '女']
subjects = ['語文', '數(shù)學', '英語', '生物', '地理','物理','化學','思想','歷史']
grades = [85, 77, 96, 74, 85, 69, 84, 59, 67, 69, 86, 96, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86]
ages = [12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
likes = ['接天蓮葉無窮碧','映日荷花別樣紅','不識廬山真面目','只緣身在此山中','兩個黃鸝鳴翠柳','一行白鷺上青天','姑蘇城外寒山寺','夜半鐘聲到客船','窗含西嶺千秋雪','門泊東吳萬里船']
webs = ['https://api.paugram.com/help/acgm','https://docs.tenapi.cn/img.html','https://api.mz-moe.cn/img.php','https://www.jinrishici.com/doc/#instance-right','https://www.bookmarkearth.com','https://www.yijianlogo.com/template']
datas = ['西風吹老洞庭波,一夜湘君白發(fā)多。醉后不知天在水,滿船清夢壓星河。','鑿破蒼苔地,偷他一片天。白云生鏡里,明月落階前。','春風倚棹闔閭城,水國春寒陰復晴。細雨濕衣看不見,閑花落地聽無聲。','日斜江上孤帆影,草綠湖南萬里情。東道若逢相識問,青袍今日誤儒生。','浪花有意千里雪,桃李無言一隊春。一壺酒,一竿身,快活如儂有幾人?','岸闊檣稀波渺茫,獨憑危檻思何長。蕭蕭遠樹疏林外 ,一半秋山帶夕陽。']
dates = ['2002-10-12-11:00:02','2022-12-12-15:18:09','2023-12-08-11:00:02','2022-11-22-16:01:01','2021-09-19-13:55:55','2019-11-11-15:07:06','2010-08-09-11:56:09','2023-06-18-01:06:01','2022-12-12-19:06:04','2023-10-15-16:13:02']
start = time.time()
# 開始批量寫入es數(shù)據(jù)庫
# 批量寫入數(shù)據(jù)
for j in range(2000):
print(j)
action = [
{
"_index": "grades_v3",
"_type": "_doc",
"_id": i,
"_source": {
"id": i,
"name": random.choice(names),
"old_name": random.choice(names2),
"sex": random.choice(sexs),
"subject": random.choice(subjects),
"grade": random.choice(grades),
"ages": random.choice(ages),
"likes": random.choice(likes),
"likes2": random.choice(likes),
"others2": random.choice(datas),
"websites": random.choice(webs),
"others": random.choice(likes),
"login_date": random.choice(dates),
"others3": random.choice(datas)
}
} for i in range(10000 * j, 10000 * j + 10000)
]
helpers.bulk(es, action)
end = time.time()
print('Total Time Spent: ', end - start)
執(zhí)行
python esgen.py
寫入了2000萬條數(shù)據(jù)約13GB,足以模擬大多數(shù)測試需求了,耗時6330秒。

指定創(chuàng)建索引的分片數(shù)
默認的索引自動創(chuàng)建只有一個分片,指定分片的代碼如下:
# 定義要創(chuàng)建的索引及其設置,包括主分片數(shù)為3
create_index_body = {
"settings": {
"index": {
"number_of_shards": 3, # 設置主分片數(shù)為3
"number_of_replicas": 1 # 設置副本數(shù)為1,可以根據(jù)需要調整
}
}
}
# 創(chuàng)建索引
if not es.indices.exists(index="my_index"):
es.indices.create(index="my_index", body=create_index_body)
在后續(xù)寫入時指定為這個"my_index"即可,名字隨意。
到此這篇關于Python批量寫入ES索引數(shù)據(jù)的示例代碼的文章就介紹到這了,更多相關Python寫入ES索引數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python實現(xiàn)數(shù)據(jù)寫入excel表格
這篇文章主要為大家詳細介紹了python實現(xiàn)數(shù)據(jù)寫入excel表格,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
Python+OpenCV實現(xiàn)基于顏色的目標識別
這篇文章主要介紹了利用OpenCV實現(xiàn)基于顏色的目標識別,即讓攝像頭識別到視野范圍內(nèi)的有顏色的氣球并返回每個氣球的中心點坐標,感興趣的可以跟隨小編學習一下2022-01-01
python實戰(zhàn)項目scrapy管道學習爬取在行高手數(shù)據(jù)
這篇文章主要為介紹了python實戰(zhàn)項目scrapy管道學習拿在行練手爬蟲項目,爬取在行高手數(shù)據(jù),本篇博客的重點為scrapy管道pipelines的應用,學習時請重點關注2021-11-11

