Example usage for com.amazonaws.services.dynamodbv2.model PutRequest PutRequest

List of usage examples for com.amazonaws.services.dynamodbv2.model PutRequest PutRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.model PutRequest PutRequest.

Prototype

public PutRequest() 

Source Link

Document

Default constructor for PutRequest object.

Usage

From source file:amazon.dynamodb.model.PutPointRequest.java

License:Open Source License

public PutPointRequest(GeoPunto geoPoint, AttributeValue rangeKeyValue) {
    putItemRequest = new PutItemRequest();
    putItemRequest.setItem(new HashMap<String, AttributeValue>());
    putRequest = new PutRequest();
    putRequest.setItem(new HashMap<String, AttributeValue>());

    this.geoPoint = geoPoint;
    this.rangeKeyValue = rangeKeyValue;
}

From source file:com.clicktravel.infrastructure.persistence.aws.dynamodb.DynamoDbTemplate.java

License:Apache License

/**
 * Creates the PutRequests to allow us to perform the batch write. Takes a batch of items and creates a bunch of
 * WriteRequests for the DynamoDB table we're writing to, then attaches PutRequests to these WriteRequests. We keep
 * a track of the versions and PutRequests for each item so we can process the results afterwards.
 * @param itemConfiguration - allows us to know which table we're writing to
 * @param itemVersions - keeps track of which version belongs to which item
 * @param requestItems - the requests to be written to DynamoDB, map of table name as the key and the WriteRequest
 *            as the value.//  w w  w . j a  v  a2  s.c  om
 * @param batch - the batch of items to be converted into WriteRequests with PutRequests.
 * @param itemPutRequests - a map to keep track of which PutRequest belongs to which item to allow us to process the
 *            results.
 */
private <T extends Item> void createRequestItems(final ItemConfiguration itemConfiguration,
        final Map<T, Long> itemVersions, final Map<String, List<WriteRequest>> requestItems,
        final List<T> batch, final Map<PutRequest, T> itemPutRequests) {
    for (final T item : batch) {
        final long newVersion = item.getVersion() != null ? item.getVersion() + 1 : 1l;
        final Map<String, AttributeValue> attributeMap = getAttributeMap(item, itemConfiguration, newVersion);
        final String tableName = databaseSchemaHolder.schemaName() + "." + itemConfiguration.tableName();
        List<WriteRequest> writeRequestsForTable = requestItems.get(tableName);
        if (writeRequestsForTable == null) {
            writeRequestsForTable = new ArrayList<WriteRequest>();
            requestItems.put(tableName, writeRequestsForTable);
        }

        final PutRequest putRequest = new PutRequest().withItem(attributeMap);
        itemPutRequests.put(putRequest, item);
        itemVersions.put(item, newVersion);
        writeRequestsForTable.add(new WriteRequest().withPutRequest(putRequest));
    }
}

From source file:com.erudika.para.persistence.AWSDynamoDAO.java

License:Apache License

private <P extends ParaObject> void writeAll(String appid, List<P> objects, boolean updateOp) {
    if (objects == null || objects.isEmpty() || StringUtils.isBlank(appid)) {
        return;// w ww.j  av a2s .c  o m
    }

    List<WriteRequest> reqs = new ArrayList<WriteRequest>(objects.size());
    int batchSteps = 1;
    if ((objects.size() > MAX_ITEMS_PER_WRITE)) {
        batchSteps = (objects.size() / MAX_ITEMS_PER_WRITE)
                + ((objects.size() % MAX_ITEMS_PER_WRITE > 0) ? 1 : 0);
    }

    Iterator<P> it = objects.iterator();
    int j = 0;

    for (int i = 0; i < batchSteps; i++) {
        while (it.hasNext() && j < MAX_ITEMS_PER_WRITE) {
            ParaObject object = it.next();
            if (StringUtils.isBlank(object.getId())) {
                object.setId(Utils.getNewId());
            }
            if (object.getTimestamp() == null) {
                object.setTimestamp(Utils.timestamp());
            }
            if (updateOp) {
                object.setUpdated(Utils.timestamp());
            }
            object.setAppid(appid);
            Map<String, AttributeValue> row = toRow(object, null);
            setRowKey(getKeyForAppid(object.getId(), appid), row);
            reqs.add(new WriteRequest().withPutRequest(new PutRequest().withItem(row)));
            j++;
        }
        batchWrite(Collections.singletonMap(getTableNameForAppid(appid), reqs));
        reqs.clear();
        j = 0;
    }
}

From source file:com.grublr.geo.model.PutPointRequest.java

License:Open Source License

public PutPointRequest(GeoPoint geoPoint, AttributeValue rangeKeyValue) {
    putItemRequest = new PutItemRequest();
    putItemRequest.setItem(new HashMap<String, AttributeValue>());
    putRequest = new PutRequest();
    putRequest.setItem(new HashMap<String, AttributeValue>());

    this.geoPoint = geoPoint;
    this.rangeKeyValue = rangeKeyValue;
}

