Search in sources :

Example 1 with Item

use of com.amazonaws.services.dynamodbv2.document.Item in project archaius by Netflix.

the class DynamoDbConfigurationSource method loadPropertiesFromTable.

@Override
protected synchronized Map<String, Object> loadPropertiesFromTable(String table) {
    Map<String, Object> propertyMap = new HashMap<String, Object>();
    Map<String, AttributeValue> lastKeysEvaluated = null;
    do {
        ScanRequest scanRequest = new ScanRequest().withTableName(table).withExclusiveStartKey(lastKeysEvaluated);
        ScanResult result = dbScanWithThroughputBackOff(scanRequest);
        for (Map<String, AttributeValue> item : result.getItems()) {
            propertyMap.put(item.get(keyAttributeName.get()).getS(), item.get(valueAttributeName.get()).getS());
        }
        lastKeysEvaluated = result.getLastEvaluatedKey();
    } while (lastKeysEvaluated != null);
    return propertyMap;
}
Also used : ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult) HashMap(java.util.HashMap)

Example 2 with Item

use of com.amazonaws.services.dynamodbv2.document.Item in project openhab1-addons by openhab.

the class DynamoDBPersistenceService method store.

/**
     * {@inheritDoc}
     */
@Override
public void store(Item item, String alias) {
    if (item.getState() instanceof UnDefType) {
        logger.debug("Undefined item state received. Not storing item.");
        return;
    }
    if (!isProperlyConfigured) {
        logger.warn("Configuration for dynamodb not yet loaded or broken. Not storing item.");
        return;
    }
    if (!maybeConnectAndCheckConnection()) {
        logger.warn("DynamoDB not connected. Not storing item.");
        return;
    }
    String realName = item.getName();
    String name = (alias != null) ? alias : realName;
    Date time = new Date(System.currentTimeMillis());
    State state = item.getState();
    logger.trace("Tried to get item from item class {}, state is {}", item.getClass(), state.toString());
    DynamoDBItem<?> dynamoItem = AbstractDynamoDBItem.fromState(name, state, time);
    DynamoDBMapper mapper = getDBMapper(tableNameResolver.fromItem(dynamoItem));
    if (!createTable(mapper, dynamoItem.getClass())) {
        logger.warn("Table creation failed. Not storing item");
        return;
    }
    try {
        logger.debug("storing {} in dynamo. Serialized value {}. Original Item: {}", name, state, item);
        mapper.save(dynamoItem);
        logger.debug("Sucessfully stored item {}", item);
    } catch (AmazonClientException e) {
        logger.error("Error storing object to dynamo: {}", e.getMessage());
    }
}
Also used : UnDefType(org.openhab.core.types.UnDefType) State(org.openhab.core.types.State) AmazonClientException(com.amazonaws.AmazonClientException) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) Date(java.util.Date)

Example 3 with Item

use of com.amazonaws.services.dynamodbv2.document.Item in project openhab1-addons by openhab.

the class DynamoDBPersistenceService method createQueryExpression.

/**
     * Construct dynamodb query from filter
     *
     * @param filter
     * @return DynamoDBQueryExpression corresponding to the given FilterCriteria
     */
private DynamoDBQueryExpression<DynamoDBItem<?>> createQueryExpression(Class<? extends DynamoDBItem<?>> dtoClass, FilterCriteria filter) {
    DynamoDBItem<?> item = getDynamoDBHashKey(dtoClass, filter.getItemName());
    final DynamoDBQueryExpression<DynamoDBItem<?>> queryExpression = new DynamoDBQueryExpression<DynamoDBItem<?>>().withHashKeyValues(item).withScanIndexForward(filter.getOrdering() == Ordering.ASCENDING).withLimit(filter.getPageSize());
    Condition timeFilter = maybeAddTimeFilter(queryExpression, filter);
    maybeAddStateFilter(filter, queryExpression);
    logger.debug("Querying: {} with {}", filter.getItemName(), timeFilter);
    return queryExpression;
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition)

Example 4 with Item

use of com.amazonaws.services.dynamodbv2.document.Item in project nifi by apache.

the class AbstractWriteDynamoDBProcessor method handleUnprocessedItems.

/**
 * Helper method to handle unprocessed items items
 * @param session process session
 * @param keysToFlowFileMap map of flow db primary key to flow file
 * @param table dynamodb table
 * @param hashKeyName the hash key name
 * @param hashKeyValueType the hash key value
 * @param rangeKeyName the range key name
 * @param rangeKeyValueType range key value
 * @param outcome the write outcome
 */
protected void handleUnprocessedItems(final ProcessSession session, Map<ItemKeys, FlowFile> keysToFlowFileMap, final String table, final String hashKeyName, final String hashKeyValueType, final String rangeKeyName, final String rangeKeyValueType, BatchWriteItemOutcome outcome) {
    BatchWriteItemResult result = outcome.getBatchWriteItemResult();
    // Handle unprocessed items
    List<WriteRequest> unprocessedItems = result.getUnprocessedItems().get(table);
    if (unprocessedItems != null && unprocessedItems.size() > 0) {
        for (WriteRequest request : unprocessedItems) {
            Map<String, AttributeValue> item = getRequestItem(request);
            Object hashKeyValue = getValue(item, hashKeyName, hashKeyValueType);
            Object rangeKeyValue = getValue(item, rangeKeyName, rangeKeyValueType);
            sendUnprocessedToUnprocessedRelationship(session, keysToFlowFileMap, hashKeyValue, rangeKeyValue);
        }
    }
}
Also used : BatchWriteItemResult(com.amazonaws.services.dynamodbv2.model.BatchWriteItemResult) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) WriteRequest(com.amazonaws.services.dynamodbv2.model.WriteRequest)

Example 5 with Item

use of com.amazonaws.services.dynamodbv2.document.Item in project nifi by apache.

the class DeleteDynamoDB method onTrigger.

@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;
    }
    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);
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) TableWriteItems(com.amazonaws.services.dynamodbv2.document.TableWriteItems) BatchWriteItemOutcome(com.amazonaws.services.dynamodbv2.document.BatchWriteItemOutcome) HashMap(java.util.HashMap) AmazonClientException(com.amazonaws.AmazonClientException) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonClientException(com.amazonaws.AmazonClientException) AmazonServiceException(com.amazonaws.AmazonServiceException) List(java.util.List)

Aggregations

AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)49 Item (com.amazonaws.services.dynamodbv2.document.Item)46 Table (com.amazonaws.services.dynamodbv2.document.Table)42 HashMap (java.util.HashMap)41 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)29 AmazonServiceException (com.amazonaws.AmazonServiceException)27 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)17 AmazonClientException (com.amazonaws.AmazonClientException)15 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)14 IOException (java.io.IOException)14 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)11 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)10 QuerySpec (com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)10 Date (java.util.Date)10 Map (java.util.Map)10 QueryRequest (com.amazonaws.services.dynamodbv2.model.QueryRequest)8 QueryResult (com.amazonaws.services.dynamodbv2.model.QueryResult)8 HashSet (java.util.HashSet)8 List (java.util.List)8