1、首先如下数据
{"_type": "type1", "_id": 1, "_index": "test", "_source": {"JOBNAME0": "guba_eastmoney_com_265162", "JOBNAME1": "guba_eastmoney_com_265162"}}
2、调用es相关模块插入数据到es中
#!/usr/bin/python import threading import queue import json import time from elasticsearch import Elasticsearch from elasticsearch import helpers import os import sys # # host_list = [ # {"host":"10.58.7.190","port":9200}, # {"host":"10.58.55.191","port":9200}, # {"host":"10.58.55.192","port":9200}, # ] # host_list = [ {"host":"10.87.7.190","port":9200}, ] # create a es clint obj client = Elasticsearch(host_list) with open(os.path.join(os.path.dirname(os.path.abspath(__file__)),"insert.json"),"r") as f: for line in f: actions = [] actions.append(json.loads(line)) try: for k, v in helpers.parallel_bulk(client=client, thread_count=1, actions=actions): # 这里的actions是插入es的数据,这个格式必须是列表的格式,列表的每个元素又必须是字典 pass except Exception as e: sys.stderr(e)
3、查看es索引中的文档数
[root@test1 cdrom]# curl -XGET 'http://10.87.7.190:9200/_cat/indices?v&pretty' health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open test r91GhsFVT7iF6M3iAuNEKg 2 5 19362 0 1.3mb 499.7kb