want to print shardID and Sequence number after adding data to kinesis data str

  amazon-kinesis, amazon-web-services, c++

I am new to AWS and I am able to add sample data successfully to Kinesis stream but I want to print shard ID and Sequence number when every record is added to stream just like when we get when add record through cli.

When we add through CLI , we get sequence number and shardID in following format
{
"SequenceNumber": "21269319989653637946712965403778482177",
"ShardId": "shardId-000000000001"
}

Just like this I want to print both fields

Code :

int main(int argc, char** argv)
{
    const std::string USAGE = "n" 
        "Usage:n"
        "    put_get_records <streamname>nn"
        "Where:n"
        "    streamname - the table to delete the item from.nn"
        "Example:n"
        "    put_get_records sample-streamnn";

    if (argc != 2)
    {
        std::cout << USAGE;
        return 1;
    }

    Aws::SDKOptions options;
    Aws::InitAPI(options);
    {
        const Aws::String streamName(argv[1]);

        std::random_device rd;
        std::mt19937 mt_rand(rd());

        Aws::Client::ClientConfiguration clientConfig;
        // set your region
        clientConfig.region = Aws::Region::EU_WEST_1;
        Aws::Kinesis::KinesisClient kinesisClient(clientConfig);

        Aws::Kinesis::Model::PutRecordsRequest putRecordsRequest;
        putRecordsRequest.SetStreamName(streamName);
        Aws::Vector<Aws::Kinesis::Model::PutRecordsRequestEntry> putRecordsRequestEntryList;

        // create 500 records
        std::cout << "Adding records to stream "" << streamName << """ << std::endl;
      
            Aws::Kinesis::Model::PutRecordsRequestEntry putRecordsRequestEntry;
            Aws::StringStream pk;
      
                pk << "test";
            putRecordsRequestEntry.SetPartitionKey(pk.str());
            Aws::StringStream data;
            data << "Hello World !";
          
            std::cout << "Result: " << data.str() << std::endl;
            Aws::Utils::ByteBuffer bytes((unsigned char*)data.str().c_str(), data.str().length());
            putRecordsRequestEntry.SetData(bytes);
            putRecordsRequestEntryList.emplace_back(putRecordsRequestEntry);
    
        putRecordsRequest.SetRecords(putRecordsRequestEntryList);


        Aws::Kinesis::Model::PutRecordsOutcome putRecordsResult  = kinesisClient.PutRecords(putRecordsRequest);






        // if one or more records were not put, retry them
        while (putRecordsResult.GetResult().GetFailedRecordCount() > 0)
        {
            std::cout << "Some records failed, retrying" << std::endl;
            Aws::Vector<Aws::Kinesis::Model::PutRecordsRequestEntry> failedRecordsList;
            Aws::Vector<Aws::Kinesis::Model::PutRecordsResultEntry> putRecordsResultEntryList = putRecordsResult.GetResult().GetRecords();
            for (unsigned int i = 0; i < putRecordsResultEntryList.size(); i++)
            {
                Aws::Kinesis::Model::PutRecordsRequestEntry putRecordRequestEntry = putRecordsRequestEntryList[i];
                Aws::Kinesis::Model::PutRecordsResultEntry putRecordsResultEntry = putRecordsResultEntryList[i];
                if (putRecordsResultEntry.GetErrorCode().length() > 0)
                    failedRecordsList.emplace_back(putRecordRequestEntry);
            }
            putRecordsRequestEntryList = failedRecordsList;
            putRecordsRequest.SetRecords(putRecordsRequestEntryList);
            putRecordsResult = kinesisClient.PutRecords(putRecordsRequest);
        }

Source: Windows Questions C++

LEAVE A COMMENT