【问题标题】:How do I modify this Mapreduce code to also change the Entity's namespace?如何修改此 Mapreduce 代码以更改实体的命名空间?
【发布时间】:2011-09-11 15:29:14
【问题描述】:

我正在使用这个由 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);
        }
    }
}

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

这怎么可能?

【问题讨论】:

    标签: java google-app-engine mapreduce


    【解决方案1】:

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

    【讨论】:

    • 好的。很高兴知道。但我真正的问题是我不知道如何在特定命名空间中创建实体在此 MapReduce 代码的上下文中。例如,我可以这样做:Entity newOne = new Entity("Kind", id); 创建一个新实体 - but I don't know how to set its namespace。有getNamespace 方法但没有setNamespaceNamespaceManager 会在这里设置命名空间吗?
    • NamespaceManager 是您所需要的。它同时具有.get().set()。创建实体时,会隐式使用当前命名空间。见code.google.com/appengine/docs/java/javadoc/com/google/…
    猜你喜欢
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 2010-12-04
    相关资源
    最近更新 更多