Back Home

Amazon DynamoDB is a proprietary Nosql Database

Item's have key's for search DynamoDB supports two different kinds of primary keys:

Create a Table

Tables are created with a range of attributes:

S - the attribute is of type String
N - the attribute is of type Number
B - the attribute is of type Binary

In code we construct a range of attributes passed to create the table:

 // Key attribute definition
        ArrayList<AttributeDefinition> attrDefList= new ArrayList<AttributeDefinition>();
        attrDefList.add(new AttributeDefinition().withAttributeName("bookId").withAttributeType("N"));
        attrDefList.add(new AttributeDefinition().withAttributeName("yop").withAttributeType("S"));

these attributes can be used to be 'indexed' as a primary key

   ArrayList<KeySchemaElement> ks = new ArrayList<KeySchemaElement>();
        ks.add(new KeySchemaElement("bookId", KeyType.HASH));
        ks.add(new KeySchemaElement("yop",KeyType.RANGE));

And then create the table:

 CreateTableRequest request = new CreateTableRequest()
                    .withTableName("book")
                    .withAttributeDefinitions(attrDefList)
                    .withKeySchema(ks)
                    .withProvisionedThroughput(provisionedThroughput);        

The bit I skipped is the “Provisioned Throughput”

  ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput()
                        .withReadCapacityUnits(20L)
                        .withWriteCapacityUnits(20L);

Crud

 private static void updateAddNewAttribute() {
        Table table = dynamoDB.getTable(tableName);
 
        try {
 
            UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("Id", 121)
                .withUpdateExpression("set #na = :val1").withNameMap(new NameMap().with("#na", "NewAttribute"))
                .withValueMap(new ValueMap().withString(":val1", "Some value")).withReturnValues(ReturnValue.ALL_NEW);
 
            UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
 
            // Check the response.
            System.out.println("Printing item after adding new attribute...");
            System.out.println(outcome.getItem().toJSONPretty());
 
        }
        catch (Exception e) {
            System.err.println("Failed to add new attribute in " + tableName);
            System.err.println(e.getMessage());
        }
    }

Running Local Version

wget https://s3.eu-central-1.amazonaws.com/dynamodb-local-frankfurt/dynamodb_local_latest.zip

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar --port 8001
 
amazon_dynamodb.txt · Last modified: 2017/09/05 07:18 by root
 
RSS - 200 © CrosswireDigitialMedia Ltd