有状态流式处理 Spark 处理



我正在学习Spark并尝试构建一个简单的流媒体服务。

例如,我有一个 Kafka 队列和一个像字数一样的 Spark 作业。该示例使用无状态模式。我想累积字数,因此如果test在不同的消息中发送了几次,我可以得到其所有出现的总数。

使用其他示例,如StatefulNetworkWordCount,我尝试修改我的Kafka流媒体服务

val sc = new SparkContext(sparkConf)
val ssc = new StreamingContext(sc, Seconds(2))
ssc.checkpoint("/tmp/data")
// Create direct kafka stream with brokers and topics
val topicsSet = topics.split(",").toSet
val kafkaParams = Map[String, String]("metadata.broker.list" -> brokers)
val messages = KafkaUtils.createDirectStream[String, String, StringDecoder, StringDecoder](ssc, kafkaParams, topicsSet)
// Get the lines, split them into words, count the words and print
val lines = messages.map(_._2)
val words = lines.flatMap(_.split(" "))
val wordDstream = words.map(x => (x, 1))
// Update the cumulative count using mapWithState
// This will give a DStream made of state (which is the cumulative count of the words)
val mappingFunc = (word: String, one: Option[Int], state: State[Int]) => {
  val sum = one.getOrElse(0) + state.getOption.getOrElse(0)
  val output = (word, sum)
  state.update(sum)
  output
}
val stateDstream = wordDstream.mapWithState(
  StateSpec.function(mappingFunc) /*.initialState(initialRDD)*/)
stateDstream.print()
stateDstream.map(s => (s._1, s._2.toString)).foreachRDD(rdd => sc.toRedisZSET(rdd, "word_count", 0))
// Start the computation
ssc.start()
ssc.awaitTermination()

我收到很多错误,例如

17/03/26 21:33:57 ERROR streaming.StreamingContext: Error starting the context, marking it as stopped
java.io.NotSerializableException: DStream checkpointing has been enabled but the DStreams with their functions are not serializable
org.apache.spark.SparkContext
Serialization stack:
    - object not serializable (class: org.apache.spark.SparkContext, value: org.apache.spark.SparkContext@2b680207)
    - field (class: com.DirectKafkaWordCount$$anonfun$main$2, name: sc$1, type: class org.apache.spark.SparkContext)
    - object (class com.DirectKafkaWordCount$$anonfun$main$2, <function1>)
    - field (class: org.apache.spark.streaming.dstream.DStream$$anonfun$foreachRDD$1$$anonfun$apply$mcV$sp$3, name: cleanedF$1, type: interface scala.Function1)

虽然无状态版本工作正常,没有错误

val sc = new SparkContext(sparkConf)
val ssc = new StreamingContext(sc, Seconds(2))
// Create direct kafka stream with brokers and topics
val topicsSet = topics.split(",").toSet
val kafkaParams = Map[String, String]("metadata.broker.list" -> brokers)
val messages = KafkaUtils.createDirectStream[String, String, StringDecoder, StringDecoder](
  ssc, kafkaParams, topicsSet)
// Get the lines, split them into words, count the words and print
val lines = messages.map(_._2)
val words = lines.flatMap(_.split(" "))
val wordCounts = words.map(x => (x, 1L)).reduceByKey(_ + _).map(s => (s._1, s._2.toString))
wordCounts.print()
wordCounts.foreachRDD(rdd => sc.toRedisZSET(rdd, "word_count", 0))
// Start the computation
ssc.start()
ssc.awaitTermination()

问题是如何使流式处理有状态字数计数。

在这一行:

ssc.checkpoint("/tmp/data")

您已启用检查点,这意味着您的以下所有内容:

wordCounts.foreachRDD(rdd => sc.toRedisZSET(rdd, "word_count", 0))

必须是可序列化的,而sc本身不是,正如您从错误消息中看到的那样:

object not serializable (class: org.apache.spark.SparkContext, value: org.apache.spark.SparkContext@2b680207)

删除检查点代码行将有助于解决这个问题。

另一种方法是连续compute DStream RDD或直接将数据写入 redis,例如:

wordCounts.foreachRDD{rdd => 
  rdd.foreachPartition(partition => RedisContext.setZset("word_count", partition, ttl, redisConfig)
}

RedisContext是一个不依赖于SparkContext的可序列化对象。

另请参阅:https://github.com/RedisLabs/spark-redis/blob/master/src/main/scala/com/redislabs/provider/redis/redisFunctions.scala

最新更新