Spark Streaming 'numRecords must not be negative'问题解决
转载自:http://blog.csdn.net/xueba207/article/details/51135423
问题描述
笔者使用spark streaming读取Kakfa中的数据,做进一步处理,用到了KafkaUtil的createDirectStream()方法;该方法不会自动保存topic partition的offset到zk,需要在代码中编写提交逻辑,此处介绍了保存offset的方法。
当删除已经使用过的kafka topic,然后新建同名topic,使用该方式时出现了"numRecords
异常
must not be negative"
详细信息如下图:
是不合法的参数异常,RDD的记录数目必须不能是负数。
下文详细分析该问题的出现的场景,以及解决方法。
异常分析
numRecords确定
首先,定位出异常出现的问题,和大致原因。异常中打印出了出现的位置 org.apache.spark.streaming.scheduler.StreamInputInfo.InputInfoTracker
的第38行,此处代码:
代码38行,判断了numRecords是否大于等于0,当不满足条件时抛出异常,可判断此时numRecords<0。
numRecords的解释: numRecords: the number of records in a batch
应该是当前rdd中records 数目计算出了问题。
numRecords 构造StreamInputInfo时的参数,结合异常中的信息,找到了DirectKafkaInputDStream中的构造InputInfo的位置:
可知 numRecords是rdd.count()的值。
rdd.count的计算
根据以上分析可知rdd.count()值为负值,因此需要分析rdd的是如何生成的。
同样在DirectKafkaInputDStream中找到rdd的生成代码:
从此处一路跟踪代码,可在KafkaRDD.scala中找到rdd.count的赋值逻辑:
offsetRanges的计算逻辑
offsetRanges的定义
offsetRanges: offset ranges that define the Kafka data
belonging to this RDD
在KafkaRDDPartition 40行找到kafka partition offsetRange的计算逻辑:
def count(): Long = untilOffset - fromOffset
fromOffset: per-topic/partition Kafka offset defining
the (inclusive) starting point of the batchuntilOffset: per-topic/partition Kafka offset defining
the (inclusive) ending point of the batch
fromOffset来自zk中保存;
untilOffset通过DirectKafkaInputDStream第145行:
val untilOffsets = clamp(latestLeaderOffsets(maxRetries))
计算得到,计算过程得到最新的offset,然后使用spark.streaming.kafka.maxRatePerPartition
做clamp,得到允许的最大untilOffsets,##而此时新建的topic,如果topic中没有数据,untilOffsets应该为0##
原因总结
当删除一个topic时,zk中的offset信息并没有被清除,因此KafkaDirectStreaming再次启动时仍会得到旧的topic offset为old_offset,作为fromOffset。
当新建了topic后,使用untiloffset计算逻辑,得到untilOffset为0(如果topic已有数据则>0);
再次被启动的KafkaDirectStreaming Job通过异常的计算逻辑得到的rdd numRecords值为可计算为:
numRecords = untilOffset - fromOffset(old_offset)
当untilOffset < old_offset时,此异常会出现,对于新建的topic这种情况的可能性很大
解决方法
思路
根据以上分析,可在确定KafkaDirectStreaming 的fromOffsets时判断fromOffset与untiloffset的大小关系,当untilOffset < fromOffset时,矫正fromOffset为offset初始值0。
流程
- 从zk获取topic/partition 的fromOffset(获取方法链接)
- 利用SimpleConsumer获取每个partiton的lastOffset(untilOffset )
- 判断每个partition lastOffset与fromOffset的关系
- 当lastOffset < fromOffset时,将fromOffset赋值为0
通过以上步骤完成fromOffset的值矫正。
核心代码
获取kafka topic partition lastoffset代码:
- package org.frey.example.utils.kafka;
- import com.google.common.collect.Lists;
- import com.google.common.collect.Maps;
- import kafka.api.PartitionOffsetRequestInfo;
- import kafka.cluster.Broker;
- import kafka.common.TopicAndPartition;
- import kafka.javaapi.*;
- import kafka.javaapi.consumer.SimpleConsumer;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * KafkaOffsetTool
- *
- * @author v1-daddy
- * @date 2016/4/11
- */
- public class KafkaOffsetTool {
- private static KafkaOffsetTool instance;
- final int TIMEOUT = 100000;
- final int BUFFERSIZE = 64 * 1024;
- private KafkaOffsetTool() {
- }
- public static synchronized KafkaOffsetTool getInstance() {
- if (instance == null) {
- instance = new KafkaOffsetTool();
- }
- return instance;
- }
- public Map<TopicAndPartition, Long> getLastOffset(String brokerList, List<String> topics,
- String groupId) {
- Map<TopicAndPartition, Long> topicAndPartitionLongMap = Maps.newHashMap();
- Map<TopicAndPartition, Broker> topicAndPartitionBrokerMap =
- KafkaOffsetTool.getInstance().findLeader(brokerList, topics);
- for (Map.Entry<TopicAndPartition, Broker> topicAndPartitionBrokerEntry : topicAndPartitionBrokerMap
- .entrySet()) {
- // get leader broker
- Broker leaderBroker = topicAndPartitionBrokerEntry.getValue();
- SimpleConsumer simpleConsumer = new SimpleConsumer(leaderBroker.host(), leaderBroker.port(),
- TIMEOUT, BUFFERSIZE, groupId);
- long readOffset = getTopicAndPartitionLastOffset(simpleConsumer,
- topicAndPartitionBrokerEntry.getKey(), groupId);
- topicAndPartitionLongMap.put(topicAndPartitionBrokerEntry.getKey(), readOffset);
- }
- return topicAndPartitionLongMap;
- }
- /**
- * 得到所有的 TopicAndPartition
- *
- * @param brokerList
- * @param topics
- * @return topicAndPartitions
- */
- private Map<TopicAndPartition, Broker> findLeader(String brokerList, List<String> topics) {
- // get broker's url array
- String[] brokerUrlArray = getBorkerUrlFromBrokerList(brokerList);
- // get broker's port map
- Map<String, Integer> brokerPortMap = getPortFromBrokerList(brokerList);
- // create array list of TopicAndPartition
- Map<TopicAndPartition, Broker> topicAndPartitionBrokerMap = Maps.newHashMap();
- for (String broker : brokerUrlArray) {
- SimpleConsumer consumer = null;
- try {
- // new instance of simple Consumer
- consumer = new SimpleConsumer(broker, brokerPortMap.get(broker), TIMEOUT, BUFFERSIZE,
- "leaderLookup" + new Date().getTime());
- TopicMetadataRequest req = new TopicMetadataRequest(topics);
- TopicMetadataResponse resp = consumer.send(req);
- List<TopicMetadata> metaData = resp.topicsMetadata();
- for (TopicMetadata item : metaData) {
- for (PartitionMetadata part : item.partitionsMetadata()) {
- TopicAndPartition topicAndPartition =
- new TopicAndPartition(item.topic(), part.partitionId());
- topicAndPartitionBrokerMap.put(topicAndPartition, part.leader());
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (consumer != null)
- consumer.close();
- }
- }
- return topicAndPartitionBrokerMap;
- }
- /**
- * get last offset
- * @param consumer
- * @param topicAndPartition
- * @param clientName
- * @return
- */
- private long getTopicAndPartitionLastOffset(SimpleConsumer consumer,
- TopicAndPartition topicAndPartition, String clientName) {
- Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo =
- new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
- requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(
- kafka.api.OffsetRequest.LatestTime(), 1));
- OffsetRequest request = new OffsetRequest(
- requestInfo, kafka.api.OffsetRequest.CurrentVersion(),
- clientName);
- OffsetResponse response = consumer.getOffsetsBefore(request);
- if (response.hasError()) {
- System.out
- .println("Error fetching data Offset Data the Broker. Reason: "
- + response.errorCode(topicAndPartition.topic(), topicAndPartition.partition()));
- return 0;
- }
- long[] offsets = response.offsets(topicAndPartition.topic(), topicAndPartition.partition());
- return offsets[0];
- }
- /**
- * 得到所有的broker url
- *
- * @param brokerlist
- * @return
- */
- private String[] getBorkerUrlFromBrokerList(String brokerlist) {
- String[] brokers = brokerlist.split(",");
- for (int i = 0; i < brokers.length; i++) {
- brokers[i] = brokers[i].split(":")[0];
- }
- return brokers;
- }
- /**
- * 得到broker url 与 其port 的映射关系
- *
- * @param brokerlist
- * @return
- */
- private Map<String, Integer> getPortFromBrokerList(String brokerlist) {
- Map<String, Integer> map = new HashMap<String, Integer>();
- String[] brokers = brokerlist.split(",");
- for (String item : brokers) {
- String[] itemArr = item.split(":");
- if (itemArr.length > 1) {
- map.put(itemArr[0], Integer.parseInt(itemArr[1]));
- }
- }
- return map;
- }
- public static void main(String[] args) {
- List<String> topics = Lists.newArrayList();
- topics.add("ys");
- topics.add("bugfix");
- Map<TopicAndPartition, Long> topicAndPartitionLongMap =
- KafkaOffsetTool.getInstance().getLastOffset("broker001:9092,broker002:9092", topics, "my.group.id");
- for (Map.Entry<TopicAndPartition, Long> entry : topicAndPartitionLongMap.entrySet()) {
- System.out.println(entry.getKey().topic() + "-"+ entry.getKey().partition() + ":" + entry.getValue());
- }
- }
- }
矫正offset核心代码:
- /** 以下 矫正 offset */
- // 得到Topic/partition 的lastOffsets
- Map<TopicAndPartition, Long> topicAndPartitionLongMap =
- KafkaOffsetTool.getInstance().getLastOffset(kafkaParams.get("metadata.broker.list"),
- topicList, "my.group.id");
- // 遍历每个Topic.partition
- for (Map.Entry<TopicAndPartition, Long> topicAndPartitionLongEntry : fromOffsets.entrySet()) {
- // fromOffset > lastOffset时
- if (topicAndPartitionLongEntry.getValue() >
- topicAndPartitionLongMap.get(topicAndPartitionLongEntry.getKey())) {
- //矫正fromoffset为offset初始值0
- topicAndPartitionLongEntry.setValue(0L);
- }
- }
- /** 以上 矫正 offset */
Spark Streaming 'numRecords must not be negative'问题解决的更多相关文章
- Spark Streaming Backpressure分析
1.为什么引入Backpressure 默认情况下,Spark Streaming通过Receiver以生产者生产数据的速率接收数据,计算过程中会出现batch processing time > ...
- spark streaming之三 rdd,job的动态生成以及动态调度
前面一篇讲到了,DAG静态模板的生成.那么spark streaming会在每一个batch时间一到,就会根据DAG所形成的逻辑以及物理依赖链(dependencies)动态生成RDD以及由这些RDD ...
- Spark Streaming性能优化: 如何在生产环境下应对流数据峰值巨变
1.为什么引入Backpressure 默认情况下,Spark Streaming通过Receiver以生产者生产数据的速率接收数据,计算过程中会出现batch processing time > ...
- Spark Streaming性能优化系列-怎样获得和持续使用足够的集群计算资源?
一:数据峰值的巨大影响 1. 数据确实不稳定,比如晚上的时候訪问流量特别大 2. 在处理的时候比如GC的时候耽误时间会产生delay延迟 二:Backpressure:数据的反压机制 基本思想:依据上 ...
- Spark Streaming应用启动过程分析
本文为SparkStreaming源码剖析的第三篇,主要分析SparkStreaming启动过程. 在调用StreamingContext.start方法后,进入JobScheduler.start方 ...
- spark streaming限制吞吐
使用spark.streaming.receiver.maxRate这个属性限制每秒的最大吞吐.官方文档如下: Maximum rate (number of records per second) ...
- Spark Streaming ReceiverTracker架构设计
本节的主要内容: 一.ReceiverTracker的架构设计 二.消息循环系统 三.ReceiverTracker具体实现 Spark Streaming作为Spark Core基础 架构之上的一个 ...
- Spark Streaming 总结
这篇文章记录我使用 Spark Streaming 进行 ETL 处理的总结,主要包含如何编程,以及遇到的问题. 环境 我在公司使用的环境如下: Spark: 2.2.0 Kakfa: 0.10.1 ...
- spark streaming 接收kafka消息之四 -- 运行在 worker 上的 receiver
使用分布式receiver来获取数据使用 WAL 来实现 exactly-once 操作: conf.set("spark.streaming.receiver.writeAheadLog. ...
随机推荐
- 最接近的三数之和(java实现)
题目: 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如 ...
- Openstack中用秘钥对(keypair)生成和访问虚机的方法
Openstack中用镜像文件生成的image来创建虚机(VM或Instance)时, 通常不支持用户名加密码的ssh方式登录访问该VM,而是用秘钥对(keypair)方式. 这里以Centos的镜像 ...
- ranch 源码分析(三)
接上ranch 源码分析(二) 上次讲到了ranch_conns_sup和ranch_acceptors_sup这2个ranch的核心模块,我们接着分析 首先查看ranch_conns_sup.erl ...
- 了解多层交换中的CEF FIB CAM TCAM
来源:http://blog.51cto.com/redant/314151 多层交换是指交换机使用硬件来交换和路由数据包,通过硬件来支持4-7层的交换.交换机执行硬件交换,第3层引擎(路由处理器)须 ...
- 强化git
[场景1]git 提交本地代码到远程master 1.git init 2.git add . 3.git commit -m " " 4.git remote add origi ...
- sparse_tensor feed_dict的时候十分不方便。
假如说,你再处理文本的时候,写tfrecord的时候用的变长的类型, example = tf.train.Example(features=tf.train.Features(feature={ ' ...
- nodeJS总结day1
1.概念:nodeJS是基于chromeV8引擎的Javascript运行环境,使用了一个事件驱动非阻塞的i/o模式,使其轻量又高效.适合做高效又并发的项目.除此之外遵循的是COMMONJS规范. 2 ...
- java基础---->Serializable的使用
本次讲解中我们建立一个Java的项目去体会一下序列化Serializable的使用,序列化的原理以及序列化的自定义请参见我的另外一篇博客(java高级---->Serializable序列化的源 ...
- angualrjs 文本框去掉表情
html: <textarea ng-module="dataText"></textarea> js: <script> var BQ_RAN ...
- 数位dp讲解及模板
转载自:传送门 数位DP其实是很灵活的,所以一定不要奢求一篇文章就会遍所有数位DP的题,这一篇只能是讲清楚一种情况,其他情况遇到再总结,在不断总结中慢慢体会这个思想,以后说不定就能达到一看到题目就能灵 ...