[英]How to use mapreduce to bulk update datastore entities that satisfy a query?
我想使用mapreduce库来更新满足查询的所有实体。 有几个并发症:
我知道如何将csv文件上传到Blobstore并使用Blobstore输入阅读器读取每一行。 我也知道使用查询获取实体的数据存储区输入阅读器。
我的问题是如何创建一个Mapper类,从Blobstore读取输入数据,获取数据存储区实体并尽可能高效地更新它们?
您可以使用DatastoreInputReader,并在map函数中查明property1是否实际位于csv中:每次从csv读取都会非常慢,您可以做的是使用memcache在读取之后提供该信息一次来自它自己的数据存储模型。 要填充数据存储模型,我建议使用property1作为每行的自定义ID,这样,查询它是直截了当的。 您只需为实际更改的那些值更新数据存储区,并使用变异池使其具有高性能(op.db.Put())。 我给你留下了伪代码(对不起......我只有它在python中),看看不同的部分会是什么样子,我进一步建议你在Google App Engine上阅读Mapreduce上的这篇文章: http ://sookocheff.com/posts/ 2014年4月15日应用内-发动机MapReduce的API-部分-1最基础/
#to get the to_dict method
from google.appengine.ext import ndb
from mapreduce import operation as op
from mapreduce.lib import pipeline
from mapreduce import mapreduce_pipeline
class TouchPipeline(pipeline.Pipeline):
"""
Pipeline to update the field of entities that have certain condition
"""
def run(self, *args, **kwargs):
""" run """
mapper_params = {
"entity_kind": "yourDatastoreKind",
}
yield mapreduce_pipeline.MapperPipeline(
"Update entities that have certain condition",
handler_spec="datastore_map",
input_reader_spec="mapreduce.input_readers.DatastoreInputReader",
params=mapper_params,
shards=64)
class csvrow(ndb.Model):
#you dont store property 1 because you are going to use its value as key
substitutefield=ndb.StringProperty()
def create_csv_datastore():
# instead of running this, make a 10,000 row function with each csv value,
# or read it from the blobstore, iterate and update the values accordingly
for i in range(10000):
#here we are using our own key as id of this row and just storing the other column that
#eventually will be subtitute if it matches
csvrow.get_or_insert('property%s' % i, substitutefield = 'substitute%s').put()
def queryfromcsv(property1):
csvrow=ndb.Key('csvrow', property1).get()
if csvrow:
return csvrow.substitutefield
else:
return property1
def property1InCSV(property1):
data = memcache.get(property1)
if data is not None:
return data
else:
data = self.queryfromcsv(property1)
memcache.add(property1, data, 60)
return data
def datastore_map(entity_type):
datastorepropertytocheck = entity_type.property1
newvalue = property1InCSV(datastorepropertytocheck)
if newvalue!=datastoreproperty:
entity_type.property11 = newvalue
#use the mutation pool
yield op.db.Put(entity)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.