Search in sources :

Example 16 with DynamoDbClient

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

the class UpdateItem method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    UpdateItem <tableName> <key> <keyVal> <name> <updateVal>\n\n" + "Where:\n" + "    tableName - the Amazon DynamoDB table (for example, Music3).\n" + "    key - the name of the key in the table (for example, Artist).\n" + "    keyVal - the value of the key (for example, Famous Band).\n" + "    name - the name of the column where the value is updated (for example, Awards).\n" + "    updateVal - the value used to update an item (for example, 14).\n" + " Example:\n" + "    UpdateItem Music3 Artist Famous Band Awards 14\n";
    if (args.length != 5) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String tableName = args[0];
    String key = args[1];
    String keyVal = args[2];
    String name = args[3];
    String updateVal = args[4];
    Region region = Region.US_EAST_1;
    DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    updateTableItem(ddb, tableName, key, keyVal, name, updateVal);
    ddb.close();
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) Region(software.amazon.awssdk.regions.Region)

Example 17 with DynamoDbClient

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

the class DynamoDBService method getClient.

private DynamoDbClient getClient() {
    // Create a DynamoDbClient object.
    Region region = Region.US_EAST_1;
    DynamoDbClient ddb = DynamoDbClient.builder().credentialsProvider(EnvironmentVariableCredentialsProvider.create()).region(region).build();
    return ddb;
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) Region(software.amazon.awssdk.regions.Region)

Example 18 with DynamoDbClient

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

the class DynamoDBService method archiveItem.

// Archives an item based on the key.
public String archiveItem(String id) {
    DynamoDbClient ddb = getClient();
    HashMap<String, AttributeValue> itemKey = new HashMap<String, AttributeValue>();
    itemKey.put("id", AttributeValue.builder().s(id).build());
    HashMap<String, AttributeValueUpdate> updatedValues = new HashMap<String, AttributeValueUpdate>();
    // Update the column specified by name with updatedVal.
    updatedValues.put("archive", AttributeValueUpdate.builder().value(AttributeValue.builder().s("Closed").build()).action(AttributeAction.PUT).build());
    UpdateItemRequest request = UpdateItemRequest.builder().tableName("Work").key(itemKey).attributeUpdates(updatedValues).build();
    try {
        ddb.updateItem(request);
        return "The item was successfully archived";
    } catch (ResourceNotFoundException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return "";
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient)

Example 19 with DynamoDbClient

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

the class DynamoDBService method getClient.

private DynamoDbClient getClient() {
    // Create a DynamoDbClient object.
    Region region = Region.US_EAST_1;
    DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    return ddb;
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) Region(software.amazon.awssdk.regions.Region)

Example 20 with DynamoDbClient

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

the class DynamoDBService method setItem.

public void setItem(PopData pop) {
    // Create a DynamoDbEnhancedClient.
    DynamoDbClient ddb = getClient();
    DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
    try {
        // Create a DynamoDbTable object.
        DynamoDbTable<Population> workTable = enhancedClient.table("Country", TableSchema.fromBean(Population.class));
        // Populate the table.
        Population record = new Population();
        String name = pop.getName();
        String code = pop.getCode();
        record.setId(name);
        record.setCode(code);
        record.set2010(pop.get2010());
        record.set2011(pop.get2011());
        record.set2012(pop.get2012());
        record.set2013(pop.get2013());
        record.set2014(pop.get2014());
        record.set2015(pop.get2015());
        record.set2016(pop.get2016());
        record.set2017(pop.get2017());
        record.set2018(pop.get2018());
        record.set2019(pop.get2019());
        // Put the customer data into a DynamoDB table.
        workTable.putItem(record);
        System.out.println("Added record " + recNum);
        recNum++;
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

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