本文整理汇总了Python中elasticsearch.exceptions.ConnectionError方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.ConnectionError方法的具体用法?Python exceptions.ConnectionError怎么用?Python exceptions.ConnectionError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类elasticsearch.exceptions
的用法示例。
在下文中一共展示了exceptions.ConnectionError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: elastic_query
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import ConnectionError [as 别名]
def elastic_query(self, query: str, csv=False):
"""
Request an http SQL query to elasticsearch
"""
self.description = None
# Sanitize query
query = self.sanitize_query(query)
payload = {"query": query}
if csv:
path = f"/{self.sql_path}/?format=csv"
else:
path = f"/{self.sql_path}/"
try:
resp = self.es.transport.perform_request("POST", path, body=payload)
except es_exceptions.ConnectionError as e:
raise exceptions.OperationalError(
f"Error connecting to {self.url}: {e.info}"
)
except es_exceptions.RequestError as e:
raise exceptions.ProgrammingError(
f"Error ({e.error}): {e.info['error']['reason']}"
)
return resp
示例2: get_test_client
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import ConnectionError [as 别名]
def get_test_client(nowait=False, **kwargs):
# construct kwargs from the environment
kw = {"timeout": 30}
if "TEST_ES_CONNECTION" in os.environ:
from elasticsearch import connection
kw["connection_class"] = getattr(connection, os.environ["TEST_ES_CONNECTION"])
kw.update(kwargs)
client = Elasticsearch([os.environ.get("TEST_ES_SERVER", {})], **kw)
# wait for yellow status
for _ in range(1 if nowait else 100):
try:
client.cluster.health(wait_for_status="yellow")
return client
except ConnectionError:
time.sleep(0.1)
else:
# timeout
raise SkipTest("Elasticsearch failed to start.")
示例3: connect
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import ConnectionError [as 别名]
def connect(self):
counter = 0
self.success = False
if self.username and self.password:
url = "%s://%s:%s@%s:%d" % (self.protocol, self.username, self.password, self.host, self.port)
else:
url = "%s://%s:%d" % (self.protocol, self.host, self.port)
while True:
try:
self.es = Elasticsearch([url])
self.es.cluster.health(wait_for_status='green', request_timeout=20)
self.success = True
break
except exceptions.ConnectionError as e:
logger.warning('Still trying to connect to Elasticsearch...')
counter += 1
if counter == MAX_ATTEMPTS:
break
logger.info('Sleeping 10 seconds...')
time.sleep(10)
示例4: get_test_client
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import ConnectionError [as 别名]
def get_test_client(nowait=False):
# construct kwargs from the environment
kw = {}
if 'TEST_ES_CONNECTION' in os.environ:
from elasticsearch import connection
kw['connection_class'] = getattr(connection, os.environ['TEST_ES_CONNECTION'])
client = Elasticsearch([os.environ.get('TEST_ES_SERVER', {})], **kw)
# wait for yellow status
for _ in range(1 if nowait else 100):
try:
client.cluster.health(wait_for_status='yellow')
return client
except ConnectionError:
time.sleep(.1)
else:
# timeout
raise SkipTest("Elasticsearch failed to start.")
示例5: get
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import ConnectionError [as 别名]
def get(self, request, format=None):
# TODO: catch all exception. At the very least, deal with 404 not found and
# connection refused exceptions.
# Temporarily remove exceptions for debugging.
try:
trail_ids = [x["key"] for x in self.es.search(index=self.index, body={
"aggs" : {
"trail_id" : {
"terms" : { "field" : "trail_id" }
}
}
})["aggregations"]["trail_id"]["buckets"]]
response = self.create_trails(trail_ids)
except ConnectionError as e:
raise OSError("Failed to connect to local elasticsearch instance.")
except NotFoundError:
raise DataWakeIndexUnavailable
return Response(response)
示例6: fetch_public_sources
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import ConnectionError [as 别名]
def fetch_public_sources():
proxy_params = get_system_settings().get_proxy_params()
try:
hdrs = { 'User-Agent': 'scirius' }
if proxy_params:
resp = requests.get(settings.DEFAULT_SOURCE_INDEX_URL, proxies = proxy_params, headers = hdrs)
else:
resp = requests.get(settings.DEFAULT_SOURCE_INDEX_URL, headers = hdrs)
resp.raise_for_status()
except requests.exceptions.ConnectionError, e:
if "Name or service not known" in unicode(e):
raise IOError("Connection error 'Name or service not known'")
elif "Connection timed out" in unicode(e):
raise IOError("Connection error 'Connection timed out'")
else:
raise IOError("Connection error '%s'" % (e))
示例7: get_test_client
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import ConnectionError [as 别名]
def get_test_client(nowait=False, **kwargs):
# construct kwargs from the environment
kw = {'timeout': 30}
if 'TEST_ES_CONNECTION' in os.environ:
from elasticsearch import connection
kw['connection_class'] = getattr(connection, os.environ['TEST_ES_CONNECTION'])
kw.update(kwargs)
client = Elasticsearch([os.environ.get('TEST_ES_SERVER', {})], **kw)
# wait for yellow status
for _ in range(1 if nowait else 100):
try:
client.cluster.health(wait_for_status='yellow')
return client
except ConnectionError:
time.sleep(.1)
else:
# timeout
raise SkipTest("Elasticsearch failed to start.")
示例8: set_up_index
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import ConnectionError [as 别名]
def set_up_index(self):
try:
try:
try:
index_exists = self.__es_conn__.indices.exists(index=__index_name__)
if not index_exists:
self.create_index()
else:
res = self.__es_conn__.indices.get_mapping(index=__index_name__)
try:
current_version = res[__index_name__]['mappings']['_meta']['version']
if current_version < __index_version__:
self.update_index(current_version)
elif current_version is None:
logger.error("Old Index Mapping. Manually reindex the index to persist your data.")
print("\n -- Old Index Mapping. Manually reindex the index to persist your data.--\n")
sys.exit(1)
except KeyError:
logger.error("Old Index Mapping. Manually reindex the index to persist your data.")
print("\n -- Old Index Mapping. Manually reindex the index to persist your data.--\n")
sys.exit(1)
except ESConnectionError as e:
logger.error("Elasticsearch is not installed or its service is not running. {0}".format(e))
print("\n -- Elasticsearch is not installed or its service is not running.--\n", e)
sys.exit(1)
except NewConnectionError:
pass
except ConnectionRefusedError:
pass
示例9: get_array_type_columns
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import ConnectionError [as 别名]
def get_array_type_columns(self, table_name: str) -> "Cursor":
"""
Queries the index (table) for just one record
and return a list of array type columns.
This is useful since arrays are not supported by ES SQL
"""
array_columns = []
try:
resp = self.es.search(index=table_name, size=1)
except es_exceptions.ConnectionError as e:
raise exceptions.OperationalError(
f"Error connecting to {self.url}: {e.info}"
)
except es_exceptions.NotFoundError as e:
raise exceptions.ProgrammingError(
f"Error ({e.error}): {e.info['error']['reason']}"
)
try:
_source = resp["hits"]["hits"][0]["_source"]
except KeyError as e:
raise exceptions.DataError(
f"Error inferring array type columns {self.url}: {e}"
)
for col_name, value in _source.items():
# If it's a list (ES Array add to cursor)
if isinstance(value, list):
if len(value) > 0:
# If it's an array of objects add all keys
if isinstance(value[0], dict):
for in_col_name in value[0]:
array_columns.append([f"{col_name}.{in_col_name}"])
array_columns.append([f"{col_name}.{in_col_name}.keyword"])
continue
array_columns.append([col_name])
array_columns.append([f"{col_name}.keyword"])
# Not array column found
if not array_columns:
array_columns = [[]]
self.description = [("name", Type.STRING, None, None, None, None, None)]
self._results = array_columns
return self
示例10: init_connection
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import ConnectionError [as 别名]
def init_connection(self):
"""
Initialize the connection with Elasticsearch
:return: Connection object if connection with Elasticsearch succeeded, False otherwise
"""
try:
http_auth = (self.settings.config.get("general", "es_username"),
self.settings.config.get("general", "es_password"))
except NoOptionError:
http_auth = ("", "")
self.conn = Elasticsearch([self.settings.config.get("general", "es_url")],
http_auth=http_auth,
use_ssl=False,
timeout=self.settings.config.getint("general", "es_timeout"),
verify_certs=False,
retry_on_timeout=True)
try:
self.conn.info()
self.logging.logger.info("connected to Elasticsearch on host %s" %
(self.settings.config.get("general", "es_url")))
result = self.conn
except AuthenticationException:
self.logging.logger.error("could not connect to Elasticsearch on host %s due to authentication error." %
(self.settings.config.get("general", "es_url")))
result = False
except ConnectionError:
self.logging.logger.error("could not connect to Elasticsearch on host %s" %
(self.settings.config.get("general", "es_url")))
result = False
return result
示例11: wait_until_responsive
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import ConnectionError [as 别名]
def wait_until_responsive(self, timeout, clock=timeit.default_timer):
"""Wait until ElasticSearch becomes responsive (or too much time passes)."""
# Elapsed time is a floating point number of seconds.
timeout = timeout.total_seconds()
# Don't poll unless we're asked to.
if timeout <= 0.0:
return
# Periodically poll ElasticSearch. Keep going until ElasticSearch is
# responsive *and* the writeback index exists.
ref = clock()
while (clock() - ref) < timeout:
try:
if self.writeback_es.indices.exists(self.writeback_alias):
return
except ConnectionError:
pass
time.sleep(1.0)
if self.writeback_es.ping():
logging.error(
'Writeback alias "%s" does not exist, did you run `elastalert-create-index`?',
self.writeback_alias,
)
else:
logging.error(
'Could not reach ElasticSearch at "%s:%d".',
self.conf['es_host'],
self.conf['es_port'],
)
exit(1)
示例12: delete_index
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import ConnectionError [as 别名]
def delete_index(self, index_name):
"""Delete Elasticsearch index.
Args:
index_name: Name of the index to delete.
"""
if self.client.indices.exists(index_name):
try:
self.client.indices.delete(index=index_name)
except ConnectionError as e:
raise RuntimeError(
'Unable to connect to Timesketch backend: {}'.format(e)
)
示例13: elasticsearch
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import ConnectionError [as 别名]
def elasticsearch():
try:
yield Elasticsearch(settings.HOOVER_ELASTICSEARCH_URL)
except ConnectionError:
raise SearchError('Could not connect to Elasticsearch.')
except RequestError as e:
reason = 'reason unknown'
try:
if e.info:
reason = e.info['error']['root_cause'][0]['reason']
except LookupError:
pass
raise SearchError('Elasticsearch failed: ' + reason)
示例14: post
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import ConnectionError [as 别名]
def post(self, request, format=None):
es_data = ESData()
msg = None
try:
es_data.es_clear()
except ConnectionError as e:
msg = 'Could not connect to Elasticsearch'
except Exception as e:
msg = 'Clearing failed: %s' % e
if msg is not None:
raise serializers.ValidationError({'delete_es_logs': [msg]})
return Response({'delete_es_logs': 'ok'})
示例15: handle
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import ConnectionError [as 别名]
def handle(self, *args, **options):
try:
count = self.es_clear()
except ConnectionError as e:
self.stderr.write('Could not connect to Elasticsearch, please retry later.')
raise CommandError(repr(e))
if count:
self.stdout.write('Data erased successfully (%i index%s deleted)' % (count, 'es' if count > 1 else ''))
else:
self.stdout.write('There is no index to erase')