手机版
你好,游客 登录 注册
背景:
阅读新闻

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

[日期:2012-08-01] 来源:Linux社区  作者:guoyunsky [字体:大 中 小]

压缩是绕不开的话题,因为当今很多程序的压力还是在IO.特别是Hadoop这种分布式存储和运算框架,单台机器的IO,网络通信IO都是压力和挑战.关于Hadoop为什么要用Lzo来压缩而没有选用传统的压缩方法,我这里不再阐述。

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

  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. }  
linux
相关资讯       Hadoop部署  Hadoop Core 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款