如何将 DStream[List[T]] 转换为 DStream[T]

How to convert DStream[List[T]] to DStream[T]

我是 scala 和 spark streaming 的新手,我有一个问题让我困惑了几个小时:

目前我生成了一个list[T]的Dstream,打印出来是

List(PV(57,2,1448910200000,0xD13617EBC1032E0869C7,BI Office),
PV(57,2,1447200000000,0xD13617EBC1032E0869C7,UPS office),     
PV(100,2,1449900000000,0xD13617EBC1032E0869C7,BI Office), 
PV(57,2,1447200000000,0xD13617EBC1032E0869C7,UPS office))

有没有办法将这个列表的 Dstream 转换为看起来像

的 Dstream
PV(57,2,1448910200000,0xD13617EBC1032E0869C7,BI Office) 
PV(57,2,1447200000000,0xD13617EBC1032E0869C7,UPS office)
PV(100,2,1449900000000,0xD13617EBC1032E0869C7,BI Office)
PV(57,2,1447200000000,0xD13617EBC1032E0869C7,UPS office)

然后我可以将其转换为 pairDStream 并在 Dstream 上实现 updateStateByKey 函数。

尝试:

dstream.flatMap(list => list)

可以使用Dstream的flatMap功能。它将通过从源中的每条记录生成多个新记录来创建一个新的 DStream。

val words:Dstream[List[T]] = { ........... }
val word:Dstream[T] = words.flatMap(list => list)