From source file:com.intuit.tank.persistence.databases.AmazonDynamoDatabaseDocApi.java

License:Open Source License

/**
 * /*w  ww.java2  s .c om*/
 * @{inheritDoc
 */
@Override
public void addTimingResults(final @Nonnull String tableName, final @Nonnull List<TankResult> results,
        boolean async) {
    if (!results.isEmpty()) {
        Runnable task = new Runnable() {
            public void run() {
                MethodTimer mt = new MethodTimer(logger, this.getClass(), "addTimingResults (" + results + ")");
                List<WriteRequest> requests = new ArrayList<WriteRequest>();
                try {
                    for (TankResult result : results) {
                        Map<String, AttributeValue> item = getTimingAttributes(result);
                        PutRequest putRequest = new PutRequest().withItem(item);
                        WriteRequest writeRequest = new WriteRequest().withPutRequest(putRequest);
                        requests.add(writeRequest);
                    }
                    sendBatch(tableName, requests);
                } catch (Exception t) {
                    logger.error("Error adding results: " + t.getMessage(), t);
                    throw new RuntimeException(t);
                }
                mt.endAndLog();
            }
        };
        if (async) {
            EXECUTOR.execute(task);
        } else {
            task.run();
        }
    }
}

From source file:com.intuit.tank.persistence.databases.AmazonDynamoDatabaseDocApi.java

License:Open Source License

/**
 * @{inheritDoc//from   w  w w .  j av a 2 s.  c  om
 */
@Override
public void addItems(final String tableName, List<Item> itemList, final boolean asynch) {
    if (!itemList.isEmpty()) {
        final List<Item> items = new ArrayList<Item>(itemList);
        Runnable task = new Runnable() {
            public void run() {
                MethodTimer mt = new MethodTimer(logger, this.getClass(), "addItems (" + items + ")");
                List<WriteRequest> requests = new ArrayList<WriteRequest>();
                try {
                    for (Item item : items) {
                        Map<String, AttributeValue> toInsert = itemToMap(item);
                        PutRequest putRequest = new PutRequest().withItem(toInsert);
                        WriteRequest writeRequest = new WriteRequest().withPutRequest(putRequest);
                        requests.add(writeRequest);
                    }
                    sendBatch(tableName, requests);
                } catch (Exception t) {
                    logger.error("Error adding results: " + t.getMessage(), t);
                    throw new RuntimeException(t);
                }
                mt.endAndLog();
            }
        };
        if (asynch) {
            EXECUTOR.execute(task);
        } else {
            task.run();
        }
    }
}

From source file:com.mortardata.pig.storage.DynamoDBStorage.java

License:Apache License

private WriteRequestWithCapacity getWriteRequestWithCapacity(Tuple tuple) throws IOException {
    ResourceFieldSchema[] fields = this.schema.getFields();
    Map<String, AttributeValue> dynamoItem = new HashMap<String, AttributeValue>();
    int dataSize = 0;
    int dynamoItemSize = 0;
    int tupleSize = tuple.size();
    for (int i = 0; i < tupleSize; i++) {
        Object field = tuple.get(i);
        AttributeValue dynamoValue = null;

        switch (DataType.findType(field)) {

        case DataType.NULL:
            // dynamodb does not support null values
            // simply don't write field
            reportCounter(DYNAMO_COUNTER_NULL_FIELDS_DISCARDED, 1);
            break;
        case DataType.BOOLEAN:
            if (((Boolean) field).booleanValue()) {
                dynamoValue = new AttributeValue().withN("1");
            } else {
                dynamoValue = new AttributeValue().withN("0");
            }//from   www .  ja v a 2 s.  co  m
            dataSize += 1;
            dynamoItemSize += 1;
            break;
        case DataType.INTEGER:
        case DataType.LONG:
        case DataType.FLOAT:
        case DataType.DOUBLE:
            String numAsString = field.toString();
            dynamoValue = new AttributeValue().withN(numAsString);
            dataSize += numAsString.length();
            dynamoItemSize += numAsString.length();
            break;
        case DataType.BYTEARRAY:
            byte[] b = ((DataByteArray) field).get();
            ByteBuffer buffer = ByteBuffer.allocate(b.length);
            buffer.put(b, 0, b.length);
            buffer.position(0);
            dynamoValue = new AttributeValue().withB(buffer);
            dataSize += b.length;
            dynamoItemSize += b.length;
            break;
        case DataType.CHARARRAY:
            String fieldStr = (String) field;
            int fieldLen = fieldStr.length();
            if (fieldLen > 0) {
                dynamoValue = new AttributeValue().withS(fieldStr);
                dataSize += fieldLen;
                dynamoItemSize += fieldLen;
            } else {
                // DynamoDB cannot handle empty strings
                reportCounter(DYNAMO_COUNTER_EMPTY_STRING_FIELDS_DISCARDED, 1);
            }
            break;
        case DataType.BYTE:
            ByteBuffer buf = ByteBuffer.allocate(1);
            buf.put((Byte) field);
            buf.position(0);
            dynamoValue = new AttributeValue().withB(buf);
            dataSize += 1;
            dynamoItemSize += 1;
            break;
        case DataType.MAP:
        case DataType.TUPLE:
        case DataType.BAG:
            throw new RuntimeException("DynamoDBStorage does not support Maps, Tuples or Bags");
        }

        if (dynamoValue != null) {
            ResourceFieldSchema fieldSchema = fields[i];
            String fieldName = fieldSchema.getName();
            if (fieldName == null) {
                throw new IllegalArgumentException(
                        "Cannot write a field with no name (element " + i + " )  FieldSchema: " + fields);
            }
            dynamoItemSize += fieldName.length();
            dynamoItem.put(fieldName, dynamoValue);
        }
    }

    // check for max item size
    if (dynamoItemSize > DYNAMO_MAX_ITEM_SIZE_IN_BYTES) {
        throw new RuntimeException("Item size " + dynamoItemSize + " bytes is larger than max dynamo item size "
                + DYNAMO_MAX_ITEM_SIZE_IN_BYTES + ". Aborting. Item: " + dynamoItem);
    }

    WriteRequest writeRequest = new WriteRequest().withPutRequest(new PutRequest().withItem(dynamoItem));

    return new WriteRequestWithCapacity(writeRequest, dynamoItemSize, dataSize);

}

From source file:com.netflix.config.sources.DynamoDbIntegrationTestHelper.java

License:Apache License

static void addElements(AmazonDynamoDB dbClient, String tableName) {
    Map<String, List<WriteRequest>> requestMap = new HashMap<String, List<WriteRequest>>(1);
    List<WriteRequest> writeList = new ArrayList<WriteRequest>(3);

    Map<String, AttributeValue> item1 = new HashMap<String, AttributeValue>(1);
    item1.put(DynamoDbConfigurationSource.defaultKeyAttribute, new AttributeValue().withS("test1"));
    item1.put(DynamoDbConfigurationSource.defaultValueAttribute, new AttributeValue().withS("val1"));
    writeList.add(new WriteRequest().withPutRequest(new PutRequest().withItem(item1)));

    HashMap<String, AttributeValue> item2 = new HashMap<String, AttributeValue>(1);
    item2.put(DynamoDbConfigurationSource.defaultKeyAttribute, new AttributeValue().withS("test2"));
    item2.put(DynamoDbConfigurationSource.defaultValueAttribute, new AttributeValue().withS("val2"));
    writeList.add(new WriteRequest().withPutRequest(new PutRequest().withItem(item2)));

    HashMap<String, AttributeValue> item3 = new HashMap<String, AttributeValue>(1);
    item3.put(DynamoDbConfigurationSource.defaultKeyAttribute, new AttributeValue().withS("test3"));
    item3.put(DynamoDbConfigurationSource.defaultValueAttribute, new AttributeValue().withS("val3"));
    writeList.add(new WriteRequest().withPutRequest(new PutRequest().withItem(item3)));

    requestMap.put(tableName, writeList);

    BatchWriteItemRequest request = new BatchWriteItemRequest().withRequestItems(requestMap);
    dbClient.batchWriteItem(request);//from  ww w.j  av a  2s. c om
}

From source file:dynamok.sink.DynamoDbSinkTask.java

License:Apache License

private PutRequest toPutRequest(SinkRecord record) {
    final PutRequest put = new PutRequest();
    if (!config.ignoreRecordValue) {
        insert(ValueSource.RECORD_VALUE, record.valueSchema(), record.value(), put);
    }//w ww.j  a va2 s .  c o  m
    if (!config.ignoreRecordKey) {
        insert(ValueSource.RECORD_KEY, record.keySchema(), record.key(), put);
    }
    if (config.kafkaCoordinateNames != null) {
        put.addItemEntry(config.kafkaCoordinateNames.topic, new AttributeValue().withS(record.topic()));
        put.addItemEntry(config.kafkaCoordinateNames.partition,
                new AttributeValue().withN(String.valueOf(record.kafkaPartition())));
        put.addItemEntry(config.kafkaCoordinateNames.offset,
                new AttributeValue().withN(String.valueOf(record.kafkaOffset())));
    }
    return put;
}

From source file:org.apache.beam.sdk.io.aws.dynamodb.DynamoDBIOTestHelper.java

License:Apache License

private static PutRequest generatePutRequest(String hashKeyData, String rangeKeyData) {
    PutRequest putRequest = new PutRequest();
    putRequest.addItemEntry(ATTR_NAME_1, new AttributeValue(hashKeyData));
    putRequest.addItemEntry(ATTR_NAME_2, new AttributeValue().withN(rangeKeyData));
    return putRequest;
}