二进制文件数据源
自Spark 3.0以来,Spark支持二进制文件数据源, 它读取二进制文件并将每个文件转换为一个单独的记录,包含文件的原始内容 和元数据。 它生成一个包含以下列和可能的分区列的DataFrame:
-
path
: 字符串类型 -
modificationTime
: 时间戳类型 -
length
: 长整型 -
content
: 二进制类型
要读取整个二进制文件,您需要将数据源
format
指定为
binaryFile
。
要加载与给定 glob 模式匹配的路径的文件,同时保持分区发现的行为,您可以使用通用数据源选项
pathGlobFilter
。
例如,以下代码从输入目录读取所有 PNG 文件:
spark.read.format("binaryFile").option("pathGlobFilter", "*.png").load("/path/to/data")
spark.read.format("binaryFile").option("pathGlobFilter", "*.png").load("/path/to/data")
spark.read().format("binaryFile").option("pathGlobFilter", "*.png").load("/path/to/data");
read.df("/path/to/data", source = "binaryFile", pathGlobFilter = "*.png")
二进制文件数据源不支持将DataFrame写回原始文件。