Search in sources :

Example 26 with AttributeValue

use of software.amazon.awssdk.services.dynamodb.model.AttributeValue in project beam by apache.

the class AttributeValueCoderTest method shouldPassForStringType.

@Test
public void shouldPassForStringType() throws IOException {
    AttributeValue expected = AttributeValue.builder().s("test").build();
    AttributeValueCoder coder = AttributeValueCoder.of();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    coder.encode(expected, output);
    ByteArrayInputStream in = new ByteArrayInputStream(output.toByteArray());
    AttributeValue actual = coder.decode(in);
    Assert.assertEquals(expected, actual);
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 27 with AttributeValue

use of software.amazon.awssdk.services.dynamodb.model.AttributeValue in project beam by apache.

the class DynamoDBIOIT method buildWriteRequest.

private static KV<String, WriteRequest> buildWriteRequest(TestRow row) {
    AttributeValue id = AttributeValue.builder().n(row.id().toString()).build();
    AttributeValue name = AttributeValue.builder().s(row.name()).build();
    PutRequest req = PutRequest.builder().item(ImmutableMap.of(COL_ID, id, COL_NAME, name)).build();
    return KV.of(env.options().getDynamoDBTable(), WriteRequest.builder().putRequest(req).build());
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) PutRequest(software.amazon.awssdk.services.dynamodb.model.PutRequest)

Example 28 with AttributeValue

use of software.amazon.awssdk.services.dynamodb.model.AttributeValue in project cas by apereo.

the class DynamoDbAuditTrailManagerFacilitator method getAuditRecordsSince.

/**
 * Gets audit records.
 *
 * @param localDate the local date
 * @return the audit records since
 */
@SuppressWarnings("JavaUtilDate")
public Set<? extends AuditActionContext> getAuditRecordsSince(final LocalDate localDate) {
    val time = DateTimeUtils.dateOf(localDate).getTime();
    val keys = DynamoDbQueryBuilder.builder().key(ColumnNames.WHEN_ACTION_PERFORMED.getColumnName()).attributeValue(List.of(AttributeValue.builder().s(String.valueOf(time)).build())).operator(ComparisonOperator.GE).build();
    return DynamoDbTableUtils.getRecordsByKeys(amazonDynamoDBClient, dynamoDbProperties.getTableName(), List.of(keys), item -> {
        val principal = item.get(ColumnNames.PRINCIPAL.getColumnName()).s();
        val actionPerformed = item.get(ColumnNames.ACTION_PERFORMED.getColumnName()).s();
        val appCode = item.get(ColumnNames.APPLICATION_CODE.getColumnName()).s();
        val clientIp = item.get(ColumnNames.CLIENT_IP_ADDRESS.getColumnName()).s();
        val serverIp = item.get(ColumnNames.SERVER_IP_ADDRESS.getColumnName()).s();
        val resource = item.get(ColumnNames.RESOURCE_OPERATED_UPON.getColumnName()).s();
        val userAgent = item.get(ColumnNames.USER_AGENT.getColumnName()).s();
        val time1 = Long.parseLong(item.get(ColumnNames.WHEN_ACTION_PERFORMED.getColumnName()).s());
        return new AuditActionContext(principal, resource, actionPerformed, appCode, new Date(time1), clientIp, serverIp, userAgent);
    }).collect(Collectors.toSet());
}
Also used : lombok.val(lombok.val) Getter(lombok.Getter) SneakyThrows(lombok.SneakyThrows) Date(java.util.Date) RequiredArgsConstructor(lombok.RequiredArgsConstructor) HashMap(java.util.HashMap) AuditActionContext(org.apereo.inspektr.audit.AuditActionContext) DynamoDbTableUtils(org.apereo.cas.dynamodb.DynamoDbTableUtils) StringUtils(org.apache.commons.lang3.StringUtils) ComparisonOperator(software.amazon.awssdk.services.dynamodb.model.ComparisonOperator) Map(java.util.Map) ScalarAttributeType(software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType) AuditDynamoDbProperties(org.apereo.cas.configuration.model.support.dynamodb.AuditDynamoDbProperties) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) DateTimeUtils(org.apereo.cas.util.DateTimeUtils) lombok.val(lombok.val) KeyType(software.amazon.awssdk.services.dynamodb.model.KeyType) DynamoDbQueryBuilder(org.apereo.cas.dynamodb.DynamoDbQueryBuilder) Set(java.util.Set) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) Collectors(java.util.stream.Collectors) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) KeySchemaElement(software.amazon.awssdk.services.dynamodb.model.KeySchemaElement) PutItemRequest(software.amazon.awssdk.services.dynamodb.model.PutItemRequest) LocalDate(java.time.LocalDate) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) AuditActionContext(org.apereo.inspektr.audit.AuditActionContext) Date(java.util.Date) LocalDate(java.time.LocalDate)

