我正在和我一起工作Hadoop MapRedue,并有一个问题.目前,我的映射的input KV type是LongWritable, LongWritable type和
output KV type也LongWritable, LongWritable type.InputFileFormat是SequenceFileInputFormat.基本上我想要做的是将一个txt文件更改为SequenceFileFormat,以便我可以将它用于我的mapper.
我想做的是
输入文件是这样的
1\t2 (key = 1, value = 2)
2\t3 (key = 2, value = 3)
等等...
我查看了这个线程如何将.txt文件转换为Hadoop的序列文件格式,但TextInputFormat只重新支持Key = LongWritable and Value = Text
有没有办法获得txt并生成一个序列文件KV = LongWritable, LongWritable?
当然,基本上与我在你链接的另一个帖子中讲述的方式相同.但你必须实现自己的Mapper.
快速为您解决问题:
public class LongLongMapper extends
Mapper<LongWritable, Text, LongWritable, LongWritable> {
@Override
protected void map(LongWritable key, Text value,
Mapper<LongWritable, Text, LongWritable, LongWritable>.Context context)
throws IOException, InterruptedException {
// assuming that your line contains key and value separated by \t
String[] split = value.toString().split("\t");
context.write(new LongWritable(Long.valueOf(split[0])), new LongWritable(
Long.valueOf(split[1])));
}
public static void main(String[] args) throws IOException,
InterruptedException, ClassNotFoundException {
Configuration conf = new Configuration();
Job job = new Job(conf);
job.setJobName("Convert Text");
job.setJarByClass(LongLongMapper.class);
job.setMapperClass(Mapper.class);
job.setReducerClass(Reducer.class);
// increase if you need sorting or a special number of files
job.setNumReduceTasks(0);
job.setOutputKeyClass(LongWritable.class);
job.setOutputValueClass(LongWritable.class);
job.setOutputFormatClass(SequenceFileOutputFormat.class);
job.setInputFormatClass(TextInputFormat.class);
FileInputFormat.addInputPath(job, new Path("/input"));
FileOutputFormat.setOutputPath(job, new Path("/output"));
// submit and wait for completion
job.waitForCompletion(true);
}
}
Run Code Online (Sandbox Code Playgroud)
mapper函数中的每个值都会得到一行输入,因此我们只需用分隔符(tab)将其拆分并将其各部分解析为long.
而已.
| 归档时间: |
|
| 查看次数: |
8710 次 |
| 最近记录: |