我是Python和Elasticsearch的新手,我编写了一段Python代码,从一个非常大的JSON文件中读取数据,并将其中一些属性索引到Elasticsearch中。
import elasticsearch
import json
es = elasticsearch.Elasticsearch() # use default of localhost, port 9200
with open('j.json') as f:
n=0
for line in f:
try:
j_content = json.loads(line)
event_type = j_content['6000000']
device_id = j_content['6500048']
raw_event_msg= j_content['6000012']
event_id = j_content["0"]
body = {
'6000000': str(event_type),
'6500048': str(device_id),
'6000012': str(raw_event_msg),
'6000014': str(event_id),
}
n=n+1
es.index(index='coredb', doc_type='json_data', body=body)
except:
pass
但是代码运行太慢,我的硬件资源还有很多空余。我应该如何通过使用多线程或批量处理来提高代码的性能呢?