壓縮是繞不開的話題,因?yàn)楫?dāng)今很多程序的壓力還是在IO.特別是Hadoop這種分布式存儲(chǔ)和運(yùn)算框架,單臺(tái)機(jī)器的IO,網(wǎng)絡(luò)通信IO都是壓力和挑戰(zhàn).關(guān)于Hadoop為什么要用Lzo來壓縮而沒有選用傳統(tǒng)的壓縮方法,我這里不再闡述。
這里只是用于讀寫lzo文件,具體請(qǐng)看代碼吧.
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. * 寫入數(shù)據(jù)到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文件中讀取數(shù)據(jù)
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. // 生成數(shù)據(jù) 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. // 數(shù)據(jù)是否一致 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.}
本文出自:億恩科技【1tcdy.com】
服務(wù)器租用/服務(wù)器托管中國五強(qiáng)!虛擬主機(jī)域名注冊(cè)頂級(jí)提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM]
|