gpt4 book ai didi

java - Spark 读取 .7z 文件

转载 作者:行者123 更新时间:2023-12-03 19:01:59 25 4
gpt4 key购买 nike

我正在尝试使用 scala 或 java 读取 spark .7z 文件。我没有找到任何合适的方法或功能。
对于 zip 文件,我能够读取 ZipInputStream 类采用 Input 流,但对于 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();
}
}
经过这么多年的 Spark 进化,应该有简单的方法来做到这一点。

最佳答案

而不是使用 java.io.File基于方法,你可以试试 SeekableByteChannel方法如下所示alternative constructor .
您可以使用 SeekableInMemoryByteChannel读取字节数组。因此,只要您可以从 S3 或其他任何地方获取 7zip 文件并将它们作为字节数组传递,您就应该没问题。
尽管如此,Spark 确实不太适合处理诸如 zip 和 7zip 文件之类的内容。我可以根据个人经验告诉您,一旦文件太大而 Spark 的执行程序无法处理,它就会严重失败。
像 Apache NiFi 这样的东西可以更好地扩展文件和处理它们。 FWIW,我目前正在处理一个大型数据转储,我经常处理其中包含数百万个文件的 50GB tarball,而 NiFi 处理它们非常优雅。

关于java - Spark 读取 .7z 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64489360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com