【问题标题】:scala Stream processing, counting the occurences of each datescala流处理,计算每个日期的出现次数
【发布时间】:2019-01-09 18:03:38
【问题描述】:

我正在使用 scala 流处理,我有一个事件列表,我正在尝试以这种格式计算每个日期的出现次数 (dd-mm-yyyy)

这是我对案例类的定义:

case class Event(prId: Int, author: String, event: String, timestamp: Date)

我的解决方法如下

def CountOccur(events: DataStreaming[Event]): DataStreaming[(String, Int)] = {
    events.map(c => (c.timestamp, 1)).keyBy(x => x._1).timeWindow(Time.seconds(5)).sum(1)
  }

这是假设返回这样的答案:

2010-09-25,10
2010-09-27,7
.
.
.

我现在遇到的问题是timestamp 属于date format,但我需要string

如何将其更改为字符串格式的日期?

固定:

这是您解决此问题的方法:

events.map(c => (sdf.format(c.timestamp), 1)).keyBy(x => sdf.format(x._1)).window(EventTimeSessionWindows.withGap(Time.seconds(10))

但问题是现在它没有返回任何东西!!

【问题讨论】:

    标签: scala apache-flink stream-processing


    【解决方案1】:

    我不知道 flink 但我猜

    def CountOccur(events: DataStreaming[Event]): DataStreaming[(String, Int)] = {
        val sdf = new SimpleDateFormat("dd-mm-yyyy")
    
        events.map(c => (sdf.format(c.timestamp), 1)).keyBy(x => x._1).timeWindow(Time.seconds(5)).sum(1)
    }
    

    您需要删除第二种格式sdf.format(x._1),它会引发异常。

    【讨论】:

    • 我已经找到了有效的答案,但最终的答案不正确!
    • 那么请发表你的答案
    猜你喜欢
    • 1970-01-01
    • 2013-08-07
    • 2016-01-24
    • 2019-07-06
    • 2010-11-26
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 2013-08-22
    相关资源
    最近更新 更多