Created
June 11, 2015 22:38
-
-
Save cjfuller/a4d26ae04c77d636d2d7 to your computer and use it in GitHub Desktop.
Batching input reader for appengine-mapreduce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class KeyRangeBatchKeyIterator( | |
datastore_range_iterators.AbstractKeyRangeIterator): | |
"""Yields datastore.Key type within a key range in batches. | |
This allows us to do several RPCs in parallel, hopefully reducing running | |
time. | |
TODO(colin): the query batch size is the same as the processing batch size. | |
Figure out if there's a benefit to changing this. | |
""" | |
_KEYS_ONLY = True | |
def __iter__(self): | |
"""Yield batches of keys for mapping. | |
The yielded batch is a generator, so that the query cursor doesn't move | |
until the mapper starts processing elements. | |
""" | |
self._query = self._key_range.make_ascending_datastore_query( | |
self._query_spec.entity_kind, filters=self._query_spec.filters) | |
while True: | |
keys = itertools.islice( | |
self._query.Run(config=datastore_query.QueryOptions( | |
batch_size=self._query_spec.batch_size, | |
keys_only=True, | |
start_cursor=self._cursor)), | |
self._query_spec.batch_size) | |
yield keys | |
if self._cursor == self._query.GetCursor(): | |
# We haven't moved since last time, which means we're at the | |
# end. | |
break | |
self._cursor = self._query.GetCursor() | |
def _get_cursor(self): | |
"""Get the current query cursor. | |
(AbstractKeyRangeIterator method taken from KeyRangeEntityIterator.) | |
""" | |
if self._query: | |
return self._query.GetCursor() | |
return self._cursor | |
# Register the new iterator so that the mapreduce library knows how to create | |
# one from the JSON mapreduce payload. | |
datastore_range_iterators._KEY_RANGE_ITERATORS[ | |
KeyRangeBatchKeyIterator.__name__] = KeyRangeBatchKeyIterator | |
class DatastoreKeyBatchingInputReader( | |
mapreduce.input_readers.RawDatastoreInputReader): | |
"""Mapreduce input reader that yields datastore keys in batch.""" | |
_KEY_RANGE_ITER_CLS = KeyRangeBatchKeyIterator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The MIT License (MIT) | |
Copyright (c) 2015 Khan Academy | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment