【问题标题】:How to combine the two rows of a dataset into a single row in spark using java如何使用java将数据集的两行组合成spark中的一行
【发布时间】:2019-07-30 02:16:42
【问题描述】:

我正在阅读来自 json 格式的 kafka 主题的交易。然后 我应用了一些转换来获得基于 txn_status 。下面是架构。

root |-- 窗口:结构 (nullable = true) | |-- 开始:时间戳 (可为空=真)| |-- 结束:时间戳(可为空 = true)|-- txn_status: string (nullable = true) |-- count: long (nullable = 假)

对给定应用分组后,我的批处理输出如下所示 窗户。 [![在此处输入图片描述][1]][1]

但我想要如下 json 格式的输出。

{
       “start_end_time”: “28/12/2018 11:32:00.000”,
       “count_Total” : 6
       “count_RCVD” : 5,
       “count_FAILED”: 1
  }


> how to combine two rows in a spark dataset.
> 
> 
>   [1]: https://i.stack.imgur.com/sCJuX.jpg

【问题讨论】:

    标签: java apache-spark dataset


    【解决方案1】:

    根据您显示的图像,我创建了一个数据框或一个临时表,并为您的问题提供了解决方案。

    Scala 代码:

    case class txn_rec(txn_status: String, count: Int, start_end_time: String)
    
    var txDf=sc.parallelize(Array(new txn_rec("FAIL",9,"2019-03-08 016:40:00, 2019-03-08 016:57:00"), 
        new txn_rec("RCVD",161,"2019-03-08 016:40:00, 2019-03-08 016:57:00"))).toDF
    
    txDf.createOrReplaceTempView("temp")
    
    var resDF=spark.sql("select start_end_time, (select sum(count) from temp) as total_count , (select count from temp where txn_status='RCVD') as rcvd_count,(select count from temp where txn_status='FAIL') as failed_count  from temp group by start_end_time")
    
    resDF.show
    
    resDF.toJSON.collectAsList.toString
    

    您可以看到屏幕截图中显示的输出。

    【讨论】:

    • 谢谢。我是新来的火花。我想在 java 中使用 Dataset 来实现这一点,我没有在数据帧函数中找到并行化。我们可以用java完成吗?
    • 你能告诉我更多关于输入批处理数据是如何表示的吗?你想如何转换成输出。
    猜你喜欢
    • 1970-01-01
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 2021-08-17
    • 2017-10-08
    • 2014-10-12
    • 1970-01-01
    相关资源
    最近更新 更多