读取nutch爬取的数据内容

  1. package org.apache.nutch.util;  
  2.   
  3. import java.io.IOException;     
  4.    
  5. import org.apache.hadoop.conf.Configuration;     
  6. import org.apache.hadoop.conf.Configured;     
  7. import org.apache.hadoop.fs.FileSystem;     
  8. import org.apache.hadoop.fs.Path;     
  9. import org.apache.hadoop.io.DataOutputBuffer;     
  10. import org.apache.hadoop.io.SequenceFile;     
  11. import org.apache.hadoop.io.Text;     
  12. import org.apache.hadoop.io.Writable;     
  13. import org.apache.hadoop.util.Tool;     
  14. import org.apache.hadoop.util.ToolRunner;     
  15. /**   
  16.  * 读取SequenceFile的信息  
  17.  */    
  18. public class SequenceFileReader<K,V> extends Configured implements Tool{     
  19.      private boolean more = true;     
  20.      private K key = null;     
  21.      private V value = null;     
  22.      private SequenceFile.Reader in;           
  23.          
  24.      public boolean nextKeyValue() throws IOException, InterruptedException {     
  25.             if (!more) {     
  26.               return false;     
  27.            }     
  28.            long pos = in.getPosition();     
  29.             key = (K) in.next(key);     
  30.             if (key == null ) {     
  31.               more = false;     
  32.               key = null;     
  33.               value = null;     
  34.             } else {     
  35.               value = (V) in.getCurrentValue(value);     
  36.             }     
  37.             return more;     
  38.           }     
  39.           public K getCurrentKey() {     
  40.             return key;     
  41.           }     
  42.                
  43.           public V getCurrentValue() {     
  44.             return value;     
  45.           }     
  46.     
  47.     @Override    
  48.     public int run(String[] arg0) throws Exception {     
  49.         Configuration conf = this.getConf();     
  50.         in = new SequenceFile.Reader(FileSystem.get(conf),new Path(arg0[0]),conf);     
  51.         DataOutputBuffer outBuf = new DataOutputBuffer();     
  52.         while(this.nextKeyValue()){     
  53.             System.out.println(this.getCurrentKey());     
  54.             System.out.println(this.getCurrentValue());     
  55.         }     
  56.         return 0;     
  57.     }     
  58.          
  59.     public static void main(String[] args) {     
  60.         try{     
  61.          String file = "C:/nutch-1.0/crawled/segments/20100624073431/content/part-00000/data";     
  62.          if(null == args || args.length == 0)     
  63.              args = new String[]{file};     
  64.          int res = ToolRunner.run(new Configuration(), new SequenceFileReader<Text,Writable>(), args);     
  65.          System.exit(res);     
  66.         }catch(Exception e){     
  67.             e.printStackTrace();     
  68.         }     
  69.     }     
  70. }    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值