【发布时间】:2018-04-13 00:40:26
【问题描述】:
val inputfile = sqlContext.read
.format("com.databricks.spark.csv")
.option("header", "true")
.option("inferSchema", "true")
.option("delimiter", "\t")
.load("data")
inputfile: org.apache.spark.sql.DataFrame = [a: string, b: bigint, c: boolean]
val outputfile = inputfile.groupBy($"a",$"b").max($"c")
上面的代码失败,因为c 是一个布尔变量,聚合不能应用于布尔值。 Spark中是否有一个函数可以将true值转换为1和false转换为0用于Spark数据框的整列。
我尝试了以下(来源:How to change column types in Spark SQL's DataFrame?)
val inputfile = sqlContext.read
.format("com.databricks.spark.csv")
.option("header", "true")
.option("inferSchema", "true")
.option("delimiter", "\t")
.load("data")
val tempfile =inputfile.select("a","b","c").withColumn("c",toInt(inputfile("c")))
val outputfile = tempfile.groupBy($"a",$"b").max($"c")
以下问题:Casting a new derived column in a DataFrame from boolean to integer PySpark 的答案,但我想要一个专门用于 Scala 的函数。
感谢任何形式的帮助。
【问题讨论】:
标签: scala spark-dataframe