Hadoop Core 学习笔记(二) lzo文件的写入和读取

zhuanzi  :http://guoyunsky.iteye.com/blog/1266226

 压缩是绕不开的话题,因为当今很多程序的压力还是在IO.特别是Hadoop这种分布式存储和运算框架,单台机器的IO,网络通信IO都是压力和挑战.关于Hadoop为什么要用Lzo来压缩而没有选用传统的压缩方法,我这里不再阐述.相关资料很多.有兴趣的可以查看cloudera这篇博客:http://www.cloudera.com/blog/2009/11/hadoop-at-twitter-part-1-splittable-lzo-compression/

      这里只是用于读写lzo文件,具体请看代码吧.

 

Java代码   收藏代码
  1. package com.guoyun.hadoop.io.study;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.io.InputStreamReader;  
  10. import java.io.OutputStream;  
  11. import java.util.ArrayList;  
  12. import java.util.List;  
  13.   
  14. import org.apache.hadoop.conf.Configuration;  
  15.   
  16. import com.hadoop.compression.lzo.LzopCodec;  
  17.   
  18. /** 
  19.  * 读写Lzo文件 
  20.  */  
  21. public class LzoFileStudy {  
  22.     
  23.   private static Configuration getDefaultConf(){  
  24.     Configuration conf=new Configuration();  
  25.     conf.set("mapred.job.tracker""local");  
  26.     conf.set("fs.default.name""file:///");  
  27.     conf.set("io.compression.codecs""com.hadoop.compression.lzo.LzoCodec");  
  28.     return conf;  
  29.   }  
  30.     
  31.   /** 
  32.    * 写入数据到lzo文件 
  33.    *  
  34.    * @param destLzoFilePath 
  35.    * @param conf 
  36.    * @param datas 
  37.    */  
  38.   public static void write2LzoFile(String destLzoFilePath,Configuration conf,byte[] datas){  
  39.     LzopCodec lzo=null;  
  40.     OutputStream out=null;  
  41.       
  42.     try {  
  43.       lzo=new LzopCodec();  
  44.       lzo.setConf(conf);  
  45.       out=lzo.createOutputStream(new FileOutputStream(destLzoFilePath));  
  46.       out.write(datas);  
  47.     } catch (FileNotFoundException e) {  
  48.       // TODO Auto-generated catch block  
  49.       e.printStackTrace();  
  50.     } catch (IOException e) {  
  51.       // TODO Auto-generated catch block  
  52.       e.printStackTrace();  
  53.     }finally{  
  54.       try {  
  55.         if(out!=null){  
  56.           out.close();  
  57.         }  
  58.       } catch (IOException e) {  
  59.         // TODO Auto-generated catch block  
  60.         e.printStackTrace();  
  61.       }  
  62.     }  
  63.       
  64.   }  
  65.     
  66.   /** 
  67.    * 从lzo文件中读取数据 
  68.    *  
  69.    * @param lzoFilePath 
  70.    * @param conf 
  71.    * @return 
  72.    */  
  73.   public static List<String> readLzoFile(String lzoFilePath,Configuration conf){  
  74.     LzopCodec lzo=null;  
  75.     InputStream is=null;  
  76.     InputStreamReader isr=null;  
  77.     BufferedReader reader=null;  
  78.     List<String> result=null;  
  79.     String line=null;  
  80.       
  81.     try {  
  82.       lzo=new LzopCodec();  
  83.       lzo.setConf(conf);  
  84.       is=lzo.createInputStream(new FileInputStream(lzoFilePath));  
  85.       isr=new InputStreamReader(is);  
  86.       reader=new BufferedReader(isr);  
  87.       result=new ArrayList<String>();  
  88.       while((line=reader.readLine())!=null){  
  89.         result.add(line);  
  90.       }  
  91.         
  92.     } catch (FileNotFoundException e) {  
  93.       // TODO Auto-generated catch block  
  94.       e.printStackTrace();  
  95.     } catch (IOException e) {  
  96.       // TODO Auto-generated catch block  
  97.       e.printStackTrace();  
  98.     }finally{  
  99.       try {  
  100.         if(reader!=null){  
  101.           reader.close();  
  102.         }  
  103.         if(isr!=null){  
  104.           isr.close();  
  105.         }  
  106.         if(is!=null){  
  107.           is.close();  
  108.         }  
  109.       } catch (IOException e) {  
  110.         // TODO Auto-generated catch block  
  111.         e.printStackTrace();  
  112.       }  
  113.     }  
  114.     return result;  
  115.   }  
  116.     
  117.   /** 
  118.    * @param args 
  119.    */  
  120.   public static void main(String[] args) {  
  121.     // 生成数据  
  122.     String dataSource="abcdefghijklmnopqrstuvwxyz0123456789~!@#¥%……&*()——+\r";  
  123.     dataSource=dataSource.concat(dataSource);  
  124.     dataSource=dataSource.concat(dataSource);  
  125.     dataSource=dataSource.concat(dataSource);  
  126.       
  127.     String lzoFilePath="./data/test.lzo";  
  128.     // 写入到lzo文件  
  129.     write2LzoFile(lzoFilePath,getDefaultConf(),dataSource.getBytes());  
  130.     StringBuilder sb=new StringBuilder();  
  131.     // 读取lzo文件  
  132.     List<String> lines=readLzoFile(lzoFilePath,getDefaultConf());  
  133.     for(String line:lines){  
  134.       sb.append(line);   
  135.       sb.append("\r");  
  136.     }  
  137.     // 数据是否一致  
  138.     if(sb.toString().equals(dataSource)){  
  139.       System.out.println(sb.toString());  
  140.     }else{  
  141.       System.err.println("Error line:"+sb.toString());  
  142.     }  
  143.       
  144.   }  
  145.   
  146. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值