put_records()仅接受Kinesis boto3 Python API中的关键字参数

2024-05-16 08:34:01 发布

您现在位置:Python中文网/ 问答频道 /正文

from __future__ import print_function # Python 2/3 compatibility
import boto3
import json
import decimal

#kinesis = boto3.resource('kinesis', region_name='eu-west-1')
client = boto3.client('kinesis')
with open("questions.json") as json_file:
    questions = json.load(json_file)
    Records = []
    count = 0
    for question in questions:
        value1 = question['value']
        if value1 is None:
            value1 = '0'
        record = { 'StreamName':'LoadtestKinesis', 'Data':b'question','PartitionKey':'value1' }
        Records.append(record)
        count +=1
        if count == 500:
            response = client.put_records(Records)
            Records = []

这是我的python脚本,用于将一个json文件数组加载到kinisis stream中,在这里我组合500个记录以使用put_records函数。但是我得到一个错误:put_records() only accepts keyword arguments。如何将记录列表传递给此方法?每个记录都是一个带有分区键的json。

示例Json:

[{
        "air_date": "2004-12-31",
        "answer": "FDDDe",
        "category": "AACC",
        "question": "'No. 2: 1912 Olympian; football star at Carlisle Indian School; 6 MLB seasons with the Reds, Giants & Braves'",
        "round": "DDSSS!",
        "show_number": "233",
        "value": "$200"
    }]

Tags: importclientjsonputcountwith记录boto3
2条回答
    from __future__ import print_function # Python 2/3 compatibility
    import boto3
    import json
    import decimal
    import time


    def putdatatokinesis(RecordKinesis):
        start = time.clock()
        response = client.put_records(Records=RecordKinesis, StreamName='LoadtestKinesis')
        print ("Time taken to process" +  len(Records) + " is " +time.clock() - start)
        return response
client = boto3.client('kinesis')
firehoseclient = boto3.client('firehose')
with open("questions.json") as json_file:
    questions = json.load(json_file)
    Records = []
    RecordKinesis = []
    count = 0
    for question in questions:
        value1 = question['value']
        if value1 is None:
            value1 = '0'
        recordkinesis = { 'Data':b'question','PartitionKey':value1 }
        RecordKinesis.append(recordkinesis)
        Records.append(record)
        count +=1
        if count == 500:
            putdatatokinesis(RecordKinesis)
            Records = []
            RecordKinesis = []

这样做有效,其思想是将参数记录作为键控参数传递。

传递多个记录时,需要将记录封装在记录列表中,然后添加流标识符。

格式如下:

{
   "Records": [ 
      { 
         "Data": blob,
         "ExplicitHashKey": "string",
         "PartitionKey": "string"
      },
      {
         "Data": "another record",
         "ExplicitHashKey": "string",
         "PartitionKey": "string"
      }
   ],
   "StreamName": "string"
}

有关详细信息,请参见Kinesis docs

相关问题 更多 >