使用python对elasticsearch进行批量索引

2024-04-20 13:24:01 发布

您现在位置: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)

它遵循基于令牌的身份验证,但是当我运行这个程序时,我得到下面的错误消息,我该如何解决这个问题。你知道吗

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

Tags: inpylibpackageslocallinesitebulk