Back Home

Amazon DynamoDB is a proprietary Nosql Database

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

AWS CLI

Describe a table

 
aws dynamodb describe-table --table-name prod-catalog-v2 --region eu-west-1

and scan

aws dynamodb scan --table-name prod-catalog-v2 --region eu-west-1

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());
        }
    }

Streams

Table events can be streamed. Those events are bundled into 'shards'

  • ListStreams - returns a list of stream descriptors for the current account and endpoint. You can optionally request just the stream descriptors for a particular table name.
  • DescribeStream - returns detailed information about a given stream. The output includes a list of shards associated with the stream, including the shard IDs.
  • GetShardIterator - returns a shard iterator, which describes a location within a shard. You can request that the iterator provide access to the oldest point, the newest point, or a particular point in the stream.
  • GetRecords - returns the stream records from within a given shard. You must provide the shard iterator returned from a GetShardIterator request.

Note also the https://github.com/awslabs/dynamodb-streams-kinesis-adapter which says “Using the Kinesis Adapter is the recommended way to consume Streams from DynamoDB.”

An example of a kinesis stream listener https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.KCLAdapter.Walkthrough.CompleteProgram.html

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

Running and Embedded Dynamodb

  FileUtils.copyFile(new File("libs/libsqlite4java-linux-amd64.so"), new File(System.getProperty("java.io.tmpdir") + "/libsqlite4java-linux-amd64.so"));
        System.load(System.getProperty("java.io.tmpdir") + "/libsqlite4java-linux-amd64.so");
 
        // now create client and table
        AmazonDynamoDBLocal amazonDynamoDBLocal = DynamoDBEmbedded.create();
 
        dbLocal = amazonDynamoDBLocal.amazonDynamoDB();
        db = new DynamoDB(dbLocal);
        CreateTableResult result = dbLocal.createTable(new CreateTableRequest().withTableName("catalog-v2")
                .withKeySchema(new KeySchemaElement().withAttributeName("id").withKeyType(KeyType.HASH))
                .withAttributeDefinitions(new AttributeDefinition().withAttributeName("id").withAttributeType(ScalarAttributeType.S))
                .withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L))
        );
 
        Table table = db.getTable("catalog-v2");
        File test = new File("test");
                System.err.println("local path " + test.getAbsolutePath());
        Resource resouce = resourceLoader.getResource("classpath:testData/catalog-v2.json");
 
 
 
        String data = FileUtils.readFileToString( resouce.getFile());
        JSONObject jsonData = new JSONObject(data);
 
        // put json direct
        table.putItem(Item.fromJSON(data));
        Item item = table.getItem(new PrimaryKey("id", "516037-289558-289559"));

Testing with Spring

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
@ActiveProfiles("local")
@TestPropertySource(properties = { 
  "amazon.dynamodb.endpoint=http://localhost:8000/", 
  "amazon.aws.accesskey=test1", 
  "amazon.aws.secretkey=test231" })
public class ProductInfoRepositoryIntegrationTest {
 
    private DynamoDBMapper dynamoDBMapper;
 
    @Autowired
    private AmazonDynamoDB amazonDynamoDB;
 
amazon_dynamodb.txt · Last modified: 2018/06/11 05:48 by root
 
RSS - 200 © CrosswireDigitialMedia Ltd