Scala Apache Spark,添加一个“;当。。。否则……”;将计算列添加到现有数据帧

Scala Apache Spark,添加一个“;当。。。否则……”;将计算列添加到现有数据帧,scala,apache-spark,dataframe,apache-spark-sql,Scala,Apache Spark,Dataframe,Apache Spark Sql,我试图使用Scala API将“CASE WHEN…ELSE…”计算列添加到现有数据帧中。 开始数据帧: color Red Green Blue 所需的数据帧(SQL语法:color==绿色时为CASE,然后1或0以bool结尾): 我应该如何实现这个逻辑?在即将发布的SPARK 1.4.0版本中(应该在未来几天发布)。您可以使用when/other语法: // Create the dataframe val df = Seq("Red", "Green", "Blue").map(Tup

我试图使用Scala API将“CASE WHEN…ELSE…”计算列添加到现有数据帧中。 开始数据帧:

color
Red
Green
Blue
所需的数据帧(SQL语法:color==绿色时为CASE,然后1或0以bool结尾):


我应该如何实现这个逻辑?

在即将发布的SPARK 1.4.0版本中(应该在未来几天发布)。您可以使用when/other语法:

// Create the dataframe
val df = Seq("Red", "Green", "Blue").map(Tuple1.apply).toDF("color")

// Use when/otherwise syntax
val df1 = df.withColumn("Green_Ind", when($"color" === "Green", 1).otherwise(0))
如果您使用的是SPARK 1.3.0,则可以选择使用自定义项:

// Define the UDF
val isGreen = udf((color: String) => {
  if (color == "Green") 1
  else 0
})
val df2 = df.withColumn("Green_Ind", isGreen($"color"))

在Spark 1.5.0中:还可以使用SQL语法expr函数

val df3 = df.withColumn("Green_Ind", expr("case when color = 'green' then 1 else 0 end"))
或纯火花sql

df.registerTempTable("data")
val df4 = sql(""" select *, case when color = 'green' then 1 else 0 end as Green_ind from data """)
我发现:

在spark 2.1.0上为我工作:

import sqlContext._
val rdd = sc.parallelize((1 to 100).map(i => Record(i, s"val_$i")))
rdd.registerTempTable("records")
println("Result of SELECT *:")
sql("SELECT case key when '93' then 'ravi' else key end FROM records").collect()

我已经寻找了很长一段时间,所以这里有一个SPARK 2.1 JAVA的示例,其中包含group by,供其他JAVA用户使用

import static org.apache.spark.sql.functions.*;
 //...
    Column uniqTrue = col("uniq").equalTo(true);
    Column uniqFalse = col("uniq").equalTo(false);

    Column testModeFalse = col("testMode").equalTo(false);
    Column testModeTrue = col("testMode").equalTo(true);

    Dataset<Row> x = basicEventDataset
            .groupBy(col(group_field))
            .agg(
                    sum(when((testModeTrue).and(uniqTrue), 1).otherwise(0)).as("tt"),
                    sum(when((testModeFalse).and(uniqTrue), 1).otherwise(0)).as("ft"),
                    sum(when((testModeTrue).and(uniqFalse), 1).otherwise(0)).as("tf"),
                    sum(when((testModeFalse).and(uniqFalse), 1).otherwise(0)).as("ff")
            );
import static org.apache.spark.sql.functions.*;
//...
列uniqTrue=col(“uniq”).equalTo(真);
列uniqFalse=col(“uniq”).equalTo(false);
列testModeFalse=col(“testMode”).equalTo(false);
列testModeTrue=col(“testMode”).equalTo(true);
数据集x=基本事件数据集
.groupBy(col(group_字段))
阿格先生(
总和(当((testModeTrue.)和(uniqTrue)为1时,否则(0))为(“tt”),
总和(当((testModeFalse)和(uniqTrue)为1时),否则(0)为(“ft”),
总和(当((testModeTrue.)和(uniqFalse)为1时,否则(0))为(“tf”),
总和(当((testModeFalse)和(uniqFalse)为1时),否则(0)为(“ff”)
);
使用expr()函数的可能重复非常好。非常感谢你。
import static org.apache.spark.sql.functions.*;
 //...
    Column uniqTrue = col("uniq").equalTo(true);
    Column uniqFalse = col("uniq").equalTo(false);

    Column testModeFalse = col("testMode").equalTo(false);
    Column testModeTrue = col("testMode").equalTo(true);

    Dataset<Row> x = basicEventDataset
            .groupBy(col(group_field))
            .agg(
                    sum(when((testModeTrue).and(uniqTrue), 1).otherwise(0)).as("tt"),
                    sum(when((testModeFalse).and(uniqTrue), 1).otherwise(0)).as("ft"),
                    sum(when((testModeTrue).and(uniqFalse), 1).otherwise(0)).as("tf"),
                    sum(when((testModeFalse).and(uniqFalse), 1).otherwise(0)).as("ff")
            );