当对多个文本文件使用sqlContext.load
时,如何防止 Spark 将每个文件拆分为多个分区?这对于 gzip 文件来说不是问题,我希望它对于常规文本文件也能同样工作。
sc.wholeTextFile
可以工作,除非读取整个 100MB 文件需要 3G 内存,所以我宁愿使用某种流式传输,因为我们有时需要读取更大的文件。
最佳答案
可分割性是 InputFormat 的一项功能。 TextInputFormat具有取决于源的条件可分割性(纯文本、某些压缩文本可以分割,但 gzip 基本上不可分割)。
要获得您想要的行为,您只需将 TextInputFormat
扩展为您自己的 NonSplittingTextInputFormat
并重写 isSplittable 方法以始终返回 false。然后,您可以通过类似于 sc.textFile 中实现的代码来加载文件。 :
import org.apache.hadoop.fs.{FileSystem, Path}
class NonSplittingTextInputFormat extends TextInputFormat {
override protected def isSplitable(context: FileSystem, file: Path): Boolean = false
}
sc.hadoopFile(path, classOf[NonSplittableInputFormat], classOf[LongWritable], classOf[Text],
minPartitions).map(pair => pair._2.toString)
关于scala - 如何防止 Spark 拆分文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35347838/