【问题标题】:How can i check for empty values on spark Dataframe using User defined functions如何使用用户定义的函数检查 spark Dataframe 上的空值
【发布时间】:2019-10-23 03:28:46
【问题描述】:

各位,我有这个用户定义的函数来检查文本行是否为空:

import org.apache.spark.sql.SparkSession
    val spark = SparkSession.builder().master("local").getOrCreate()
    import spark.implicits._
    {{{
      val df = Seq(
        (0, "","Mongo"),
        (1, "World","sql"),
        (2, "","")
        ).toDF("id", "text", "Source")

      // Define a "regular" Scala function
      val checkEmpty: String => Boolean =  x => {
        var test = false
        if(x.isEmpty){
          test = true
        }
        test
      }
      val upper = udf(checkEmpty)
      df.withColumn("isEmpty", upper('text)).show
    }}}

我实际上得到了这个数据框:

+---+-----+------+-------+
| id| text|Source|isEmpty|
+---+-----+------+-------+
|  0|     | Mongo|   true|
|  1|World|   sql|  false|
|  2|     |      |   true|
+---+-----+------+-------+

如何检查所有行的空值并返回如下消息:

id 0 的文本列具有空值
id 2 的 text,source 列具有空值

【问题讨论】:

  • 嗨,耶稣!如果你有数千行怎么办?还是数百万?你想打印出所有空字段的行吗?你的目标是什么?
  • 我需要获取空列名称来创建一个新的数据框,并将名称附加为元数据

标签: scala apache-spark dataframe apache-spark-sql user-defined-functions


【解决方案1】:

UDF 可以将可空列作为 Row 用于获取空列名称。然后可以过滤具有非空列的行:

val emptyColumnList = (r: Row) => r
  .toSeq
  .zipWithIndex
  .filter(_._1.toString().isEmpty)
  .map(pair => r.schema.fields(pair._2).name)

val emptyColumnListUDF = udf(emptyColumnList)

val columnsToCheck = Seq($"text", $"Source")
val result = df
  .withColumn("EmptyColumns", emptyColumnListUDF(struct(columnsToCheck: _*)))
  .where(size($"EmptyColumns") > 0)
  .select(format_string("id %s has the %s columns with empty values", $"id", $"EmptyColumns").alias("description"))

结果:

+----------------------------------------------------+
|description                                         |
+----------------------------------------------------+
|id 0 has the [text] columns with empty values       |
|id 2 has the [text,Source] columns with empty values|
+----------------------------------------------------+

【讨论】:

    【解决方案2】:

    你可以这样做:

    case class IsEmptyRow(id: Int, description: String) //case class for column names
    
    val isEmptyDf = df.map { 
       row => row.getInt(row.fieldIndex("id")) -> row //we take id of row as first column
         .toSeq //then to get secod we change row values to seq
         .zip(df.columns) //zip it with column names
         .collect { //if value is string and empty we append column name
            case (value: String, column) if value.isEmpty => column
         }
    }.map { //then we create description string and pack results to case class
       case (id, Nil)  => IsEmptyRow(id, s"id $id has no columns with empty values")
       case (id, List(column))  => IsEmptyRow(id, s"id $id has the $column column with empty values")
       case (id, columns) => IsEmptyRow(id, s"id $id has the ${columns.mkString(", ")} columns with empty values")
    }
    

    然后运行isEmptyDf.show(truncate = false)会显示:

    +---+---------------------------------------------------+
    |id |description                                        |
    +---+---------------------------------------------------+
    |0  |id 0 has the text columns with empty values        |
    |1  |id 1 has no columns with empty values              |
    |2  |id 2 has the text, Source columns with empty values|
    +---+---------------------------------------------------+
    

    你也可以加入原来的dataset:

    df.join(isEmptyDf, "id").show(truncate = false)
    

    【讨论】:

      猜你喜欢
      • 2015-11-13
      • 2015-11-28
      • 1970-01-01
      • 2018-12-10
      相关资源
      最近更新 更多