答案 0 :(得分:0)
DataFrame dataFrame = sqlContext.read()
.format("com.databricks.spark.csv")
.option("inferSchema", "true")
.option("header", "true")
.load(filePath.getAbsolutePath());
DataFrame withDate = dataFrame.withColumn("date",col("dt").cast("date"));
withDate.withColumn(
"year",
year(col("date"))
).withColumn(
"month",
month(col("date"))
).withColumn(
"day",
dayofmonth(col("date"))
).show();
或
DataFrame inputFrame = sqlContext.read()
.format("com.databricks.spark.csv")
.option("inferSchema", "true")
.option("header", "true")
.load(filePath.getAbsolutePath());
DataFrame with_data_frame = inputFrame
.withColumn(
"date", col("dt").cast("date"))
.withColumn(
"year",year(col("date"))
);
with_data_frame.show();
完美)