使用python的Elasticsearch批量索引

时间:2019-05-15 09:35:34

标签: python elasticsearch

我有以下代码,该代码使用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)

它遵循基于令牌的身份验证,但是当我运行此程序时,出现以下错误消息,我该如何解决。

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')```

1 个答案:

答案 0 :(得分:0)

我认为es应该是这样。

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