在你的榜样,你的response
变量没有分配任何地方的任何值。根据您的示例response
变量保存您的流描述的响应。
我建议蟒蛇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'
退出蟒蛇REPL,
>>> exit()
是的,我现在看到。在第9行添加此行可解决此问题。所以,非常适合用其他人的例子来学习。我绝对学会了! response = kinesis_client.describe_stream(StreamName = my_stream_name) – Jason
酷,你让它工作。奇怪的是,你所关注的教程没有定义响应。 – prayagupd