AWS: DynamoDB
nagaraju juluru
Hadoop | Hive | Sqoop | PySpark| Spark Streaming | Kafka |AWS(S3,EMR,Ec2,Athena,Glue,Dynamo DB and Redshift) | Databricks | HBASE | Cassandra |Snowflake| Airflow|
DynamoDB is a serverless NoSQL distributed database which scales horizontally. And the data inside DynamoDB is stored in multiple AZs.?
DynamoDB guarantees single digit latency ( Only for good database designs though?). DynamoDB can scale to MILLIONS of requests per second.
DynamoDB Characteristics:
POM Dependency:
<dependency> <groupId></groupId> <artifactId>aws-sdk-factory</artifactId> <version>2.0.2.AWSNAT-LIBRARY-42</version> </dependency>
1) How data is stored in DynamoDB
Data is stored in partitions inside DynamoDB.?
1) In case of Primary Key as Partition/HASH Key:
2) In case of Primary Key as a combination of Partition/HASH and Sort/RANGE key:
2) Local Secondary Index and Global Secondary Index
Whenever we write to a table, the item is written to primary index. The changes are propagated to LSIs and GSIs of the table. We don't have to write to them explicitly.
We can query LSIs and GSIs directly!
Note:
3) Write Capacity Units and Read Capacity Units
We can configure our table to scale on demand. In that case our DynamoDB table can handle many number of requests without throttling. Otherwise, we can configure read and write capacity units explicitly and if the load goes beyond that measure. DynamoDB starts throttling and gives exception as response.
Documentation:?https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadWriteCapacityMode.html
4) Query Examples
Table Structure:
1) Primary Index:
ProfileIdConsumerId as Partition/HASH Key
InvoiceId as Sort/RANGE Key
2) Global Secondary Index (StatusIndex):
Status as Partition/HASH Key
CreatedDateInvoiceId as Sort/RANGE Key
ValueMap?→ {:st1=SETTLEMENT_PENDING, :ProfileIdConsumerId=DataCorrectionTestSAT4|VGI-US-DIGITAL_ADVICE_TEST} NameMap?→?{#st=Status} The above query spec will fetch all the invoices of?DataCorrectionTestSAT4|VGI-US-DIGITAL_ADVICE_TEST?this partitionKey having status?SETTLEMENT_PENDING.
QuerySpec Code:
QuerySpec querySpec = new QuerySpec() ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?.withKeyConditionExpression(String.valueOf(keyCondition)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?.withFilterExpression(filterExpression) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?.withValueMap(valueMap) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?.withNameMap(nameMap);
UpdateItemSpec Code:
UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey(profileIdConsumerIdColumn, profileIdConsumerId, invoiceIdColumn, invoiceId) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .withUpdateExpression(String.valueOf(updateExpression)) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .withNameMap(nameMap) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .withValueMap(valueMap) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .withReturnValues(ReturnValue.ALL_NEW);
Thanks for reading the article ..!