java - 实例化 IdentityMapper 得到 ClassException。如何使用IdentityMapper?

标签 java hadoop mapreduce

我有两份工作。第一个作业只执行 map 任务 [1],不执行 reduce 任务。这会强制将 map 输出保存在 HDFS 中。

此作业完成后,我将运行另一个具有标识映射器类 [2] 的作业,该作业将读取上一个作业执行产生的映射输出,并生成相同的映射输出。我已将身份映射器设置为 [3] 中的作业,但我在 [4] 中收到错误。

我认为这个问题的原因是 setMapperClass(Class<? extends Mapper> cls)来自不同类型的 IdentityMapper [5].

我如何使用 IdentityMapper

[1] 第一份工作的 map 类

public static class MyMap extends Mapper<LongWritable, Text, Text, IntWritable> {
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(LongWritable key, Text value, Context context
    ) throws IOException, InterruptedException {
        StringTokenizer itr = new StringTokenizer(value.toString());
        while (itr.hasMoreTokens()) {
            word.set(itr.nextToken());
            context.write(word, one);
        }
    }

    public void run(Context context) throws IOException, InterruptedException {
        try {
            while (context.nextKeyValue()) {
                map(context.getCurrentKey(), context.getCurrentValue(), context);
            }
        } finally {
            cleanup(context);
        }
    }
}

[2] 身份类:

public static class MyFullyIndentityMapper extends IdentityMapper<Text, IntWritable> {}

[3] 我将标识类设置为映射器。

JobConf conf = new JobConf(MyWordCount.class);
conf.setJobName("wordcount");
conf.setClass("mapreduce.job.map.identity.class", MyFullyIndentityMapper.class, IdentityMapper.class);
Class<? extends IdentityMapper> identityClass = (Class<? extends IdentityMapper>) conf.getClass("mapreduce.job.map.identity.class", IdentityMapper.class);
job.setMapperClass(identityClass.asSubclass(Mapper.class));

[4] 我得到的错误:

Output path: /output1
java.lang.ClassCastException: class org.apache.hadoop.mapred.examples.MyWordCount$MyFullyIndentityMapper
at java.lang.Class.asSubclass(Class.java:3404)

[5] 身份映射器

public class IdentityMapper<K, V> extends MapReduceBase implements Mapper<K, V, K, V> {...}

最佳答案

我不明白你为什么要在下面的代码片段中把事情复杂化

JobConf conf = new JobConf(MyWordCount.class);
conf.setJobName("wordcount");
conf.setClass("mapreduce.job.map.identity.class", MyFullyIndentityMapper.class, IdentityMapper.class);
Class<? extends IdentityMapper> identityClass = (Class<? extends IdentityMapper>) conf.getClass("mapreduce.job.map.identity.class", IdentityMapper.class);
job.setMapperClass(identityClass.asSubclass(Mapper.class));

不要设置任何映射器类。

job.setMapperClass(identityClass.asSubclass(Mapper.class));

把上面那行注释掉就行了。 MapReduce 框架将默认运行 IdentityMapper。

以上是我对你问题的理解。我可能误解了你的问题。

关于java - 实例化 IdentityMapper 得到 ClassException。如何使用IdentityMapper?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33654476/

相关文章:

java - 使用自定义用户名的 Firebase 登录用户

scala - 如何使用 withColumn Spark Dataframe scala with while

hadoop - Reduce 未运行,但作业已成功完成

python - 如何使用Python中的map reduce函数确定值?

java - 将字符串从 Java 类迁移到资源

java - 滑动旋转图像

java - 如何查看一个数组是否包含另一个数组?

hadoop - 我应该如何将我的事件流保存到冷存储?

java - 运行Map精简程序时的大数据和hadoop异常

hadoop - 将为 Hive 中的分区表创建多少个映射器和缩减器