我正在使用Spark流媒体应用程序。应用程序使用直接流从Kafka主题(具有200个分区)读取消息。有时,应用程序会抛出ConcurrentModificationException->
java.util.ConcurrentModificationException: KafkaConsumer is not safe for multi-threaded access
at org.apache.kafka.clients.consumer.KafkaConsumer.acquire(KafkaConsumer.java:1431)
at org.apache.kafka.clients.consumer.KafkaConsumer.close(KafkaConsumer.java:1361)
at org.apache.spark.streaming.kafka010.CachedKafkaConsumer$$anon$1.removeEldestEntry(CachedKafkaConsumer.scala:128)
at java.util.LinkedHashMap.afterNodeInsertion(LinkedHashMap.java:299)
at java.util.HashMap.putVal(HashMap.java:663)
at java.util.HashMap.put(HashMap.java:611)
at org.apache.spark.streaming.kafka010.CachedKafkaConsumer$.get(CachedKafkaConsumer.scala:158)
at org.apache.spark.streaming.kafka010.KafkaRDD$KafkaRDDIterator.<init>(KafkaRDD.scala:211)
at org.apache.spark.streaming.kafka010.KafkaRDD.compute(KafkaRDD.scala:186)
at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:323)
at org.apache.spark.rdd.RDD.iterator(RDD.scala:287)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
at org.apache.spark.scheduler.Task.run(Task.scala:99)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:282)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
我的火花星团有两个节点。 Spark版本是2.1。该应用程序运行两个执行程序。从我从异常和kafka消费者代码中可以看出,似乎两个线程正在使用相同的kakfa消费者。我不知道两个线程如何访问同一个接收器。理想情况下,每个执行程序应由单个线程提供独占的kafka接收器服务,该线程必须读取所有分配的分区的消息。 从kafka-&gt;
读取的代码段JavaInputDStream<ConsumerRecord<String, String>> consumerRecords = KafkaUtils.createDirectStream(
jssc,
LocationStrategies.PreferConsistent(),
ConsumerStrategies.<String, String>Subscribe(topics, kafkaParams));
答案 0 :(得分:1)
就我而言,问题与kafka消费者缓存大小有关。我将大小(默认值:每个执行程序64个)更改为每个执行程序200个(由于200个分区,200个并行使用者)。我不得不升级到Spark 2.2,因为在Spark 2.1中没有更改大小的选项。
spark.streaming.kafka.consumer.cache.maxCapacity = 200