Example 29 with AttributeValue

use of software.amazon.awssdk.services.dynamodb.model.AttributeValue in project cas by apereo.

the class DynamoDbTableUtils method getRecordsByKeys.

/**
 * Gets records by keys.
 *
 * @param <T>            the type parameter
 * @param dynamoDbClient the dynamo db client
 * @param tableName      the table name
 * @param queries        the queries
 * @param itemMapper     the item mapper
 * @return the records by keys
 */
public static <T> Stream<T> getRecordsByKeys(final DynamoDbClient dynamoDbClient, final String tableName, final List<DynamoDbQueryBuilder> queries, final Function<Map<String, AttributeValue>, T> itemMapper) {
    try {
        val scanFilter = queries.stream().map(query -> {
            val cond = Condition.builder().comparisonOperator(query.getOperator()).attributeValueList(query.getAttributeValue()).build();
            return Pair.of(query.getKey(), cond);
        }).collect(Collectors.toMap(Pair::getKey, Pair::getValue));
        val scanRequest = ScanRequest.builder().tableName(tableName).scanFilter(scanFilter).build();
        LOGGER.debug("Submitting request [{}] to get record with keys [{}]", scanRequest, queries);
        val items = dynamoDbClient.scan(scanRequest).items();
        return items.stream().map(itemMapper);
    } catch (final Exception e) {
        LoggingUtils.error(LOGGER, e);
    }
    return Stream.empty();
}
Also used : lombok.val(lombok.val) Condition(software.amazon.awssdk.services.dynamodb.model.Condition) TableDescription(software.amazon.awssdk.services.dynamodb.model.TableDescription) AbstractDynamoDbProperties(org.apereo.cas.configuration.model.support.dynamodb.AbstractDynamoDbProperties) Function(java.util.function.Function) UtilityClass(lombok.experimental.UtilityClass) LoggingUtils(org.apereo.cas.util.LoggingUtils) Pair(org.apache.commons.lang3.tuple.Pair) ProvisionedThroughput(software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest) ScanRequest(software.amazon.awssdk.services.dynamodb.model.ScanRequest) Map(java.util.Map) DeleteTableRequest(software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) TableStatus(software.amazon.awssdk.services.dynamodb.model.TableStatus) lombok.val(lombok.val) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) Collectors(java.util.stream.Collectors) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) KeySchemaElement(software.amazon.awssdk.services.dynamodb.model.KeySchemaElement) Stream(java.util.stream.Stream) BillingMode(software.amazon.awssdk.services.dynamodb.model.BillingMode) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DescribeTableRequest(software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)

Example 30 with AttributeValue

use of software.amazon.awssdk.services.dynamodb.model.AttributeValue in project syndesis-qe by syndesisio.

the class DynamoDbUtils method purgeTable.

public void purgeTable() {
    ScanRequest scanRequest = ScanRequest.builder().tableName(tableName).build();
    ScanResponse result = dynamoDb.scan(scanRequest);
    for (Map<String, AttributeValue> item : result.items()) {
        dynamoDb.deleteItem(DeleteItemRequest.builder().tableName(tableName).key(item).build());
    }
}
Also used : ScanRequest(software.amazon.awssdk.services.dynamodb.model.ScanRequest) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) ScanResponse(software.amazon.awssdk.services.dynamodb.model.ScanResponse)

Aggregations

AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)31 HashMap (java.util.HashMap)14 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)13 Test (org.junit.Test)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 Map (java.util.Map)6 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)5 ResourceNotFoundException (software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)5 ScanRequest (software.amazon.awssdk.services.dynamodb.model.ScanRequest)5 Collectors (java.util.stream.Collectors)4 PutItemRequest (software.amazon.awssdk.services.dynamodb.model.PutItemRequest)4 List (java.util.List)3 Slf4j (lombok.extern.slf4j.Slf4j)3 lombok.val (lombok.val)3 AttributeDefinition (software.amazon.awssdk.services.dynamodb.model.AttributeDefinition)3 KeySchemaElement (software.amazon.awssdk.services.dynamodb.model.KeySchemaElement)3 ScanResponse (software.amazon.awssdk.services.dynamodb.model.ScanResponse)3 Set (java.util.Set)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2