Search in sources :

Example 11 with DynamoDbClient

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

the class ScanEmployees method sendEmployeMessage.

public Boolean sendEmployeMessage() {
    Boolean send = false;
    String myDate = getDate();
    Region region = Region.US_WEST_2;
    DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    // Create a DynamoDbEnhancedClient and use the DynamoDbClient object.
    DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
    // Create a DynamoDbTable object based on Employee.
    DynamoDbTable<Employee> table = enhancedClient.table("Employee", TableSchema.fromBean(Employee.class));
    try {
        AttributeValue attVal = AttributeValue.builder().s(myDate).build();
        // Get only items in the Employee table that match the date.
        Map<String, AttributeValue> myMap = new HashMap<>();
        myMap.put(":val1", attVal);
        Map<String, String> myExMap = new HashMap<>();
        myExMap.put("#startDate", "startDate");
        Expression expression = Expression.builder().expressionValues(myMap).expressionNames(myExMap).expression("#startDate = :val1").build();
        ScanEnhancedRequest enhancedRequest = ScanEnhancedRequest.builder().filterExpression(expression).limit(// you can increase this value.
        15).build();
        // Get items in the Employee table.
        Iterator<Employee> employees = table.scan(enhancedRequest).items().iterator();
        while (employees.hasNext()) {
            Employee employee = employees.next();
            String first = employee.getFirst();
            String phone = employee.getPhone();
            // Send an anniversary message.
            sentTextMessage(first, phone);
            send = true;
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return send;
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) ScanEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.ScanEnhancedRequest) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) Region(software.amazon.awssdk.regions.Region) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Example 12 with DynamoDbClient

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

the class PersistCase method putRecord.

// Puts an item into a DynamoDB table
public void putRecord(String caseId, String employeeName, String email) {
    // Create a DynamoDbClient object
    Region region = Region.US_WEST_2;
    DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    // Create a DynamoDbEnhancedClient and use the DynamoDbClient object
    DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
    try {
        // Create a DynamoDbTable object
        DynamoDbTable<Case> caseTable = enhancedClient.table("Case", TableSchema.fromBean(Case.class));
        // Create an Instant object
        LocalDate localDate = LocalDate.parse("2020-04-07");
        LocalDateTime localDateTime = localDate.atStartOfDay();
        Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
        // Populate the table
        Case caseRecord = new Case();
        caseRecord.setName(employeeName);
        caseRecord.setId(caseId);
        caseRecord.setEmail(email);
        caseRecord.setRegistrationDate(instant);
        // Put the case data into a DynamoDB table
        caseTable.putItem(caseRecord);
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("done");
}
Also used : LocalDateTime(java.time.LocalDateTime) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) Instant(java.time.Instant) Region(software.amazon.awssdk.regions.Region) LocalDate(java.time.LocalDate) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Example 13 with DynamoDbClient

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

the class ListTables method main.

public static void main(String[] args) {
    System.out.println("Your Amazon DynamoDB tables:\n");
    Region region = Region.US_EAST_1;
    DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    listAllTables(ddb);
    ddb.close();
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) Region(software.amazon.awssdk.regions.Region)

Example 14 with DynamoDbClient

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

the class PutItemEncrypt method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    PutItem <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" + "    keyId - a KMS key id value to use to encrypt/decrypt the data (for example, xxxxxbcd-12ab-34cd-56ef-1234567890ab).";
    if (args.length != 10) {
        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];
    String keyId = args[9];
    Region region = Region.US_WEST_2;
    DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    // Create a KmsClient object to use to encrpt data
    KmsClient kmsClient = KmsClient.builder().region(region).build();
    putItemInTable(ddb, kmsClient, tableName, key, keyVal, albumTitle, albumTitleValue, awards, awardVal, songTitle, songTitleVal, keyId);
    System.out.println("Done!");
    ddb.close();
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) Region(software.amazon.awssdk.regions.Region) KmsClient(software.amazon.awssdk.services.kms.KmsClient)

Example 15 with DynamoDbClient

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

the class Scenario method main.

public static void main(String[] args) throws IOException {
    final String USAGE = "\n" + "Usage:\n" + "    <fileName>\n\n" + "Where:\n" + "    fileName - the path to the moviedata.json file that you can download from the Amazon DynamoDB Developer Guide.\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String tableName = "Movies";
    String fileName = args[0];
    Region region = Region.US_EAST_1;
    DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    System.out.println("******* Creating an Amazon DynamoDB table named Movies with a key named year and a sort key named title.");
    createTable(ddb, tableName);
    System.out.println("******* Loading data into the Amazon DynamoDB table.");
    loadData(ddb, tableName, fileName);
    System.out.println("******* Getting data from the Movie table.");
    getItem(ddb);
    System.out.println("******* Putting a record into the Amazon DynamoDB table.");
    putRecord(ddb);
    System.out.println("******* Updating a record.");
    updateTableItem(ddb, tableName);
    System.out.println("******* Scanning the Amazon DynamoDB table.");
    scanMovies(ddb, tableName);
    System.out.println("******* Querying the Movies released in 2013.");
    queryTable(ddb);
    System.out.println("******* Deleting the Amazon DynamoDB table.");
    deleteDynamoDBTable(ddb, tableName);
    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