如果预先存在的列附加了不同的数据类型,Databricks Delta LakemergeSchema
选项会做什么?
例如,给定一个具有模式foo INT, bar INT
的DeltaLake表,当指定选项mergeSchema = true
时,尝试使用模式foo INT, bar DOUBLE
写入附加新数据时会发生什么?
写入失败。(截至Databricks 6.3上的Delta Lake 0.5.0(
我想这就是您想要的。
import org.apache.spark.sql.SQLContext
import org.apache.spark.sql.types.{StructType, StructField, StringType, IntegerType};
import org.apache.spark.sql.functions.input_file_name
val customSchema = StructType(Array(
StructField("field1", StringType, true),
StructField("field2", StringType, true),
StructField("field3", StringType, true),
StructField("field4", StringType, true),
StructField("field5", StringType, true),
StructField("field6", StringType, true),
StructField("field7", StringType, true)))
val df = sqlContext.read
.format("com.databricks.spark.csv")
.option("header", "false")
.option("sep", "|")
.schema(customSchema)
.load("mnt/rawdata/corp/ABC*.gz")
.withColumn("file_name", input_file_name())
只需将"field1"、"field2"等命名为实际字段名称即可。此外,"ABC*.gz"对以特定字符串(如"ABC"或其他字符串(和"*"字符(表示任何字符组合(开头的文件进行通配符搜索,从反斜杠到".gz",表示它是一个压缩文件。当然,你的可能会有所不同,所以只要改变这种惯例就可以满足你的特定需求。