Python bulk方式写入ElasticSearch全流程,设置用户名密码,ImportError: No module named elasticsearch

测试需要写入一堆数据,找到一个例子,但是只有中间部分,补了一些,但是一步步都是问题。先看几个解决的问题:

1、ImportError: No module named elasticsearch

Traceback (most recent call last):
  File "pyimportjson.py", line 2, in <module>
    import elasticsearch
ImportError: No module named elasticsearch

看代码没问题,书写争取,需要pip安装elasticsearch模块,根据python版本选择

pip install elasticsearch 或者 pip3 install elasticsearch

 

2、TypeError: 'encoding' is an invalid keyword argument for this function

    input_file = open('300.json', "r", encoding='utf-8')
TypeError: 'encoding' is an invalid keyword argument for this function

 

修改为  input_file = open('300.json')后正常。

完整代码,读取文件内容后通过bulk方式写入ES

 

from datetime import datetime
from elasticsearch import Elasticsearch
from elasticsearch import helpers

import json

client = Elasticsearch(["192.168.99.110"],port="19200",http_auth=('elastic', 'passmy123'), timeout=20)




def read_and_write_index():
    # define an empty list for the Elasticsearch docs
    doc_list = []

    # use Python's enumerate() function to iterate over list of doc strings
    input_file=open('filedata.json')

    json_array = json.load(input_file)

    for item in json_array:
        try:
            # convert the string to a dict object
            # add a new field to the Elasticsearch doc
            dict_doc = {}
            # add a dict key called "_id" if you'd like to specify an ID for the doc
            dict_doc["_id"] = item['id']
            dict_doc["contents"] = item['contents']
            dict_doc["type"] = item['type']
            dict_doc["author"] = item['author']
            dict_doc["title"] = item['title']

            # append the dict object to the list []
            doc_list += [dict_doc]

        except json.decoder.JSONDecodeError as err:
            # print the errors
            print("ERROR for num:", item['id'], "-- JSONDecodeError:", err, "for doc:", dict_doc)
            print("Dict docs length:", len(doc_list))



    try:
        print ("\nAttempting to index the list of docs using helpers.bulk()")

        # use the helpers library's Bulk API to index list of Elasticsearch docs
        resp = helpers.bulk(
            client,
            doc_list,
            index = "some_index",
            doc_type = "_doc"
            )

        # print the response returned by Elasticsearch
        print ("helpers.bulk() RESPONSE:", resp)
        print ("helpers.bulk() RESPONSE:", json.dumps(resp, indent=4))
    except Exception as err:
        # print any errors returned w
        ## Prerequisiteshile making the helpers.bulk() API call
        print("Elasticsearch helpers.bulk() ERROR:", err)
        quit()

read_and_write_index()

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值