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
    评论
Elasticsearch 6.5.4 中,可以使用 Java API 批量插入数据。下面是一个示例代码: ```java import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.rest.RestStatus; import java.io.IOException; import java.util.List; public class ElasticsearchBulkInsert { private static final String INDEX_NAME = "my_index"; private static final String TYPE_NAME = "_doc"; public static void bulkInsert(RestHighLevelClient client, List<String> documents) throws IOException { BulkRequest bulkRequest = new BulkRequest(); for (String document : documents) { IndexRequest indexRequest = new IndexRequest(INDEX_NAME, TYPE_NAME); indexRequest.source(document, XContentType.JSON); bulkRequest.add(indexRequest); } try { BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT); if (bulkResponse.hasFailures()) { // 处理错误 for (BulkItemResponse bulkItemResponse : bulkResponse) { if (bulkItemResponse.isFailed()) { BulkItemResponse.Failure failure = bulkItemResponse.getFailure(); String id = bulkItemResponse.getId(); String message = failure.getMessage(); RestStatus status = failure.getStatus(); // 处理错误 } } } } catch (IndexNotFoundException e) { // 处理索引不存在异常 } } } ``` 在上面的代码中,我们首先创建了一个 `BulkRequest` 对象,然后循环要插入的文档,将每个文档构造成一个 `IndexRequest` 对象,并调用 `bulkRequest.add()` 方法将其添加到 `BulkRequest` 中。最后,调用 `client.bulk()` 方法执行批量插入操作。 要注意的是,如果批量插入过程中出现错误,可以通过检查 `BulkResponse` 对象中的 `hasFailures()` 方法来判断是否存在错误。如果存在错误,可以通过遍历 `BulkItemResponse` 对象来处理每个文档的错误信息。如果在批量插入时发现索引不存在,可以捕获 `IndexNotFoundException` 异常并进行处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值