如何在单个查询中针对不同类型的列计算流数据框架上的统计信息? [英] How to compute statistics on a streaming dataframe for different type of columns in a single query?

查看:44
本文介绍了如何在单个查询中针对不同类型的列计算流数据框架上的统计信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个流数据帧,该数据帧具有三列时间col1,col2.

I have a streaming dataframe having three columns time, col1,col2.

+-----------------------+-------------------+--------------------+
|time                   |col1               |col2                |
+-----------------------+-------------------+--------------------+
|2018-01-10 15:27:21.289|0.4988615628926717 |0.1926744113882285  |
|2018-01-10 15:27:22.289|0.5430687338123434 |0.17084552928040175 |
|2018-01-10 15:27:23.289|0.20527770821641478|0.2221980020202523  |
|2018-01-10 15:27:24.289|0.130852802747647  |0.5213147910202641  |
+-----------------------+-------------------+--------------------+

col1和col2的数据类型是可变的.它可以是字符串或数字数据类型. 因此,我必须计算每一列的统计信息. 对于字符串列,仅计算有效计数和无效计数. 对于时间戳列,仅计算分钟和分钟;最大限度. 对于数字类型列,计算最小值,最大值,平均值和均值. 我必须在一个查询中计算所有统计信息. 现在,我已经针对每种类型的列分别计算了三个查询.

The datatype of col1 and col2 is variable. It could be a string or numeric datatype. So I have to calculate statistics for each column. For string column, calculate only valid count and invalid count. For timestamp column, calculate only min & max. For numeric type column, calculate min, max, average and mean. I have to compute all statistics in a single query. Right now, I have computed with three queries separately for every type of column.

推荐答案

枚举您想要的案例并选择.例如,如果流定义为:

Enumerate cases you want and select. For example, if stream is defined as:

import org.apache.spark.sql.types._
import org.apache.spark.sql.functions._
import org.apache.spark.sql.Column

val schema = StructType(Seq(
  StructField("v", TimestampType),
  StructField("x", IntegerType),
  StructField("y", StringType),
  StructField("z", DecimalType(10, 2))
))

val df = spark.readStream.schema(schema).format("csv").load("/tmp/foo")

结果将是

val stats = df.select(df.dtypes.flatMap {
  case (c, "StringType") => 
    Seq(count(c) as s"valid_${c}", count("*") - count(c) as s"invalid_${c}")
  case (c, t) if Seq("TimestampType", "DateType") contains t => 
    Seq(min(c), max(c))
  case (c, t) if (Seq("FloatType", "DoubleType", "IntegerType") contains t) || t.startsWith("DecimalType") => 
    Seq(min(c), max(c), avg(c), stddev(c))
  case _ => Seq.empty[Column]
}: _*)

// root
//  |-- min(v): timestamp (nullable = true)
//  |-- max(v): timestamp (nullable = true)
//  |-- min(x): integer (nullable = true)
//  |-- max(x): integer (nullable = true)
//  |-- avg(x): double (nullable = true)
//  |-- stddev_samp(x): double (nullable = true)
//  |-- valid_y: long (nullable = false)
//  |-- invalid_y: long (nullable = false)
//  |-- min(z): decimal(10,2) (nullable = true)
//  |-- max(z): decimal(10,2) (nullable = true)
//  |-- avg(z): decimal(14,6) (nullable = true)
//  |-- stddev_samp(z): double (nullable = true)

这篇关于如何在单个查询中针对不同类型的列计算流数据框架上的统计信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