从 Spark 中的压缩中读取整个文本文件
我有以下问题:假设我有一个包含压缩目录的目录,其中包含存储在HDFS上的多个文件。我想创建一个由一些T类型的对象组成的RDD,即:
context = new JavaSparkContext(conf);
JavaPairRDD<String, String> filesRDD = context.wholeTextFiles(inputDataPath);
JavaPairRDD<String, String> filesRDD = context.wholeTextFiles(inputDataPath);
JavaRDD<T> processingFiles = filesRDD.map(fileNameContent -> {
// The name of the file
String fileName = fileNameContent._1();
// The content of the file
String content = fileNameContent._2();
// Class T has a constructor of taking the filename and the content of each
// processed file (as two strings)
T t = new T(content, fileName);
return t;
});
现在,当包含文件的目录时,这可以完全正常工作,即当它类似于以下内容时:inputDataPath
String inputDataPath = "hdfs://some_path/*/*/"; // because it contains subfolders
但是,当有一个包含多个文件的tgz时,文件内容()会给我一些无用的二进制字符串(非常预期)。我在SO上发现了一个类似的问题,但情况并非如此,因为解决方案是每次压缩仅由一个文件组成时,在我的情况下,还有许多其他文件,我想将它们作为整个文件单独读取。我还发现了一个关于 的问题,但这在我的情况下不起作用。fileNameContent._2()
wholeTextFiles
任何想法如何做到这一点?
编辑:
我尝试从这里与读者(试图从这里测试读者,就像在函数中一样),但每当我调用testTarballWithFolders()
TarballReader tarballReader = new TarballReader(fileName);
我得到:NullPointerException
java.lang.NullPointerException
at java.util.zip.InflaterInputStream.<init>(InflaterInputStream.java:83)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:77)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:91)
at utils.TarballReader.<init>(TarballReader.java:61)
at main.SparkMain.lambda$0(SparkMain.java:105)
at main.SparkMain$$Lambda$18/1667100242.call(Unknown Source)
at org.apache.spark.api.java.JavaPairRDD$$anonfun$toScalaFunction$1.apply(JavaPairRDD.scala:1015)
at scala.collection.Iterator$$anon$11.next(Iterator.scala:328)
at scala.collection.Iterator$class.foreach(Iterator.scala:727)
at scala.collection.AbstractIterator.foreach(Iterator.scala:1157)
at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:48)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:103)
at scala.collection.mutable.ArrayBuffer.$plus$plus$eq(ArrayBuffer.scala:47)
at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:273)
at scala.collection.AbstractIterator.to(Iterator.scala:1157)
at scala.collection.TraversableOnce$class.toBuffer(TraversableOnce.scala:265)
at scala.collection.AbstractIterator.toBuffer(Iterator.scala:1157)
at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:252)
at scala.collection.AbstractIterator.toArray(Iterator.scala:1157)
at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$12.apply(RDD.scala:927)
at org.apache.spark.rdd.RDD$$anonfun$collect$1$$anonfun$12.apply(RDD.scala:927)
at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1858)
at org.apache.spark.SparkContext$$anonfun$runJob$5.apply(SparkContext.scala:1858)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:66)
at org.apache.spark.scheduler.Task.run(Task.scala:89)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:214)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
第105行是我在编辑帖子时在上面显示的,第61行是MainSpark
TarballReader
GZIPInputStream gzip = new GZIPInputStream(in);
它为上行中的输入流提供空值:in
InputStream in = this.getClass().getResourceAsStream(tarball);
我在这里走在正确的道路上吗?如果是这样,我该如何继续?为什么我会得到这个空值,我该如何修复它?