从Kinesis获取记录时在ShardIterator上接收TypeError

时间:2014-06-13 14:44:29

标签: amazon-kinesis

我有以下Python块来获取我放在Kinesis流上的记录,然后将记录放入S3存储桶。此流有一个分片。

# Fetching the shard iterators from the Kinesis stream
shard_iterators = []
if response and 'StreamDescription' in response:
    for shard_id in response['StreamDescription']['Shards']:
        shard_id = shard_id['ShardId']
        shard_iterator = kinesis_connection.get_shard_iterator(stream_name, shard_id, 'LATEST')
        shard_iterators.append(shard_iterator['ShardIterator'])

# Iterating over the Kinesis stream and pushing data to S3
bucket = s3_connection.get_bucket(bucket_name)
k = Key(bucket)

for shard_iterator in shard_iterators:
    while 1:
        response = kinesis_connection.get_records(shard_iterator)
        shard_iterator = response['NextShardIterator']
        if len(response['Records'])> 0:
            for res in response['Records']:
                k.key = datetime.datetime.now().strftime('%Y/%m/%d/') + res['SequenceNumber']
                k.set_contents_from_string(res['Data'])

数据最初从流中拉出并推送到S3,但在某些时候,我会在TypeError: expected string or buffer行上收到response = kinesis_connection.get_records(shard_iterator)。碎片上有更多记录应该可供我使用。有没有人之前遇到过这个问题和/或有没有人对我应该尝试修复它的想法有什么想法?

1 个答案:

答案 0 :(得分:0)

json解码非字符串可能会引发该错误。例如None或int。

import json
json.loads(None)
...
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
   obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer

这可能发生在这里:https://github.com/boto/boto/blob/develop/boto/kinesis/layer1.py#L705

这意味着回复的回复正是None

可能是由于你的shard_iterator到期了。碎片迭代器只持续5分钟,因此如果您花费很长时间来处理当前批记录,或者以其他方式停止,那么可能就是这样。 见:http://docs.aws.amazon.com/kinesis/latest/APIReference/API_GetShardIterator.html

另一种可能性是它是一个间歇性的auth /连接错误,我之前见过它,并且可能产生虚假的异常,这可能导致相同的boto代码路径?

虽然如果你能提供更多的堆栈跟踪和你看到的任何日志消息,那将会有所帮助。