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

发布于 2024-12-03 18:40:53 字数 1519 浏览 3 评论 0原文

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

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

这怎么可能?

I'm using this mapper created by 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);
        }
    }
}

What I want to do in addition to the above is also change the datastore namespace of the entity that is being modified.

How is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终陌 2024-12-10 18:40:53

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

The namespace is a component of an Entity's immutable key. There is no notion of changing it. If you're trying to 'move' an entity into a different namespace, you'll need to create a new entity in that namespace, track down any references to the old entity and update them, then delete the old entity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文