mapreduce读取sequencefile文件中的数据

sequencefile中的数据是以key,value对存储的。
通过mapreduce模式,可以读取sequencefile中的数据。
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);
	}

}

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值