读取 Snappy 压缩文件时出错
Error while reading Snappy compressed file
我正在通过 java
从本地阅读 Snappy Compressed file
。
File snappyFile = new File(fileName);
Configuration conf = new Configuration();
CompressionCodec codec = (CompressionCodec)
ReflectionUtils.newInstance(SnappyCodec.class, conf);
FileInputStream is2 = new FileInputStream(snappyFile);
CompressionInputStream cis = codec.createInputStream(is2);
BufferedReader cr = new BufferedReader(new InputStreamReader(cis));
我的代码在 ReflectionUtils.newInstance
出现异常
Exception in thread "main" java.lang.RuntimeException: java.lang.NoSuchMethodException: org.xerial.snappy.SnappyCodec.<init>()
at org.apache.hadoop.util.ReflectionUtils.newInstance(ReflectionUtils.java:128)
at org.finra.oats.AWS.AWSnappyRead.run(AWSnappyRead.java:64)
at org.finra.oats.AWS.AWSnappyRead.main(AWSnappyRead.java:48)
Caused by: java.lang.NoSuchMethodException: org.xerial.snappy.SnappyCodec.<init>()
at java.lang.Class.getConstructor0(Class.java:2849)
at java.lang.Class.getDeclaredConstructor(Class.java:2053)
at org.apache.hadoop.util.ReflectionUtils.newInstance(ReflectionUtils.java:122)
... 2 more
是不是版本不匹配的问题。我正在使用 snappy 1.1.1.2
版本
要读取和写入 Snappy 压缩文件,您可以使用提供的 SnappyInputStream / SnappyOutputStream 类.
String fileName = "foo.snap";
// write a snappy compressed file
try (OutputStream os = new SnappyOutputStream(new FileOutputStream(fileName))) {
os.write("Hello Snappy-World".getBytes(Charset.defaultCharset()));
}
// read a snappy compressed file
try (InputStream is = new SnappyInputStream(new FileInputStream(fileName))) {
byte[] bytes = new byte[100];
is.read(bytes);
System.out.println(new String(bytes, Charset.defaultCharset()));
}
// check if the file is compressed with the snappy algorithm
try (InputStream is = new FileInputStream(fileName)) {
SnappyCodec readHeader = SnappyCodec.readHeader(is);
if (readHeader.isValidMagicHeader()) {
System.out.println("is a Snappy compressed file");
System.out.printf("%s: %d%n%s: %d%n",
"compatible version", readHeader.compatibleVersion,
"version", readHeader.version
);
} else {
System.out.println("is not a Snappy compressed file");
}
}
我正在通过 java
从本地阅读 Snappy Compressed file
。
File snappyFile = new File(fileName);
Configuration conf = new Configuration();
CompressionCodec codec = (CompressionCodec)
ReflectionUtils.newInstance(SnappyCodec.class, conf);
FileInputStream is2 = new FileInputStream(snappyFile);
CompressionInputStream cis = codec.createInputStream(is2);
BufferedReader cr = new BufferedReader(new InputStreamReader(cis));
我的代码在 ReflectionUtils.newInstance
出现异常Exception in thread "main" java.lang.RuntimeException: java.lang.NoSuchMethodException: org.xerial.snappy.SnappyCodec.<init>()
at org.apache.hadoop.util.ReflectionUtils.newInstance(ReflectionUtils.java:128)
at org.finra.oats.AWS.AWSnappyRead.run(AWSnappyRead.java:64)
at org.finra.oats.AWS.AWSnappyRead.main(AWSnappyRead.java:48)
Caused by: java.lang.NoSuchMethodException: org.xerial.snappy.SnappyCodec.<init>()
at java.lang.Class.getConstructor0(Class.java:2849)
at java.lang.Class.getDeclaredConstructor(Class.java:2053)
at org.apache.hadoop.util.ReflectionUtils.newInstance(ReflectionUtils.java:122)
... 2 more
是不是版本不匹配的问题。我正在使用 snappy 1.1.1.2
版本
要读取和写入 Snappy 压缩文件,您可以使用提供的 SnappyInputStream / SnappyOutputStream 类.
String fileName = "foo.snap";
// write a snappy compressed file
try (OutputStream os = new SnappyOutputStream(new FileOutputStream(fileName))) {
os.write("Hello Snappy-World".getBytes(Charset.defaultCharset()));
}
// read a snappy compressed file
try (InputStream is = new SnappyInputStream(new FileInputStream(fileName))) {
byte[] bytes = new byte[100];
is.read(bytes);
System.out.println(new String(bytes, Charset.defaultCharset()));
}
// check if the file is compressed with the snappy algorithm
try (InputStream is = new FileInputStream(fileName)) {
SnappyCodec readHeader = SnappyCodec.readHeader(is);
if (readHeader.isValidMagicHeader()) {
System.out.println("is a Snappy compressed file");
System.out.printf("%s: %d%n%s: %d%n",
"compatible version", readHeader.compatibleVersion,
"version", readHeader.version
);
} else {
System.out.println("is not a Snappy compressed file");
}
}