问题
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 = []
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 .
Sample 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"
}]
回答1:
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 = []
This worked , The idea is to pass the argument Records as a keyed argument .
回答2:
When passing multiple records, you need to encapsulate the records in a list of records, and then add the stream identifier.
Format is like so:
{
"Records": [
{
"Data": blob,
"ExplicitHashKey": "string",
"PartitionKey": "string"
},
{
"Data": "another record",
"ExplicitHashKey": "string",
"PartitionKey": "string"
}
],
"StreamName": "string"
}
See the Kinesis docs for more info.
来源:https://stackoverflow.com/questions/37484987/put-records-only-accepts-keyword-arguments-in-kinesis-boto3-python-api