elasticsearch6.5.4批量插入数据

elasticsearch6.5.4批量插入数据

# -*- coding: utf-8 -*-
import json
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
ES_HOST = '127.0.0.1'
ES_USER = ''
ES_PASSWD = ''
ES_PORT = 9002


class ES():
    def __init__(self):
        self.es = Elasticsearch([ES_HOST], port=ES_PORT, timeout=300)
  
    def create_index(self, index):
		"""
		创建索引
		:param index: 索引名
		:return: 
		"""
        mappings = {
			"settings": {
				"number_of_shards": 3,
				"number_of_replicas": 0
			},
			"mappings": {
				"book": {
					"properties": {
						"author": {"type": "text"},
						"characters": {"type": "text"},
						"copies": {"type": "long", "ignore_malformed": False},
						"otitle": {"type": "text"},
						"tags": {"type": "text"},
						"title": {"type": "text"},
						"year": {"type": "long", "ignore_malformed": False, "index": True},
						"available": {"type": "boolean"}
					}
				}
			}
		}

        rtn = self.es.indices.create(index=index, body=mappings, ignore=400)
        return rtn

    def insert_bulk(self, data_list):
		"""
		批量插入数据
		:return:
		"""
		try:
			success, _ = bulk(self.es, data_list, raise_on_error=True, request_timeout=300)
			return success
		except Exception, e:
			print e
			return False


if __name__ == '__main__':
    es = ES()
    #  创建索引
    index = 'library'
    print '---------'
    es.create_index(index)
    print '---------'
    # 初始数据
	data_list = [{
		"_index": "library",
		"_type": "book",
		"_id": "1",
		"title": "All Quiet on the Western Front",
		"otitle": "Im Westen nichts Neues",
		"author": "Erich Maria Remarque",
		"year": 1929,
		"characters": ["Paul Bäumer", "Albert Kropp", "Haie  Westhus", "Fredrich Müller", "Stanislaus Katczinsky",
			"Tjaden"
		],
		"tags": ["novel"],
		"copies": 1,
		"available": True,
		"section": 3
	}, {
		"_index": "library",
		"_type": "book",
		"_id": "2",
		"title": "Catch-22",
		"author": "Joseph Heller",
		"year": 1961,
		"characters": ["John Yossarian", "Captain Aardvark",
			"Chaplain Tappman", "Colonel Cathcart", "Doctor Daneeka"
		],
		"tags": ["novel"],
		"copies": 6,
		"available": False,
		"section": 1
	}, {
		"_index": "library",
		"_type": "book",
		"_id": "3",
		"title": "The Complete Sherlock Holmes",
		"author": "Arthur Conan Doyle",
		"year": 1936,
		"characters": ["Sherlock Holmes", "Dr. Watson", "G. Lestrade"],
		"tags": [],
		"copies": 0,
		"available": False,
		"section": 12
	}, {
		"_index": "library",
		"_type": "book",
		"_id": "4",
		"title": "Crime and Punishment",
		"otitle": "Преступлéние инаказáние",
		"author": "Fyodor Dostoevsky",
		"year": 1886,
		"characters": ["Raskolnikov", "Sofia Semyonovna Marmeladova"],
		"tags": [],
		"copies": 0,
		"available": True
	}]
	# 批量插入数据
	es.insert_bulk(data_list)
	print '---------'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值