2017-07-14 126 views
1

我正在使用elasticsearch數據庫來存儲我從在線提取的數據,但是當我嘗試索引數據庫中的數據時收到錯誤。Elasticsearch缺少REST請求的身份驗證令牌

這裏是我的創建和索引數據代碼:

es = Elasticsearch() 

es.index(index='weather', doc_type='data', body=doc) 

然而,當我運行這個程序,這些行的第二個導致錯誤,下面是完整的回溯:

Traceback (most recent call last): 
File "weatherScraper.py", line 79, in <module> 
    main() 
File "weatherScraper.py", line 73, in main 
    es.index(index='weather', doc_type='data', body=doc) 
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/client/utils.py", line 73, in _wrapped 
    return func(*args, params=params, **kwargs) 
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/client/__init__.py", line 298, in index 
    _make_path(index, doc_type, id), params=params, body=body) 
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/transport.py", line 312, in perform_request 
    status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout) 
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/connection/http_urllib3.py", line 128, in perform_request 
    self._raise_error(response.status, raw_data) 
File "/home/alec/.local/lib/python2.7/site-packages/elasticsearch/connection/base.py", line 125, in _raise_error 
    raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info) 
elasticsearch.exceptions.AuthenticationException: TransportError(401, u'security_exception', u'missing authentication token for REST request [/weather/data]') 

回答

3

「缺少認證令牌」表示在與此Elasticsearch實例交談之前需要進行認證。要索引文檔,用戶必須具有寫入權限。您可以在以下網址用戶名和密碼:http://user:[email protected]:post

例如,在一個shell:

export ES_ENDPOINT="http://usernameWithWriteAccess:[email protected]:9200" 

然後在python:

es = Elasticsearch(os.environ['ES_ENDPOINT'])