【发布时间】:2021-02-05 21:22:24
【问题描述】:
我正在尝试使用 scala 或 java 读取 spark .7z 文件。我没有找到任何合适的方法或功能。
对于 zip 文件,我可以读取 ZipInputStream 类采用输入流,但对于 7Z 文件,SevenZFile 类不采用任何输入流。 https://commons.apache.org/proper/commons-compress/javadocs/api-1.16/org/apache/commons/compress/archivers/sevenz/SevenZFile.html
压缩文件代码
spark.sparkContext.binaryFiles("fileName").flatMap{case (name: String, content: PortableDataStream) =>
val zis = new ZipInputStream(content.open)
Stream.continually(zis.getNextEntry)
.takeWhile(_ != null)
.flatMap { _ =>
val br = new BufferedReader(new InputStreamReader(zis))
Stream.continually(br.readLine()).takeWhile(_ != null)
}}
我正在为 7z 文件尝试类似的代码,例如
spark.sparkContext.binaryFiles(""filename"").flatMap{case (name: String, content: PortableDataStream) =>
val zis = new SevenZFile(content.open)
Stream.continually(zis.getNextEntry)
.takeWhile(_ != null)
.flatMap { _ =>
val br = new BufferedReader(new InputStreamReader(zis))
Stream.continually(br.readLine()).takeWhile(_ != null)
}}
但是 SevenZFile 不接受这些格式。寻找想法。
如果文件在本地文件系统中,则以下解决方案有效,但我的文件在 hdfs 中
本地文件系统代码
public static void decompress(String in, File destination) throws IOException {
SevenZFile sevenZFile = new SevenZFile(new File(in));
SevenZArchiveEntry entry;
while ((entry = sevenZFile.getNextEntry()) != null){
if (entry.isDirectory()){
continue;
}
File curfile = new File(destination, entry.getName());
File parent = curfile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
FileOutputStream out = new FileOutputStream(curfile);
byte[] content = new byte[(int) entry.getSize()];
sevenZFile.read(content, 0, content.length);
out.write(content);
out.close();
}
}
经过这么多年的火花进化,应该有简单的方法来做到这一点。
【问题讨论】:
-
如果文件以
example.7z的二进制形式存储在hdfs中,你能解释一下吗? -
@silentsudo,是的,文件存储在 hdfs 中
-
@silentsudo 文件存储在 hdfs 中。
标签: java scala apache-spark hdfs 7zip