我写了一个简单的代码来读取文本文件,并使用Hadoop Map reduce程序显示与输出相同的文本,这里我不使用reducer只使用mapper来显示输入文本文件
我把映射器作为
Mapper<LongWritable, Text, NullWritable, Text>
但我只希望我的输出值我不想看到键值
我在Mapper中编写了一个代码,但它显示了NUllPointerException
package com.demo.mr;
import java.io.IOException;
import org.apache.commons.io.output.NullWriter;
import org.apache.commons.lang.ObjectUtils.Null;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class DemoMR {
public static class MapperDemo extends
Mapper<LongWritable, Text, NullWritable, Text> {
//public NullWritable result = new NullWritable();
@Override
protected void map(LongWritable key, Text value,
org.apache.hadoop.mapreduce.Mapper.Context context)
throws IOException, InterruptedException {
context.write(Null,value);
}
}
public static void main(String args[]) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf, "Demo MR");
job.setJarByClass(DemoMR.class);
job.setMapperClass(MapperDemo.class);
job.setOutputKeyClass(LongWritable.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path("/home/node1/WordCountInput.txt"));
FileOutputFormat.setOutputPath(job, new Path("/home/node1/output"));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
答案 0 :(得分:0)
您应该像以下
一样对地图输出进行编码context.write(NullWritable, value)
如果地图输出只有键,则可以在值字段中使用NullWritable。
在Hadoop中,您应该将所有数据类型用作Writable。将数据从/向网络序列化/反序列化很容易。