简体   繁体   English

使用python对elasticsearch进行批量索引

[英]Bulk index to elasticsearch using python

I have following code which indexes data to elasticsearch using python, 我有以下代码使用python将数据索引到elasticsearch,

from elasticsearch import Elasticsearch
from elasticsearch import helpers
import requests
from requests.auth import AuthBase

requests.packages.urllib3.disable_warnings()

class TokenAuth(AuthBase):

    def __init__(self, token):
        self.token = token

    def __call__(self, r):
        r.headers['Authorization :Bearer'] = f'{self.token}'  
        return r

es = Elasticsearch('https://localhost:9200/user/type',ca_certs=False,verify_certs=False,auth=TokenAuth(''))

#requests.get('https://httpbin.org/get', auth=TokenAuth('12345abcde-token'))
res = helpers.bulk(es, "ldif2.json", chunk_size=1, request_timeout=200)

It follows token based autheentication , but whan i run this progam i get below error message ,how do i solve this. 它遵循基于令牌的身份验证,但是我运行这个程序,我得到以下错误消息,我该如何解决这个问题。

Traceback (most recent call last):
  File "bulk_index.py", line 20, in <module>
    res = helpers.bulk(es, "ldif2.json", chunk_size=1, request_timeout=200)
  File "C:\Users\mkumaru\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch\helpers\actions.py", line 300, in bulk
    for ok, item in streaming_bulk(client, actions, *args, **kwargs):
  File "C:\Users\mkumaru\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch\helpers\actions.py", line 230, in streaming_bulk
    **kwargs
  File "C:\Users\mkumaru\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch\helpers\actions.py", line 116, in _process_bulk_chunk
    raise e
  File "C:\Users\mkumaru\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch\helpers\actions.py", line 112, in _process_bulk_chunk
    resp = client.bulk("\n".join(bulk_actions) + "\n", *args, **kwargs)
  File "C:\Users\mkumaru\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch\client\utils.py", line 84, in _wrapped
    return func(*args, params=params, **kwargs)
  File "C:\Users\mkumaru\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch\client\__init__.py", line 1498, in bulk
    headers={"content-type": "application/x-ndjson"},
  File "C:\Users\mkumaru\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch\transport.py", line 353, in perform_request
    timeout=timeout,
  File "C:\Users\mkumaru\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch\connection\http_urllib3.py", line 239, in perform_request
    self._raise_error(response.status, raw_data)
  File "C:\Users\mkumaru\AppData\Local\Programs\Python\Python37\lib\site-packages\elasticsearch\connection\base.py", line 168, in _raise_error
    status_code, error_message, additional_info
elasticsearch.exceptions.AuthenticationException: AuthenticationException(401, 'Access denied')```

我认为es应该是这样的。

es = Elasticsearch("http://127.0.0.1:9200", http_auth=('user', 'passwd'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM