Example usage for com.amazonaws.services.dynamodbv2.document TableWriteItems addHashOnlyPrimaryKeysToDelete

List of usage examples for com.amazonaws.services.dynamodbv2.document TableWriteItems addHashOnlyPrimaryKeysToDelete

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.document TableWriteItems addHashOnlyPrimaryKeysToDelete.

Prototype

public TableWriteItems addHashOnlyPrimaryKeysToDelete(String hashKeyName, Object... hashKeyValues) 

Source Link

Document

Adds multiple hash-only primary keys to be deleted in a batch write operation.

Usage

From source file:org.apache.nifi.processors.aws.dynamodb.DeleteDynamoDB.java

License:Apache License

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    List<FlowFile> flowFiles = session
            .get(context.getProperty(BATCH_SIZE).evaluateAttributeExpressions().asInteger());
    if (flowFiles == null || flowFiles.size() == 0) {
        return;/*  ww  w .  j a  va  2 s .  c  o  m*/
    }

    Map<ItemKeys, FlowFile> keysToFlowFileMap = new HashMap<>();

    final String table = context.getProperty(TABLE).evaluateAttributeExpressions().getValue();

    final String hashKeyName = context.getProperty(HASH_KEY_NAME).evaluateAttributeExpressions().getValue();
    final String hashKeyValueType = context.getProperty(HASH_KEY_VALUE_TYPE).getValue();
    final String rangeKeyName = context.getProperty(RANGE_KEY_NAME).evaluateAttributeExpressions().getValue();
    final String rangeKeyValueType = context.getProperty(RANGE_KEY_VALUE_TYPE).getValue();

    TableWriteItems tableWriteItems = new TableWriteItems(table);

    for (FlowFile flowFile : flowFiles) {
        final Object hashKeyValue = getValue(context, HASH_KEY_VALUE_TYPE, HASH_KEY_VALUE, flowFile);
        final Object rangeKeyValue = getValue(context, RANGE_KEY_VALUE_TYPE, RANGE_KEY_VALUE, flowFile);

        if (!isHashKeyValueConsistent(hashKeyName, hashKeyValue, session, flowFile)) {
            continue;
        }

        if (!isRangeKeyValueConsistent(rangeKeyName, rangeKeyValue, session, flowFile)) {
            continue;
        }

        if (rangeKeyValue == null || StringUtils.isBlank(rangeKeyValue.toString())) {
            tableWriteItems.addHashOnlyPrimaryKeysToDelete(hashKeyName, hashKeyValue);
        } else {
            tableWriteItems.addHashAndRangePrimaryKeyToDelete(hashKeyName, hashKeyValue, rangeKeyName,
                    rangeKeyValue);
        }
        keysToFlowFileMap.put(new ItemKeys(hashKeyValue, rangeKeyValue), flowFile);
    }

    if (keysToFlowFileMap.isEmpty()) {
        return;
    }

    final DynamoDB dynamoDB = getDynamoDB();

    try {
        BatchWriteItemOutcome outcome = dynamoDB.batchWriteItem(tableWriteItems);

        handleUnprocessedItems(session, keysToFlowFileMap, table, hashKeyName, hashKeyValueType, rangeKeyName,
                rangeKeyValueType, outcome);

        // All non unprocessed items are successful
        for (FlowFile flowFile : keysToFlowFileMap.values()) {
            getLogger().debug("Successfully deleted item from dynamodb : " + table);
            session.transfer(flowFile, REL_SUCCESS);
        }
    } catch (AmazonServiceException exception) {
        getLogger().error("Could not process flowFiles due to service exception : " + exception.getMessage());
        List<FlowFile> failedFlowFiles = processServiceException(session, flowFiles, exception);
        session.transfer(failedFlowFiles, REL_FAILURE);
    } catch (AmazonClientException exception) {
        getLogger().error("Could not process flowFiles due to client exception : " + exception.getMessage());
        List<FlowFile> failedFlowFiles = processClientException(session, flowFiles, exception);
        session.transfer(failedFlowFiles, REL_FAILURE);
    } catch (Exception exception) {
        getLogger().error("Could not process flowFiles due to exception : " + exception.getMessage());
        List<FlowFile> failedFlowFiles = processException(session, flowFiles, exception);
        session.transfer(failedFlowFiles, REL_FAILURE);
    }
}