Search in sources :

Example 21 with AmazonDynamoDB

use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project aws-doc-sdk-examples by awsdocs.

the class StreamsAdapterDemoHelper method putItem.

public static void putItem(AmazonDynamoDB dynamoDBClient, String tableName, java.util.Map<String, AttributeValue> items) {
    PutItemRequest putItemRequest = new PutItemRequest().withTableName(tableName).withItem(items);
    dynamoDBClient.putItem(putItemRequest);
}
Also used : PutItemRequest(com.amazonaws.services.dynamodbv2.model.PutItemRequest)

Example 22 with AmazonDynamoDB

use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project aws-doc-sdk-examples by awsdocs.

the class StreamsAdapterDemoHelper method putItem.

public static void putItem(AmazonDynamoDB dynamoDBClient, String tableName, String id, String val) {
    java.util.Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
    item.put("Id", new AttributeValue().withN(id));
    item.put("attribute-1", new AttributeValue().withS(val));
    PutItemRequest putItemRequest = new PutItemRequest().withTableName(tableName).withItem(item);
    dynamoDBClient.putItem(putItemRequest);
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) HashMap(java.util.HashMap) PutItemRequest(com.amazonaws.services.dynamodbv2.model.PutItemRequest)

Example 23 with AmazonDynamoDB

use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project aws-doc-sdk-examples by awsdocs.

the class StreamsAdapterDemoHelper method createTable.

/**
 * @return StreamArn
 */
public static String createTable(AmazonDynamoDB client, String tableName) {
    java.util.List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N"));
    java.util.List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
    // Partition
    keySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH));
    // key
    ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(2L).withWriteCapacityUnits(2L);
    StreamSpecification streamSpecification = new StreamSpecification();
    streamSpecification.setStreamEnabled(true);
    streamSpecification.setStreamViewType(StreamViewType.NEW_IMAGE);
    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName).withAttributeDefinitions(attributeDefinitions).withKeySchema(keySchema).withProvisionedThroughput(provisionedThroughput).withStreamSpecification(streamSpecification);
    try {
        System.out.println("Creating table " + tableName);
        CreateTableResult result = client.createTable(createTableRequest);
        return result.getTableDescription().getLatestStreamArn();
    } catch (ResourceInUseException e) {
        System.out.println("Table already exists.");
        return describeTable(client, tableName).getTable().getLatestStreamArn();
    }
}
Also used : StreamSpecification(com.amazonaws.services.dynamodbv2.model.StreamSpecification) ResourceInUseException(com.amazonaws.services.dynamodbv2.model.ResourceInUseException) ArrayList(java.util.ArrayList) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) CreateTableResult(com.amazonaws.services.dynamodbv2.model.CreateTableResult)

Example 24 with AmazonDynamoDB

use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project aws-doc-sdk-examples by awsdocs.

the class MoviesCreateTable method main.

public static void main(String[] args) throws Exception {
    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")).build();
    DynamoDB dynamoDB = new DynamoDB(client);
    String tableName = "Movies";
    try {
        System.out.println("Attempting to create table; please wait...");
        Table table = dynamoDB.createTable(tableName, // Partition
        Arrays.asList(// Partition
        new KeySchemaElement("year", KeyType.HASH), // key
        new KeySchemaElement("title", // Sort key
        KeyType.RANGE)), Arrays.asList(new AttributeDefinition("year", ScalarAttributeType.N), new AttributeDefinition("title", ScalarAttributeType.S)), new ProvisionedThroughput(10L, 10L));
        table.waitForActive();
        System.out.println("Success.  Table status: " + table.getDescription().getTableStatus());
    } catch (Exception e) {
        System.err.println("Unable to create table: ");
        System.err.println(e.getMessage());
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement)

Example 25 with AmazonDynamoDB

use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project aws-doc-sdk-examples by awsdocs.

the class MoviesItemOps01 method main.

public static void main(String[] args) throws Exception {
    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")).build();
    DynamoDB dynamoDB = new DynamoDB(client);
    Table table = dynamoDB.getTable("Movies");
    int year = 2015;
    String title = "The Big New Movie";
    final Map<String, Object> infoMap = new HashMap<String, Object>();
    infoMap.put("plot", "Nothing happens at all.");
    infoMap.put("rating", 0);
    try {
        System.out.println("Adding a new item...");
        PutItemOutcome outcome = table.putItem(new Item().withPrimaryKey("year", year, "title", title).withMap("info", infoMap));
        System.out.println("PutItem succeeded:\n" + outcome.getPutItemResult());
    } catch (Exception e) {
        System.err.println("Unable to add item: " + year + " " + title);
        System.err.println(e.getMessage());
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) PutItemOutcome(com.amazonaws.services.dynamodbv2.document.PutItemOutcome) Table(com.amazonaws.services.dynamodbv2.document.Table) HashMap(java.util.HashMap) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Aggregations

AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)70 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)16 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)14 Test (org.junit.Test)13 Table (com.amazonaws.services.dynamodbv2.document.Table)12 IOException (java.io.IOException)12 AmazonServiceException (com.amazonaws.AmazonServiceException)11 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)11 HashMap (java.util.HashMap)11 AmazonClientException (com.amazonaws.AmazonClientException)10 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)10 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)9 ScanRequest (com.amazonaws.services.dynamodbv2.model.ScanRequest)9 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)8 ToString (lombok.ToString)7 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)6 ArrayList (java.util.ArrayList)6 DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)5 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)5 DescribeTableResult (com.amazonaws.services.dynamodbv2.model.DescribeTableResult)5