创建带有来自 Spark 的分区的外部表
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.SaveMode
val spark = SparkSession.builder().master("local[*]").appName("splitInput").enableHiveSupport().getOrCreate()
val sparkDf = spark.read.option("header","true").option("inferSchema","true").csv("input/candidate/event=ABCD/CandidateScheduleData_3007_2018.csv")
var newDf = sparkDf
for(col <- sparkDf.columns){ newDf = newDf.withColumnRenamed(col,col.replaceAll("\s", "_")) }
newDf.write.mode(SaveMode.Overwrite).option("path","/output/candidate/event=ABCD/").partitionBy("CenterCode","ExamDate").saveAsTable("abc.candidatelist")
一切正常,除了分区列ExamDate
创建格式
ExamDate=30%2F07%2F2018 instead of ExamDate=30-07-2018
如何将%2F
替换为考试日期格式的-
。
%2F
是百分比编码/
。这意味着数据完全采用30/07/2018
格式。您可以:
- 使用指定的格式
to_date
解析它。 - 使用所需格式手动设置列的格式。