【发布时间】:2015-06-05 15:30:54
【问题描述】:
假设我正在做类似的事情:
val df = sqlContext.load("com.databricks.spark.csv", Map("path" -> "cars.csv", "header" -> "true"))
df.printSchema()
root
|-- year: string (nullable = true)
|-- make: string (nullable = true)
|-- model: string (nullable = true)
|-- comment: string (nullable = true)
|-- blank: string (nullable = true)
df.show()
year make model comment blank
2012 Tesla S No comment
1997 Ford E350 Go get one now th...
但我真的希望 year 像 Int 一样(并且可能转换一些其他列)。
我能想到的最好的是
df.withColumn("year2", 'year.cast("Int")).select('year2 as 'year, 'make, 'model, 'comment, 'blank)
org.apache.spark.sql.DataFrame = [year: int, make: string, model: string, comment: string, blank: string]
这有点令人费解。
我来自 R,我习惯于写作,例如
df2 <- df %>%
mutate(year = year %>% as.integer,
make = make %>% toupper)
我可能遗漏了一些东西,因为在 Spark/Scala 中应该有更好的方法来做到这一点......
【问题讨论】:
-
我喜欢这种方式 spark.sql("SELECT STRING(NULLIF(column,'')) as column_string")
标签: scala apache-spark apache-spark-sql