Apache spark spark,插入uniq kafka请求中的30天数据

Apache spark spark,插入uniq kafka请求中的30天数据,apache-spark,cassandra,spark-streaming,Apache Spark,Cassandra,Spark Streaming,我有一个来自卡夫卡的活动,内容如下: (date, user_id, app_id, duration, session_id, ....) 我使用以下代码获取主题: val kafkaStream = KafkaUtils.createStream[String, String, StringDecoder, StringDecoder]( ssc, kafkaParams, Map(topicSessionDuration -> 2), StorageLevel.ME

我有一个来自卡夫卡的活动,内容如下:

(date,
user_id,
app_id,
duration,
session_id,
....)
我使用以下代码获取主题:

val kafkaStream = KafkaUtils.createStream[String, String, StringDecoder, StringDecoder](
        ssc, kafkaParams, Map(topicSessionDuration -> 2), StorageLevel.MEMORY_AND_DISK_2)
        .map(_._2)
        .map(RawSessionData(_))
我在卡桑德拉储存了:

   kafkaStream.map(session_duration => (
        session_duration.year,
        session_duration.month,
        session_duration.day,
        session_duration.publisher_id,
        session_duration.app_id,
        session_duration.user_id
        )).saveToCassandra(configServer.getString("cassandra.keyspace"), configServer.getString("cassandra.table.daily.user_by_app"))
从这个事件中,我存储在15个表中,用于不同的用途。会话数、用户数、持续时间

我需要存储在另一个,但从这个事件中,我需要存储30个不同的行(日期+0天到日期+30天)

我试着这样做:

    for (a <- 0 to 30) {
       val toto = a
        kafkaStream.map(x => {
            val date = new DateTime(x.date_create).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0).plusDays(toto)               
            (
                date.getYear,
                date.getMonthOfYear,
                date.getDayOfMonth,
                x.user_id
                )
        }).saveToCassandra(configServer.getString("cassandra.keyspace"), configServer.getString("cassandra.table.daily.user_30d"))
我可能做错了什么?
你能帮我吗?:)

最好将该转换表示为:

kafkaStream.flatMap{x => 
    (0 to 30).map{day => 
        val date = new DateTime(x.date_create).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0).plusDays(day)               
        (
            date.getYear,
            date.getMonthOfYear,
            date.getDayOfMonth,
            x.user_id
        )
    }}.saveToCassandra(configServer.getString("cassandra.keyspace"),   configServer.getString("cassandra.table.daily.user_30d"))

最好将这种转变表示为:

kafkaStream.flatMap{x => 
    (0 to 30).map{day => 
        val date = new DateTime(x.date_create).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0).plusDays(day)               
        (
            date.getYear,
            date.getMonthOfYear,
            date.getDayOfMonth,
            x.user_id
        )
    }}.saveToCassandra(configServer.getString("cassandra.keyspace"),   configServer.getString("cassandra.table.daily.user_30d"))

该错误可能表示过载。您的处理延迟情况如何?您是否正在监视Spark UI的流媒体选项卡?我的延迟在第一个记录(3秒)上很差,但随后变好,平均为409ms,平均为232.00条记录/秒,不是吗?我有3台服务器(1台有8个进程32g,1台有8个进程64g,1台有12个进程64b),当我使用内存和磁盘时,我停止运行,出现错误:内存和磁盘服务器2,而不是内存和磁盘服务器2。但是您使用flatmap的答案减少了延迟,所以它是好的:)区别在于外部循环在内部移动循环时增加了30个额外的(转换->存储)任务,它变为1。该错误可能表示过载。您的处理延迟情况如何?您是否正在监视Spark UI的流媒体选项卡?我的延迟在第一个记录(3秒)上很差,但随后变好,平均为409ms,平均为232.00条记录/秒,不是吗?我有3台服务器(1台有8个进程32g,1台有8个进程64g,1台有12个进程64b),当我使用内存和磁盘时,我停止运行,出现错误:内存和磁盘服务器2,而不是内存和磁盘服务器2。但是您使用平面图的答案减少了延迟,因此效果很好:)不同之处在于外部循环增加了30个额外的(转换->存储)任务,而将循环移到内部时,它变为1。