/**
* @author HJX
* @version 1.0,2013-01-16
* @since JDK1.7,Ubuntu-12.04-64bit
* 在hadoop环境下运行
* 将一个String写入到本地lzo文件中(不是hadoop的hdfs上)
* 再从该lzo文件中读取出来并与原String进行校对
*/
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import com.hadoop.compression.lzo.LzopCodec;
public class LzoCompress {
/**
* @param args
*/
public static void main(String[] args) {
//生成数据
String dataSource = "abcdefghijklmnopqrstuvwxyz0123456789~!%#^@*#*%$(\n";
dataSource = dataSource.concat(dataSource);
dataSource = dataSource.concat(dataSource);
dataSource = dataSource.concat(dataSource);
/* System.out.println("dataSource = " + dataSource);*/
String lzoFilePath = "/home/hadoop/LzoCompressTest.lzo";
//写入到lzo文件,即lzo压缩
write2LzoFile(lzoFilePath, getDefaultConf(),dataSource.getBytes());
StringBuilder sb = new StringBuilder();
//读取lzo文件,即lzo解压缩
List lines = readLzoFile(lzoFilePath, getDefaultConf());
for(String line : lines) {
sb.append(line);
//LINUX/UNIX 下添加一个换行符
sb.append("\n");
/* //Windows 下添加一个换行符
sb.append("\r\n");*/
}
if (sb.toString().equals(dataSource)) {
System.out.println(sb.toString());
} else {
System.err.println("Error line : " + sb.toString());
}
}
private static Configuration getDefaultConf(){
Configuration conf = new Configuration();
conf.set("mapred.job.tracker", "local");
conf.set("fs.default.name", "file:///");
conf.set("io.compression.codecs", "com.hadoop.compression.lzo.LzoCodec");
return conf;
}
/**
* 写数据到lzo文件,即lzo压缩
* @param destLzoFilePath
* @param conf
* @param datas
* @return void
*/
public static void write2LzoFile(String destLzoFilePath,Configuration conf,byte[] datas) {
LzopCodec lzo = null;
OutputStream out = null;
try {
/* System.setProperty("java.library.path", "/usr/local/hadoop/lib/native/Linux-amd64-64/lib");*/
lzo = new LzopCodec();
lzo.setConf(conf);
out = lzo.createOutputStream(new FileOutputStream(destLzoFilePath));
out.write(datas);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 从lzo文件中读取数据,即lzo解压缩
* @param lzoFilePath
* @param conf
* @return void
*/
public static List readLzoFile(String lzoFilePath,Configuration conf) {
LzopCodec lzo = null;
InputStream is = null;
InputStreamReader isr = null;
BufferedReader reader = null;
List result = null;
String line = null;
try {
/* System.setProperty("java.library.path", "/usr/local/hadoop/lib/native/Linux-amd64-64/lib");*/
lzo = new LzopCodec();
lzo.setConf(conf);
is = lzo.createInputStream(new FileInputStream(lzoFilePath));
isr = new InputStreamReader(is);
reader = new BufferedReader(isr);
result = new ArrayList();
while((line = reader.readLine()) != null) {
result.add(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
if (isr != null) {
isr.close();
}
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}
程序是没有错的,但是一开始运行的时候总会提示无法读取libgplcompression这个库,其实我知道少了哪些库的,分别是
libgplcompression.a
libgplcompression.la
libgplcompression.so
libgplcompression.so.0
libgplcompression.so.0.0.0
可问题是把这些库放在哪里。尝试过把这几个库放在$CLASSPATH下面,但没用。于是查看了错误提示,提示缺少的这个库在com.hadoop.compression.lzo.GPLNativeCodeLoader这个类里面被引用到,于是看了一下hadoop-lzo-0.45.jar的源文件(当时编译hadoop-lzo-0.45.jar时留下的源文件,在kevinweil-hadoop-lzo-6bb1b7f/src/java/com/hadoop/compression/lzo/里),GPLNativeCodeLoader.java的内容是这样的:
package com.hadoop.compression.lzo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class GPLNativeCodeLoader {
private static final Log LOG = LogFactory.getLog(GPLNativeCodeLoader.class);
private static boolean nativeLibraryLoaded = false;
static {
try {
//try to load the lib
System.loadLibrary("gplcompression");
nativeLibraryLoaded = true;
LOG.info("Loaded native gpl library");
} catch (Throwable t) {
LOG.error("Could not load native gpl library", t);
nativeLibraryLoaded = false;
}
}
/**
* Are the native gpl libraries loaded?
* @return true if loaded, otherwise false
*/
public static boolean isNativeCodeLoaded() {
return nativeLibraryLoaded;
}
}