本文整理汇总了Python中elasticsearch.exceptions.TransportError方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.TransportError方法的具体用法?Python exceptions.TransportError怎么用?Python exceptions.TransportError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类elasticsearch.exceptions
的用法示例。
在下文中一共展示了exceptions.TransportError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import TransportError [as 别名]
def __init__(
self, hosts=None, index="metrics", doc_type="metric", index_pattern="{index}-{date:%Y.%m.%d}", *args, **kwargs
):
# Assign these in the backend as they are needed when writing metrics
# to elasticsearch
self.index = index
self.doc_type = doc_type
self.index_pattern = index_pattern
# setup the client
self.client = Elasticsearch(hosts=hosts, *args, **kwargs)
# ensure the index is created
try:
self._setup_index()
except TransportError as exc:
logger.error('index setup error %r', exc)
try:
self._setup_mapping()
except TransportError as exc:
logger.error('mapping setup error %r', exc)
示例2: write
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import TransportError [as 别名]
def write(self, name, **data):
"""
Write the metric to elasticsearch
Args:
name (str): The name of the metric to write
data (dict): Additional data to store with the metric
"""
data["name"] = name
if not ("timestamp" in data):
data["timestamp"] = datetime.utcnow()
try:
self.client.index(index=self.get_index(), doc_type=self.doc_type, id=None, body=data)
except TransportError as exc:
logger.warning('writing metric %r failure %r', data, exc)
示例3: bulk_write
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import TransportError [as 别名]
def bulk_write(self, metrics):
"""
Write multiple metrics to elasticsearch in one request
Args:
metrics (list): data with mappings to send to elasticsearch
"""
actions = []
index = self.get_index()
for metric in metrics:
actions.append({'index': {'_index': index, '_type': self.doc_type}})
actions.append(metric)
try:
self.client.bulk(actions)
except TransportError as exc:
logger.warning('bulk_write metrics %r failure %r', metrics, exc)
示例4: do_index_command
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import TransportError [as 别名]
def do_index_command(self, index: str, **options: Any) -> CommandReturnType:
"""Rebuild search index."""
if options["interactive"]:
logger.warning("This will permanently delete the index '%s'.", index)
if not self._confirm_action():
logger.warning(
"Aborting rebuild of index '%s' at user's request.", index
)
return None
try:
delete = delete_index(index)
except TransportError:
delete = {}
logger.info("Index %s does not exist, cannot be deleted.", index)
create = create_index(index)
update = update_index(index)
return {"delete": delete, "create": create, "update": update}
示例5: test_rebuild_search_index
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import TransportError [as 别名]
def test_rebuild_search_index(self, mock_update, mock_create, mock_delete):
"""Test the rebuild_search_index command."""
cmd = rebuild_search_index.Command()
result = cmd.do_index_command(
"foo", interactive=False
) # True would hang the tests
mock_delete.assert_called_once_with("foo")
mock_create.assert_called_once_with("foo")
mock_update.assert_called_once_with("foo")
self.assertEqual(result["delete"], mock_delete.return_value)
self.assertEqual(result["create"], mock_create.return_value)
self.assertEqual(result["update"], mock_update.return_value)
# check that the delete is handled if the index does not exist
mock_delete.side_effect = TransportError("Index not found")
result = cmd.do_index_command(
"foo", interactive=False
) # True would hang the tests
self.assertEqual(result["delete"], {})
示例6: add_viewset
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import TransportError [as 别名]
def add_viewset(table):
data_index = table.name
record_data_index = "{}.".format(table.name)
deleted_data_index = "{}..".format(table.name)
def retrieve(self, request, *args, **kwargs):
try:
res = es.search(index=record_data_index, doc_type="record-data", body={"query": {"term": {"S-data-id": kwargs["pk"]}}}, sort="S-update-time:desc")
except NotFoundError as exc:
raise exceptions.NotFound("Document {} was not found in Type data of Index {}".format(kwargs["pk"], record_data_index))
except TransportError as exc:
return Response([])
return Response(res["hits"])
viewset = type(table.name, (mixins.RetrieveModelMixin, viewsets.GenericViewSet), dict(
permission_classes=(permissions.IsAuthenticated, ), retrieve=retrieve))
setattr(views, table.name, viewset)
return viewset
示例7: add_transaction
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import TransportError [as 别名]
def add_transaction(self, tx):
temp = { '_type': 'doc',
'_op_type': 'update',
'_index': "btc-opreturn",
'_id': "%s-%s" % (tx['tx'], tx['n']),
'doc_as_upsert': True,
'doc': tx
}
self.transactions.append(temp)
if len(self.transactions) > 200:
try:
self.es_handle.add_bulk_tx(self)
except TransportError:
import pdb; pdb.set_trace
self.transactions = []
self.current = -1
示例8: execute
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import TransportError [as 别名]
def execute(self, ignore_cache=False, raise_on_error=True):
"""
Execute the multi search request and return a list of search results.
"""
if ignore_cache or not hasattr(self, '_response'):
es = connections.get_connection(self._using)
responses = es.msearch(
index=self._index,
body=self.to_dict(),
**self._params
)
out = []
for s, r in zip(self._searches, responses['responses']):
if r.get('error', False):
if raise_on_error:
raise TransportError('N/A', r['error']['type'], r['error'])
r = None
else:
r = Response(s, r)
out.append(r)
self._response = out
return self._response
示例9: take_snapshot
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import TransportError [as 别名]
def take_snapshot(options):
esm = ElasticsearchSnapshotManager(options)
sh = esm.sh
snapshot = options.snapshot and options.snapshot or 'all_' + time.strftime('%Y%m%d%H')
snapdef = {
"include_global_state": True
}
if options.indices:
snapdef['indices'] = ','.join(options.indices)
try:
sh.create(repository=options.repository, snapshot=snapshot, body=json.dumps(snapdef), wait_for_completion=options.wait, request_timeout=7200)
# Housekeeping - delete old snapshots
snapshots = sh.get(repository=options.repository, snapshot="_all", request_timeout=120)['snapshots']
num_snaps = len(snapshots)
if num_snaps > options.keep:
up_to = num_snaps - options.keep
logger.info('TOTAL: %d - Will delete 1 -> %d' % (num_snaps, up_to + 1))
for snap in snapshots[0:up_to]:
sh.delete(repository=options.repository, snapshot=snap['snapshot'], request_timeout=3600)
logger.info('Deleted snapshot %s' % snap['snapshot'])
except exceptions.TransportError as e:
pass
示例10: test_perform_request_exception
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import TransportError [as 别名]
def test_perform_request_exception(self):
conn = es.ESHttpConnection()
conn.pool = Mock()
conn.pool.urlopen.side_effect = TransportError('N/A', '')
with pytest.raises(JHTTPBadRequest):
conn.perform_request('POST', 'http://localhost:9200')
示例11: test_perform_request_no_index
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import TransportError [as 别名]
def test_perform_request_no_index(self, mock_log):
mock_log.level = logging.DEBUG
mock_log.debug.side_effect = TransportError(
404, 'IndexMissingException')
conn = es.ESHttpConnection()
with pytest.raises(es.IndexNotFoundException):
conn.perform_request('POST', 'http://localhost:9200')
示例12: es_version
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import TransportError [as 别名]
def es_version(self):
"""
Returns the reported version from the Elasticsearch server.
"""
if self._es_version is None:
for retry in range(3):
try:
self._es_version = self.info()['version']['number']
break
except TransportError:
if retry == 2:
raise
time.sleep(3)
return self._es_version
示例13: create_new_index
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import TransportError [as 别名]
def create_new_index(group):
"""Create a new index for a specific Resource Type Group. Upon
exit of this method, the index is still not ready to be used.
The index still needs to have the settings/mappings set for
each plugin (Document Type).
"""
es_engine = searchlight.elasticsearch.get_api()
kwargs = {}
index_settings = _get_index_settings_from_config()
if index_settings:
kwargs = {'body': {'index': index_settings}}
index_name = None
while not index_name:
# Use utcnow() to ensure that the name is unique.
now = oslo_utils.timeutils.utcnow()
index_name = (group + '-' + now.strftime(FORMAT))
try:
es_engine.indices.create(index=index_name, **kwargs)
except es_exc.TransportError as e:
if (e.error.startswith("IndexAlreadyExistsException") or
e.error.startswith("index_already_exists_exception")):
# This index already exists! Try again.
index_name = None
else:
raise
return index_name
示例14: test_error_warning
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import TransportError [as 别名]
def test_error_warning(self, mocked_logger):
transport_error = TransportError('mocked error')
es_index_error_ctx = mock.patch(
'time_execution.backends.elasticsearch.Elasticsearch.index', side_effect=transport_error
)
frozen_time_ctx = freeze_time('2016-07-13')
with es_index_error_ctx, frozen_time_ctx:
self.backend.write(name='test:metric', value=None)
mocked_logger.warning.assert_called_once_with(
'writing metric %r failure %r',
{'timestamp': datetime(2016, 7, 13), 'value': None, 'name': 'test:metric'},
transport_error,
)
示例15: test_bulk_write_error
# 需要导入模块: from elasticsearch import exceptions [as 别名]
# 或者: from elasticsearch.exceptions import TransportError [as 别名]
def test_bulk_write_error(self, mocked_logger):
transport_error = TransportError('mocked error')
es_index_error_ctx = mock.patch(
'time_execution.backends.elasticsearch.Elasticsearch.bulk', side_effect=transport_error
)
metrics = [1, 2, 3]
with es_index_error_ctx:
self.backend.bulk_write(metrics)
mocked_logger.warning.assert_called_once_with('bulk_write metrics %r failure %r', metrics, transport_error)