无法将snappycodec与hadoop jar:nullpointerexception一起使用

j2qf4p5b  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(308)

我试图在一个简单的java程序中使用hadoop的压缩库。但是,我无法使用snappy编解码器:执行会产生 NullPointerException in方法 SnappyCodec.createCompressor .
注意,我没有得到典型的 java.lang.UnsatisfiedLinkError 这可能是因为没有设置ld\u library\u path和java\u library\u path环境变量。snappy与cdh一起正确安装,通过运行 hadoop checknative 当我做手术的时候,快速减压很管用 hdfs dfs -text 在一个快速的文件上。

$ hadoop jar SnappyTool-0.0.1-SNAPSHOT.jar com.mycorp.SnappyCompressor
Exception in thread "main" java.lang.NullPointerException
        at org.apache.hadoop.io.compress.SnappyCodec.createCompressor(SnappyCodec.java:145)
        at org.apache.hadoop.io.compress.CodecPool.getCompressor(CodecPool.java:152)
        at org.apache.hadoop.io.compress.CodecPool.getCompressor(CodecPool.java:165)
        at com.mycorp.SnappyCompressor.main(SnappyCompressor.java:19)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.hadoop.util.RunJar.run(RunJar.java:221)
        at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
$
$ hadoop checknative | grep snappy 2>/dev/null
$ snappy:  true /opt/cloudera/parcels/CDH-5.5.1-1.cdh5.5.1.p0.11/lib/hadoop/lib/native/libsnappy.so.1
$ ls /opt/cloudera/parcels/CDH-5.5.1-1.cdh5.5.1.p0.11/lib/hadoop/lib/native/
libhadoop.a       libhadoop.so.1.0.0  libnativetask.a         libsnappy.so
libhadooppipes.a  libhadooputils.a    libnativetask.so        libsnappy.so.1
libhadoop.so      libhdfs.a           libnativetask.so.1.0.0
libsnappy.so.1.1.4
$ export LD_LIBRARY_PATH=/opt/cloudera/parcels/CDH-5.5.1-1.cdh5.5.1.p0.11/lib/hadoop/lib/native/
$ java -Djava.library.path=/opt/cloudera/parcels/CDH-5.5.1-1.cdh5.5.1.p0.11/lib/hadoop/lib/native/ -cp `hadoop classpath`:SnappyTool-0.0.1-SNAPSHOT.jar com.mycorp.SnappyCompressor
Exception in thread "main" java.lang.NullPointerException
        at org.apache.hadoop.io.compress.SnappyCodec.createCompressor(SnappyCodec.java:145)
        at org.apache.hadoop.io.compress.CodecPool.getCompressor(CodecPool.java:152)
        at org.apache.hadoop.io.compress.CodecPool.getCompressor(CodecPool.java:165)
        at com.mycorp.SnappyCompressor.main(SnappyCompressor.java:19)

java代码如下所示,最后一行是罪魁祸首:

SnappyCodec.checkNativeCodeLoaded();
        CompressionCodec codec = new SnappyCodec();
        Compressor comp = CodecPool.getCompressor(codec);

我错过了什么?

gorkyyrv

gorkyyrv1#

好吧,问题是 CompressionCodec 需要适当的配置,如本回答中所指出的。
配置snappy压缩机的简单方法如下:

Configuration conf = new Configuration();
CompressionCodecFactory ccf = new CompressionCodecFactory(conf);
CompressionCodec codec = ccf.getCodecByClassName(SnappyCodec.class.getName());
Compressor comp = codec.createCompressor();

生成的jar可以与原始问题中使用的命令行一起运行。

相关问题