如何修改此Mapreduce代码以更改Entity的命名空间?

时间:2011-09-11 15:29:14

标签: java google-app-engine mapreduce

我正在使用Ikai Lan创建的这个映射器:

package com.ikai.mapperdemo.mappers;

import java.util.Date;
import java.util.logging.Logger;

import org.apache.hadoop.io.NullWritable;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.tools.mapreduce.AppEngineMapper;
import com.google.appengine.tools.mapreduce.DatastoreMutationPool;

/**
 *
 * The functionality of this is exactly the same as in {@link NaiveToLowercaseMapper}.
 * The advantage here is that since a {@link DatastoreMutationPool} is used, mutations
 * can be done in batch, saving API calls.
 *
 * @author Ikai Lan
 *
 */
public class PooledToLowercaseMapper extends
        AppEngineMapper<Key, Entity, NullWritable, NullWritable> {
    private static final Logger log = Logger
            .getLogger(PooledToLowercaseMapper.class.getName());

    @Override
    public void map(Key key, Entity value, Context context) {
        log.info("Mapping key: " + key);

        if (value.hasProperty("comment")) {
            String comment = (String) value.getProperty("comment");
            comment = comment.toLowerCase();
            value.setProperty("comment", comment);
            value.setProperty("updatedAt", new Date());

            DatastoreMutationPool mutationPool = this.getAppEngineContext(
                    context).getMutationPool();
            mutationPool.put(value);
        }
    }
}

除了上述内容之外,我还要做的是更改正在修改的实体的数据存储名称空间。

这怎么可能?

1 个答案:

答案 0 :(得分:2)

命名空间是Entity的不可变键的一个组件。没有改变它的概念。如果您尝试将实体“移动”到不同的命名空间,则需要在该命名空间中创建新实体,跟踪对旧实体的任何引用并更新它们,然后删除旧实体。