【发布时间】:2016-10-27 13:31:36
【问题描述】:
我有一个 S3 存储桶,里面装满了没有文件扩展名的 Gz 文件。例如s3://mybucket/1234502827-34231
sc.textFile 使用该文件扩展名来选择解码器。我发现了很多关于处理自定义文件扩展名的博客文章,但没有关于缺少文件扩展名的内容。
我认为解决方案可能是sc.binaryFiles 并手动解压缩文件。
另一种可能性是弄清楚sc.textFile 是如何找到文件格式的。我不清楚这些classOf[] 调用的作用。
def textFile(
path: String,
minPartitions: Int = defaultMinPartitions): RDD[String] = withScope {
assertNotStopped()
hadoopFile(path, classOf[TextInputFormat], classOf[LongWritable], classOf[Text],
minPartitions).map(pair => pair._2.toString).setName(path)
}
【问题讨论】:
-
sc.textFile无法确定格式。由TextInputFormat完成,仅使用扩展名。 -
或者重命名s3中的所有文件,加上
.gz。我查看了源代码,它在此处实现:hadoop.apache.org/docs/stable/api/src-html/org/apache/hadoop/io/… 它确实使用了文件扩展名。该规范建议您可以只查看前几个字节 zlib.org/rfc-gzip.html#file-format ,但这表明您可能会得到误报并且必须考虑字节序 stackoverflow.com/questions/6059302/… 所以毫无疑问,使用.gz是一种更快、更可靠的约定跨度> -
@user6022341
TextInputFormat没有这样做,是这个类中的getCodec(Path file)方法org.apache.hadoop.io.compress.CompressionCodecFactory
标签: apache-spark