我是100%的Python新手,并且在关注 Python 3.6.2 文档的同时从网上抓取示例。
我有一个来自这里的脚本,我正在努力工作:
Getting Started with AWS Kinesis and Python
我的PyDev IDE说"未定义的变量:响应"以下脚本第10行:
1。)作为Python的新手,我无法理解这里使用的是什么语言结构?它看起来像一个多维数组(来自Java背景......)
响应[' StreamDescription'] ['碎片'] [0] [' ShardId']
2。)我如何解决不再使用#34;未定义"?
import boto3
import json
from datetime import datetime
import time
my_stream_name = 'python-stream'
kinesis_client = boto3.client('kinesis', region_name='us-east-1')
my_shard_id = response['StreamDescription']['Shards'][0]['ShardId']
shard_iterator = kinesis_client.get_shard_iterator(StreamName=my_stream_name,
ShardId=my_shard_id,
ShardIteratorType='LATEST')
my_shard_iterator = shard_iterator['ShardIterator']
record_response = kinesis_client.get_records(ShardIterator=my_shard_iterator,
Limit=2)
while 'NextShardIterator' in record_response:
record_response = kinesis_client.get_records(ShardIterator=record_response['NextShardIterator'],
Limit=2)
print record_response
# wait for 5 seconds
time.sleep(5)
答案 0 :(得分:1)
在您的示例中,您的response
变量未在任何位置分配任何值。根据您的示例response
变量保存您的流描述的响应。
我建议使用python REPL开始,
$ python
Python 2.7.13 (default, Apr 4 2017, 08:46:44)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto3
>>> import json
>>> boto3.setup_default_session(profile_name="aws-federated") //using credentials localted in ~/.aws/credentials
>>> client = boto3.client('kinesis', region_name='us-west-2') //I use west regiion, you can change to east as in your question
我有一个名为gregor-samsa-2
,
>>> client.list_streams(Limit=100)
{u'StreamNames': [u'gregor-samsa-1', u'gregor-samsa-2', u'HasMoreStreams': False, 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': 'ed632ba9-8bba-3173-bc93-5b04547d1f2a', 'HTTPHeaders': {'x-amzn-requestid': 'ed632ba9-8bba-3173-bc93-5b04547d1f2a', 'content-length': '244', 'x-amz-id-2': 'NJpwLfREWneUwQHIYrS+L9EmUwTUyywMLUWNzNK53C1GIKRZx8/z2TiMe9+oY3eOblNxYleMkEHPCP7D7An3Clw4EII+Tn5M', 'server': 'Apache-Coyote/1.1', 'date': 'Mon, 24 Jul 2017 22:02:20 GMT', 'content-type': 'application/x-amz-json-1.1'}}}
>>> response = client.describe_stream( StreamName='gregor-samsa-2')
如果你打印响应,它是一个hashmap /或python的术语是Dictionary
。
>>> response
{'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': 'f4130cb3-6ffb-aebe-a5e3-7e831eeea949', 'HTTPHeaders': {'x-amzn-requestid': 'f4130cb3-6ffb-aebe-a5e3-7e831eeea949', 'content-length': '841', 'x-amz-id-2': 'O0qfcRJcv9NrNlPc+DmjMPl4Fa9LKl/3D4dxzD+BQTnkisAlL3chw8P5GGZFcUSmHi+WaDBSWcNvcGuHQcfivJN2EBN7nPig', 'server': 'Apache-Coyote/1.1', 'date': 'Mon, 24 Jul 2017 22:08:47 GMT', 'content-type': 'application/x-amz-json-1.1'}}, u'StreamDescription': {u'HasMoreShards': False, u'RetentionPeriodHours': 24, u'StreamName': u'gregor-samsa-2', u'Shards': [{u'ShardId': u'shardId-000000000000', u'HashKeyRange': {u'EndingHashKey': u'170141183460469231731687303715884105727', u'StartingHashKey': u'0'}, u'SequenceNumberRange': {u'StartingSequenceNumber': u'49574403640320687182855826859993394354817752107006820354'}}, {u'ShardId': u'shardId-000000000001', u'HashKeyRange': {u'EndingHashKey': u'340282366920938463463374607431768211455', u'StartingHashKey': u'170141183460469231731687303715884105728'}, u'SequenceNumberRange': {u'StartingSequenceNumber': u'49574403640342987928054357483134930073090400468512800786'}}], u'StreamARN': u'arn:aws:kinesis:us-west-2:033814027302:stream/gregor-samsa-2', u'EnhancedMonitoring': [{u'ShardLevelMetrics': []}], u'StreamCreationTimestamp': datetime.datetime(2017, 6, 22, 0, 51, 53, tzinfo=tzlocal()), u'StreamStatus': u'ACTIVE'}}
>>> print(type(response))
<type 'dict'>
要获取要查询的分区,请像在示例中一样访问响应hashmap(使用dictionary['key1']['key2']
)
response['StreamDescription']['Shards']
给出一个数组。
>>> print(type(response['StreamDescription']['Shards']))
<type 'list'>
您可以使用list[index]
模式访问列表。
>>> response['StreamDescription']['Shards'][0]['ShardId']
u'shardId-000000000000'
退出python REPL,
>>> exit()