Spark整合kafka0.10.0新特性(一)(3)
kafkaParams: ju.Map[String, Object], offsets: ju.Map[TopicPartition, jl.Long]
) extends ConsumerStrategy[K, V] with Logging { /**
*继承至ConsumerStrategy的方法,获取配置参数 *
* @return */
def executorKafkaParams: ju.Map[String, Object] = kafkaParams
/** *
* 继承至ConsumerStrategy的方法,用于创建Consumer * @return */
def onStart(currentOffsets: ju.Map[TopicPartition, jl.Long]): Consumer[K, V] = { val consumer = new KafkaConsumer[K, V](kafkaParams) consumer.subscribe(topics)
val toSeek = if (currentOffsets.isEmpty) { offsets } else {
currentOffsets }
if (!toSeek.isEmpty) {
// work around KAFKA-3370 when reset is none
// poll will throw if no position, i.e. auto offset reset none and no explicit position
// but cant seek to a position before poll, because poll is what gets subscription partitions // So, poll, suppress the first exception, then seek
val aor = kafkaParams.get(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG)
val shouldSuppress = aor != null && aor.asInstanceOf[String].toUpperCase == \ try {
consumer.poll(0) } catch {
case x: NoOffsetForPartitionException if shouldSuppress =>
logWarning(\
ConsumerConfig.AUTO_OFFSET_RESET_CONFIG + \is none. See KAFKA-3370\ }
toSeek.asScala.foreach { case (topicPartition, offset) => consumer.seek(topicPartition, offset) }
// we've called poll, we must pause or next poll may consume messages and set position consumer.pause(consumer.assignment()) }
consumer } } /**
* Subscribe to all topics matching specified pattern to get dynamically assigned partitions. * The pattern matching will be done periodically against topics existing at the time of check. * @param pattern pattern to subscribe to
* @param kafkaParams Kafka
*
* configuration parameters to be used on driver. The same params will be used on executors,
* with minor automatic modifications applied. * Requires \
* with Kafka broker(s) specified in host1:port1,host2:port2 form.
* @param offsets: offsets to begin at on initial startup. If no offset is given for a * TopicPartition, the committed offset (if applicable) or kafka param * auto.offset.reset will be used. *
* SubscribePattern是订阅所有匹配正则表达式模式的topic的数据
* topic模式匹配将会周期性的进行匹配,而不是在检查已存在的topic时候进行模式匹配 *
* kafkaParams参数:
*配置参数将会在driver上使用,并且相同参数也会发送到executor上使用,在executor上可能略微的配置修改
*通知需要设置bootstrap.servers指定broker *
*offsets参数:
* offsets伴随着startup开始,如果对于TopicPartition没有指定offset, * 提交的offset或auto.offset.reset将会被使用 * * */
private case class SubscribePattern[K, V](
pattern: ju.regex.Pattern,
kafkaParams: ju.Map[String, Object], offsets: ju.Map[TopicPartition, jl.Long] ) extends ConsumerStrategy[K, V] with Logging {
def executorKafkaParams: ju.Map[String, Object] = kafkaParams
def onStart(currentOffsets: ju.Map[TopicPartition, jl.Long]): Consumer[K, V] = { val consumer = new KafkaConsumer[K, V](kafkaParams)
consumer.subscribe(pattern, new NoOpConsumerRebalanceListener()) val toSeek = if (currentOffsets.isEmpty) { offsets } else {
currentOffsets }
if (!toSeek.isEmpty) {
// work around KAFKA-3370 when reset is none, see explanation in Subscribe above val aor = kafkaParams.get(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG)
val shouldSuppress = aor != null && aor.asInstanceOf[String].toUpperCase == \ try {
consumer.poll(0) } catch {
case x: NoOffsetForPartitionException if shouldSuppress =>
logWarning(\
ConsumerConfig.AUTO_OFFSET_RESET_CONFIG + \is none. See KAFKA-3370\ }
toSeek.asScala.foreach { case (topicPartition, offset) => consumer.seek(topicPartition, offset) }
// we've called poll, we must pause or next poll may consume messages and set position consumer.pause(consumer.assignment()) }
consumer } } /**
* Assign a fixed collection of TopicPartitions
* @param topicPartitions collection of TopicPartitions to assign * @param kafkaParams Kafka
* configuration parameters to be used on driver. The same params will be used on executors, * with minor autowww.sm136.commatic modifications applied. * Requires \
* with Kafka broker(s) specified in host1:port1,host2:port2 form.
* @param offsets: offsets to begin at on initial startup. If no offset is given for a * TopicPartition, the committed offset (if applicable) or kafka param * auto.offset.reset will be used. *
* 订阅一个固定数目的TopicPartitions * kafkaParams参数:
*配置参数将会在driver上使用,并且相同参数也会发送到executor上使用,在executor上可能略微的配置修改
*通知需要设置bootstrap.servers指定broker *
*offsets参数:
* offsets伴随着startup开始,如果对于TopicPartition没有指定offset, * 提交的offset或auto.offset.reset将会被使用 …… 此处隐藏:3355字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [高等教育]公司协助某村精准扶贫工作总结.doc
- [高等教育]高二生物知识点总结(全)
- [高等教育]苏教版数学三年级下册《解决问题的策略
- [高等教育]仪器分析课程学习心得
- [高等教育]2017年五邑大学数学与计算科学学院333
- [高等教育]人教版七年级下册语文第四单元测试题(
- [高等教育]2018年秋七年级英语上册Unit7Howmuchar
- [高等教育]2017年八年级下数学教学工作小结
- [高等教育]湖南省怀化市2019届高三统一模拟考试(
- [高等教育]四年级下册科学_基础训练及答案教材
- [高等教育]城郊煤矿西风井管路伸缩器更换施工安全
- [高等教育]昆八中20182019学年度上学期期末考试
- [高等教育]项目部各类人员任命书
- [高等教育]上市公司经营水务产业的模式
- [高等教育]人教版高二化学第一学期第三章水溶液中
- [高等教育]【中考物理第一轮复习资料】四.压强与
- [高等教育]金坑水电站报废改建工程机电设备更新改
- [高等教育]高中生物教学工作计划简易版
- [高等教育]2017年西华大学攀枝花学院(联合办学)44
- [高等教育]最新整理超短爆笑英文小笑话大全
- 优秀教师继续教育学习心得体会
- 阳历到阴历的转换
- 留守儿童教育案例分析
- 华师17春秋学期《玩教具制作与环境布置
- 测速传感器新型安装装置的现场应用
- 人教版小学数学三年级下册第四单元
- 创业个人意向书
- 山东省潍坊市2012年高考仿真试题(三)
- [恒心][好卷速递]四川省成都外国语学校
- 多少人错把好转反应当成了病情加重处理
- 中外广播电视史复习资料整理
- 江苏省扬州市江都区宜陵镇中学2014-201
- 工程造价专业毕业实习报告
- 广西师范学院心理与教育统计
- aympkrq基于 - asp的博客网站设计与开
- 建筑业外出经营相关流程操作(营改增后
- 人治 德治 法治
- [精华篇]常识判断专项训练题库
- 中国共产党为什么要实行民主集中
- 小学数学第三册第一单元试卷(A、B、C




