Serialize the data in bulk helper
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from itertools import islice
|
from itertools import islice
|
||||||
from operator import methodcaller
|
from operator import methodcaller
|
||||||
@@ -38,13 +40,13 @@ def expand_action(data):
|
|||||||
|
|
||||||
return action, data.get('_source', data)
|
return action, data.get('_source', data)
|
||||||
|
|
||||||
def _chunk_actions(actions, chunk_size):
|
def _chunk_actions(actions, chunk_size, serializer):
|
||||||
while True:
|
while True:
|
||||||
bulk_actions = []
|
bulk_actions = []
|
||||||
for action, data in islice(actions, chunk_size):
|
for action, data in islice(actions, chunk_size):
|
||||||
bulk_actions.append(action)
|
bulk_actions.append(serializer.dumps(action))
|
||||||
if data is not None:
|
if data is not None:
|
||||||
bulk_actions.append(data)
|
bulk_actions.append(serializer.dumps(data))
|
||||||
|
|
||||||
if not bulk_actions:
|
if not bulk_actions:
|
||||||
return
|
return
|
||||||
@@ -107,17 +109,16 @@ def streaming_bulk(client, actions, chunk_size=500, raise_on_error=True,
|
|||||||
should return a tuple containing the action line and the data line
|
should return a tuple containing the action line and the data line
|
||||||
(`None` if data line should be omitted).
|
(`None` if data line should be omitted).
|
||||||
"""
|
"""
|
||||||
|
serializer = client.transport.serializer
|
||||||
actions = map(expand_action_callback, actions)
|
actions = map(expand_action_callback, actions)
|
||||||
|
|
||||||
# if raise on error is set, we need to collect errors per chunk before raising them
|
# if raise on error is set, we need to collect errors per chunk before raising them
|
||||||
errors = []
|
errors = []
|
||||||
|
|
||||||
for bulk_actions in _chunk_actions(actions, chunk_size):
|
for bulk_actions in _chunk_actions(actions, chunk_size, serializer):
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# send the actual request
|
# send the actual request
|
||||||
resp = client.bulk(bulk_actions, **kwargs)
|
resp = client.bulk('\n'.join(bulk_actions) + '\n', **kwargs)
|
||||||
except TransportError as e:
|
except TransportError as e:
|
||||||
# default behavior - just propagate exception
|
# default behavior - just propagate exception
|
||||||
if raise_on_exception:
|
if raise_on_exception:
|
||||||
@@ -126,7 +127,11 @@ def streaming_bulk(client, actions, chunk_size=500, raise_on_error=True,
|
|||||||
# if we are not propagating, mark all actions in current chunk as failed
|
# if we are not propagating, mark all actions in current chunk as failed
|
||||||
err_message = str(e)
|
err_message = str(e)
|
||||||
exc_errors = []
|
exc_errors = []
|
||||||
bulk_data = iter(bulk_actions)
|
|
||||||
|
# deserialize the data back, thisis expensive but only run on
|
||||||
|
# errors if raise_on_exception is false, so shouldn't be a real
|
||||||
|
# issue
|
||||||
|
bulk_data = iter(map(serializer.loads, bulk_actions))
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
# collect all the information about failed actions
|
# collect all the information about failed actions
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ class FailingBulkClient(object):
|
|||||||
self.client = client
|
self.client = client
|
||||||
self._called = -1
|
self._called = -1
|
||||||
self._fail_at = fail_at
|
self._fail_at = fail_at
|
||||||
|
self.transport = client.transport
|
||||||
|
|
||||||
def bulk(self, *args, **kwargs):
|
def bulk(self, *args, **kwargs):
|
||||||
self._called += 1
|
self._called += 1
|
||||||
|
|||||||
Reference in New Issue
Block a user