Search in sources :

Example 1 with PutRecordsResultEntry

use of com.amazonaws.services.kinesis.model.PutRecordsResultEntry in project beam by apache.

the class KinesisUploader method uploadAll.

public static void uploadAll(List<String> data, KinesisTestOptions options) {
    AmazonKinesisClient client = new AmazonKinesisClient(new StaticCredentialsProvider(new BasicAWSCredentials(options.getAwsAccessKey(), options.getAwsSecretKey()))).withRegion(Regions.fromName(options.getAwsKinesisRegion()));
    List<List<String>> partitions = Lists.partition(data, MAX_NUMBER_OF_RECORDS_IN_BATCH);
    for (List<String> partition : partitions) {
        List<PutRecordsRequestEntry> allRecords = newArrayList();
        for (String row : partition) {
            allRecords.add(new PutRecordsRequestEntry().withData(ByteBuffer.wrap(row.getBytes(Charsets.UTF_8))).withPartitionKey(Integer.toString(row.hashCode())));
        }
        PutRecordsResult result;
        do {
            result = client.putRecords(new PutRecordsRequest().withStreamName(options.getAwsKinesisStream()).withRecords(allRecords));
            List<PutRecordsRequestEntry> failedRecords = newArrayList();
            int i = 0;
            for (PutRecordsResultEntry row : result.getRecords()) {
                if (row.getErrorCode() != null) {
                    failedRecords.add(allRecords.get(i));
                }
                ++i;
            }
            allRecords = failedRecords;
        } while (result.getFailedRecordCount() > 0);
    }
}
Also used : AmazonKinesisClient(com.amazonaws.services.kinesis.AmazonKinesisClient) PutRecordsRequestEntry(com.amazonaws.services.kinesis.model.PutRecordsRequestEntry) StaticCredentialsProvider(com.amazonaws.internal.StaticCredentialsProvider) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) PutRecordsResult(com.amazonaws.services.kinesis.model.PutRecordsResult) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) PutRecordsResultEntry(com.amazonaws.services.kinesis.model.PutRecordsResultEntry) PutRecordsRequest(com.amazonaws.services.kinesis.model.PutRecordsRequest)

Aggregations

BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 StaticCredentialsProvider (com.amazonaws.internal.StaticCredentialsProvider)1 AmazonKinesisClient (com.amazonaws.services.kinesis.AmazonKinesisClient)1 PutRecordsRequest (com.amazonaws.services.kinesis.model.PutRecordsRequest)1 PutRecordsRequestEntry (com.amazonaws.services.kinesis.model.PutRecordsRequestEntry)1 PutRecordsResult (com.amazonaws.services.kinesis.model.PutRecordsResult)1 PutRecordsResultEntry (com.amazonaws.services.kinesis.model.PutRecordsResultEntry)1 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 List (java.util.List)1