我试图使用to_date函数将一个String格式的列转换为Date格式,但是它返回的Null值.
df.createOrReplaceTempView("incidents")
spark.sql("select Date from incidents").show()
+----------+
| Date|
+----------+
|08/26/2016|
|08/26/2016|
|08/26/2016|
|06/14/2016|
spark.sql("select to_date(Date) from incidents").show()
+---------------------------+
|to_date(CAST(Date AS DATE))|
+---------------------------+
| null|
| null|
| null|
| null|
Run Code Online (Sandbox Code Playgroud)
Date列采用String格式:
|-- Date: string (nullable = true)
Run Code Online (Sandbox Code Playgroud) 我正在将CSV文件加载到DataFrame中,如下所示.
val conf=new SparkConf().setAppName("dataframes").setMaster("local")
val sc=new SparkContext(conf)
val spark=SparkSession.builder().getOrCreate()
import spark.implicits._
val df = spark.
read.
format("org.apache.spark.csv").
option("header", true).
csv("/home/cloudera/Book1.csv")
scala> df.printSchema()
root
|-- name: string (nullable = true)
|-- address: string (nullable = true)
|-- age: string (nullable = true)
Run Code Online (Sandbox Code Playgroud)
如何将age列更改为类型Int?