Search in sources :

Example 21 with DynamoDbClient

use of software.amazon.awssdk.services.dynamodb.DynamoDbClient 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 22 with DynamoDbClient

use of software.amazon.awssdk.services.dynamodb.DynamoDbClient in project aws-doc-sdk-examples by awsdocs.

the class EnhancedScanRecords method main.

public static void main(String[] args) {
    Region region = Region.US_EAST_1;
    DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
    scan(enhancedClient);
    ddb.close();
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) Region(software.amazon.awssdk.regions.Region) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Example 23 with DynamoDbClient

use of software.amazon.awssdk.services.dynamodb.DynamoDbClient in project aws-doc-sdk-examples by awsdocs.

the class EnhancedTableSchema method main.

public static void main(String[] args) {
    Region region = Region.US_EAST_1;
    DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
    putRecord(enhancedClient);
    ddb.close();
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) Region(software.amazon.awssdk.regions.Region) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Example 24 with DynamoDbClient

use of software.amazon.awssdk.services.dynamodb.DynamoDbClient in project aws-doc-sdk-examples by awsdocs.

the class PutItem method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    <tableName> <key> <keyVal> <albumtitle> <albumtitleval> <awards> <awardsval> <Songtitle> <songtitleval>\n\n" + "Where:\n" + "    tableName - the Amazon DynamoDB table in which an item is placed (for example, Music3).\n" + "    key - the key used in the Amazon DynamoDB table (for example, Artist).\n" + "    keyval - the key value that represents the item to get (for example, Famous Band).\n" + "    albumTitle - album title (for example, AlbumTitle).\n" + "    AlbumTitleValue - the name of the album (for example, Songs About Life ).\n" + "    Awards - the awards column (for example, Awards).\n" + "    AwardVal - the value of the awards (for example, 10).\n" + "    SongTitle - the song title (for example, SongTitle).\n" + "    SongTitleVal - the value of the song title (for example, Happy Day).\n" + "**Warning** This program will  place an item that you specify into a table!\n";
    if (args.length != 9) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String tableName = args[0];
    String key = args[1];
    String keyVal = args[2];
    String albumTitle = args[3];
    String albumTitleValue = args[4];
    String awards = args[5];
    String awardVal = args[6];
    String songTitle = args[7];
    String songTitleVal = args[8];
    Region region = Region.US_WEST_2;
    DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    putItemInTable(ddb, tableName, key, keyVal, albumTitle, albumTitleValue, awards, awardVal, songTitle, songTitleVal);
    System.out.println("Done!");
    ddb.close();
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) Region(software.amazon.awssdk.regions.Region)

Example 25 with DynamoDbClient

use of software.amazon.awssdk.services.dynamodb.DynamoDbClient in project aws-doc-sdk-examples by awsdocs.

the class Query method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    Query <tableName> <partitionKeyName> <partitionKeyVal>\n\n" + "Where:\n" + "    tableName - the Amazon DynamoDB table to put the item in (for example, Music3).\n" + "    partitionKeyName - the partition key name of the Amazon DynamoDB table (for example, Artist).\n" + "    partitionKeyVal - value of the partition key that should match (for example, Famous Band).\n\n" + "Example:\n";
    if (args.length != 3) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String tableName = args[0];
    String partitionKeyName = args[1];
    String partitionKeyVal = args[2];
    // For more information about an alias, see:
    // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.ExpressionAttributeNames.html
    String partitionAlias = "#a";
    System.out.format("Querying %s", tableName);
    System.out.println("");
    Region region = Region.US_EAST_1;
    DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    int count = queryTable(ddb, tableName, partitionKeyName, partitionKeyVal, partitionAlias);
    System.out.println("There were " + count + "record(s) returned");
    ddb.close();
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) Region(software.amazon.awssdk.regions.Region)

Aggregations

DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)40 Region (software.amazon.awssdk.regions.Region)32 DynamoDbEnhancedClient (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)13 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)5 Instant (java.time.Instant)2 LocalDateTime (java.time.LocalDateTime)2 List (java.util.List)2 Map (java.util.Map)2 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)2 Segment (com.amazonaws.xray.entities.Segment)1 Subsegment (com.amazonaws.xray.entities.Subsegment)1 LocalDate (java.time.LocalDate)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 UtilityClass (lombok.experimental.UtilityClass)1 Slf4j (lombok.extern.slf4j.Slf4j)1 lombok.val (lombok.val)1