¿Cómo cambiar los tipos de columna en el DataFrame de Spark SQL?

Supongamos que estoy haciendo algo como:

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...  

pero realmente quería elyear comoInt (y quizás transformar algunas otras columnas).

Lo mejor que se me ocurre es

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]

lo cual es un poco complicado.

Vengo de R y estoy acostumbrado a poder escribir, p.

df2 <- df %>%
   mutate(year = year %>% as.integer, 
          make = make %>% toupper)

Es probable que me falte algo, ya que debería haber una mejor manera de hacer esto en spark / scala ...

Respuestas a la pregunta(16)

Su respuesta a la pregunta