mapreduce读取sequencefile文件中的数据

[java]  view plain copy
  1. sequencefile中的数据是以key,value对存储的。  
[java]  view plain copy
  1. 通过mapreduce模式,可以读取sequencefile中的数据。  
[java]  view plain copy
  1. public class MapReduceReadFile {  
  2.       
  3.     private static SequenceFile.Reader reader = null;  
  4.     private static Configuration conf = new Configuration();  
  5.   
  6.     public static class ReadFileMapper extends  
  7.             Mapper<LongWritable, Text, LongWritable, Text> {  
  8.   
  9.         /* (non-Javadoc) 
  10.          * @see org.apache.hadoop.mapreduce.Mapper#map(KEYIN, VALUEIN, org.apache.hadoop.mapreduce.Mapper.Context) 
  11.          */  
  12.         @Override  
  13.         public void map(LongWritable key, Text value, Context context) {  
  14.             key = (LongWritable) ReflectionUtils.newInstance(  
  15.                     reader.getKeyClass(), conf);  
  16.             value = (Text) ReflectionUtils.newInstance(  
  17.                     reader.getValueClass(), conf);  
  18.             try {  
  19.                 while (reader.next(key, value)) {  
  20.                     System.out.printf("%s\t%s\n", key, value);  
  21.                     context.write(key, value);  
  22.                 }  
  23.             } catch (IOException e1) {  
  24.                 e1.printStackTrace();  
  25.             } catch (InterruptedException e) {  
  26.                 e.printStackTrace();  
  27.             }  
  28.         }  
  29.   
  30.     }  
  31.     /** 
  32.      * @param args 
  33.      * @throws IOException 
  34.      * @throws InterruptedException 
  35.      * @throws ClassNotFoundException 
  36.      */  
  37.     public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {  
  38.           
  39.         Job job = new Job(conf,"read seq file");  
  40.         job.setJarByClass(MapReduceReadFile.class);  
  41.         job.setMapperClass(ReadFileMapper.class);  
  42.         job.setMapOutputValueClass(Text.class);  
  43.         Path path = new Path("logfile2");  
  44.         FileSystem fs = FileSystem.get(conf);  
  45.         reader = new SequenceFile.Reader(fs, path, conf);  
  46.         FileInputFormat.addInputPath(job, path);  
  47.         FileOutputFormat.setOutputPath(job, new Path("result"));  
  48.         System.exit(job.waitForCompletion(true)?0:1);  
  49.     }  
  50.   
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值