如果需要在mapreduce中进行SequenceFile的读取和写入,则需要到SequcenFileInputFormat和SequenceFileOutputFormat,示例代码如下所示:
1)输出格式为SequenceFileOutputFormat
public class SequenceFileOutputFormatDemo extends Configured implements Tool {
public static class SequenceFileOutputFormatDemoMapper extends
Mapper<LongWritable, Text, LongWritable, Text> {
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
context.write(key, value);
}
}
public static void main(String[] args) throws Exception {
int nRet = ToolRunner.run(new Configuration(),
new SequenceFileOutputFormatDemo(), args);
System.out.println(nRet);
}
@Override
public int run(String[] args) throws Exception {
// TODO Auto-generated method stub
Configuration conf = getConf();
Job job = new Job(conf, "sequence file output demo ");
job.setJarByClass(SequenceFileOutputFormatDemo.class);
FileInputFormat.addInputPaths(job, args[0]);
HdfsUtil.deleteDir(args[1]);
job.setMapperClass(SequenceFileOutputFormatDemoMapper.class);
// 因为没有reducer,所以map的输出为job的最后输出,所以需要把outputkeyclass
// outputvalueclass设置为与map的输出一致
job.setOutputKeyClass(LongWritable.class);
job.setOutputValueClass(Text.class);
// 如果不希望有reducer,设置为0
job.setNumReduceTasks(0);
// 设置输出类
job.setOutputFormatClass(SequenceFileOutputFormat.class);
// 设置sequecnfile的格式,对于sequencefile的输出格式,有多种组合方式,
//从下面的模式中选择一种,并将其余的注释掉
// 组合方式1:不压缩模式
SequenceFileOutputFormat.setOutputCompressionType(job,
CompressionType.NONE);
// 组合方式2:record压缩模式,并指定采用的压缩方式 :默认、gzip压缩等
// SequenceFileOutputFormat.setOutputCompressionType(job,
// CompressionType.RECORD);
// SequenceFileOutputFormat.setOutputCompressorClass(job,
// DefaultCodec.class);
// 组合方式3:block压缩模式,并指定采用的压缩方式 :默认、gzip压缩等
// SequenceFileOutputFormat.setOutputCompressionType(job,
// CompressionType.BLOCK);
// SequenceFileOutputFormat.setOutputCompressorClass(job,
// DefaultCodec.class);
SequenceFileOutputFormat.setOutputPath(job, new Path(args[1]));
int result = job.waitForCompletion(true) ? 0 : 1;
return result;
}
}
2)输入格式为SequcenFileInputFormat
public class SequenceFileInputFormatDemo extends Configured implements Tool {
public static class SequenceFileInputFormatDemoMapper extends
Mapper<LongWritable, Text, Text, NullWritable> {
public void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
System.out.println("key: " + key.toString() + " ; value: "
+ value.toString());
}
}
public static void main(String[] args) throws Exception {
int nRet = ToolRunner.run(new Configuration(),
new SequenceFileInputFormatDemo(), args);
System.out.println(nRet);
}
@Override
public int run(String[] args) throws Exception {
Configuration conf = getConf();
Job job = new Job(conf, "sequence file input demo");
job.setJarByClass(SequenceFileInputFormatDemo.class);
FileInputFormat.addInputPaths(job, args[0]);
HdfsUtil.deleteDir(args[1]);
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.setMapperClass(SequenceFileInputFormatDemoMapper.class);
job.setNumReduceTasks(1);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
job.setInputFormatClass(SequenceFileInputFormat.class);
int result = job.waitForCompletion(true) ? 0 : 1;
return result;
}
}
或者读取的时候也可以如下面的方式进行读取,但是此时输出格式就为普通FileOutputFormat了,输入格式也为普通FileInputFormat了。示例代码如下面所示 :
public class MapReduceReadFile {
private static SequenceFile.Reader reader = null;
private static Configuration conf = new Configuration();
public static class ReadFileMapper extends
Mapper<LongWritable, Text, LongWritable, Text> {
/* (non-Javadoc)
* @see org.apache.Hadoop.mapreduce.Mapper#map(KEYIN, VALUEIN, org.apache.hadoop.mapreduce.Mapper.Context)
*/
@Override
public void map(LongWritable key, Text value, Context context) {
key = (LongWritable) ReflectionUtils.newInstance(
reader.getKeyClass(), conf);
value = (Text) ReflectionUtils.newInstance(
reader.getValueClass(), conf);
try {
while (reader.next(key, value)) {
System.out.printf("%s\t%s\n", key, value);
context.write(key, value);
}
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* @param args
* @throws IOException
* @throws InterruptedException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
Job job = new Job(conf,"read seq file");
job.setJarByClass(MapReduceReadFile.class);
job.setMapperClass(ReadFileMapper.class);
job.setMapOutputValueClass(Text.class);
Path path = new Path("logfile2");
FileSystem fs = FileSystem.get(conf);
reader = new SequenceFile.Reader(fs, path, conf);
FileInputFormat.addInputPath(job, path);
FileOutputFormat.setOutputPath(job, new Path("result"));
System.exit(job.waitForCompletion(true)?0:1);
}
}
更多Hadoop相关信息见Hadoop 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=13
本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-10/107989.htm
