1. <var id="fe6gj"></var>

    <rp id="fe6gj"><nav id="fe6gj"></nav></rp>

    <noframes id="fe6gj"><cite id="fe6gj"></cite>

    <ins id="fe6gj"><button id="fe6gj"><p id="fe6gj"></p></button></ins>
    1. <tt id="fe6gj"><i id="fe6gj"><sub id="fe6gj"></sub></i></tt>
        始創于2000年 股票代碼:831685
        咨詢熱線:0371-60135900 注冊有禮 登錄
        • 掛牌上市企業
        • 60秒人工響應
        • 99.99%連通率
        • 7*24h人工
        • 故障100倍補償
        您的位置: 網站首頁 > 幫助中心>文章內容

        Hadoop Core 學習筆記(二) lzo文件的寫入和讀取

        發布時間:  2012/8/6 18:27:28

        壓縮是繞不開的話題,因為當今很多程序的壓力還是在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.} 


         


        本文出自:億恩科技【www.endtimedelusion.com】

        服務器租用/服務器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質保障!--億恩科技[ENKJ.COM]

      1. 您可能在找
      2. 億恩北京公司:
      3. 經營性ICP/ISP證:京B2-20150015
      4. 億恩鄭州公司:
      5. 經營性ICP/ISP/IDC證:豫B1.B2-20060070
      6. 億恩南昌公司:
      7. 經營性ICP/ISP證:贛B2-20080012
      8. 服務器/云主機 24小時售后服務電話:0371-60135900
      9. 虛擬主機/智能建站 24小時售后服務電話:0371-60135900
      10. 專注服務器托管17年
        掃掃關注-微信公眾號
        0371-60135900
        Copyright© 1999-2019 ENKJ All Rights Reserved 億恩科技 版權所有  地址:鄭州市高新區翠竹街1號總部企業基地億恩大廈  法律顧問:河南亞太人律師事務所郝建鋒、杜慧月律師   京公網安備41019702002023號
          1
         
         
         
         

        0371-60135900
        7*24小時客服服務熱線

         
         
        av不卡不卡在线观看_最近2018年中文字幕_亚洲欧美一区二区三区_一级A爱做片免费观看国产_日韩在线中文天天更新_伊人中文无码在线