put_records() 只接受 Kinesis boto3 Python API 中的关键字参数 [英] put_records() only accepts keyword arguments in Kinesis boto3 Python API

查看:22
本文介绍了put_records() 只接受 Kinesis boto3 Python API 中的关键字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 文件加载到 kinesis 流,我在其中组合了 500 条记录以使用 put_records 函数.但我收到一个错误: put_records() 只接受关键字参数 .如何将记录的列表传递给此方法?每条记录都是一个带有分区键的 json.

This is my python script to load a array of json files to kinesis stream where I am combining 500 records to use put_records function . But I am getting an error: put_records() only accepts keyword arguments . How do I pass a list of Records to this method? Each record is a json with a partition key .

示例 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"
    }]

推荐答案

    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 作为键控参数传递.

This worked , The idea is to pass the argument Records as a keyed argument .

这篇关于put_records() 只接受 Kinesis boto3 Python API 中的关键字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