微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Elasticsearch-检查索引是否存在时的身份验证错误

如何解决Elasticsearch-检查索引是否存在时的身份验证错误

我为kibana启用了具有认知身份验证的弹性搜索。一切正常。

在我的python脚本中,我在创建连接对象时通过在http_auth()中提供用户名/密码来连接到elasticsearch。但是,当我尝试检查索引是否存在时,是否收到身份验证错误?有人可以帮忙吗?请在这里为您的仿真示例代码

from __future__ import print_function
import json
import time
import urllib
import re
import sys
import requests
import base64
import time
from elasticsearch import Elasticsearch
from datetime import datetime

esEndpoint = ""
uname
pwd
indexName = 'index_1'

mappings_rds = {
    "settings": {
        "number_of_shards": 2,"number_of_replicas": 1
    },"mappings": {
      "properties" : {
         "tableName": {
            "type": "keyword"
          },"tableRows": {
            "type": "integer"
          },"updatedTime": {
            "type": "date","format":"date_optional_time||yyyy-MM-dd'T'HH:mm:ss"
          },"created_timestamp":{
            "type": "date","format":"date_optional_time||yyyy-MM-dd'T'HH:mm:ss"
          }
      }
    }
}

esClient = Elasticsearch([esEndPoint],http_auth=(uname,pwd))

try:
      res = esClient.indices.exists(indexName)
      print(res)
      if res is False:
          r = esClient.indices.create(indexName,body=mapping_rds,ignore=400)
      return 1
  except Exception as E:
          print("Unable to Create Index {0}".format(indexName))

解决方法

谢谢大家的评论。我曾就此与AWS支持团队联系。发现问题是我使用的用户名和密码。我正在使用通过AWS Cognito创建的用户凭证。 AWS支持团队确认,上述凭证仅用于访问Kibana,不能用于从databricks / python脚本连接到Elasticsearch。

此处提供更多信息:https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-request-signing.html

一旦我生成了访问/秘密密钥,并从我的python脚本中使用了它,便能够连接到elasticsearch以创建索引。

共享示例代码以供将来参考:

session=boto3.session.Session(aws_access_key_id=akey,aws_secret_access_key=skey,region_name=region)
credentials = session.get_credentials()
awsauth = AWS4Auth(credentials.access_key,credentials.secret_key,region,service,session_token=credentials.token)
print(credentials.access_key,credentials.token)
es = Elasticsearch(
    hosts = [{'host': esEndpoint,'port': 443}],http_auth = awsauth,use_ssl = True,verify_certs = True,connection_class = RequestsHttpConnection
)

谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。