DynamoDb
Presentation
Amazon DynamoDB is a fully managed, cloud-hosted, NoSQL database.
It provides fast and predictable performance with the ability to scale seamlessly.
It allows you to store and retrieve any amount of data, serving any level of network traffic without having any operational burden. DynamoDB gives numerous other advantages like consistent and predictable performance, flexible data modeling, and durability.
DynamoDB uses Solid State Disks (SSD) to store the data
It also automatically replicates the data across other AWS Availability Zones, which provides built-in high availability and reliability
Document Store: MongoDB, CouchDB, MarkLogic
Column Store: Hbase, Cassandra
Key Value Store: DynamoDB, Azure, Redis
Graph Databases: Neo4J, DEX
Tables, Items, and Attributes.
It needs only the fixed primary key, its data type, and a secondary index if needed
Operations
The Query and Scan operations are used to retrieve information from tables. The Query operation allows us to query the given table with provided hash key and range key. We can also query tables for secondary indexes.
The Scan operation reads all items from a given table.
Primary key
DynamoDB, being a key-value pair database, does not index on all given attributes for a given item;
it only indexes on the primary key, which is a mandatory attribute for each item of the table.
having emp_id as the hash key and email as the range key, Mandatory
s the primary key, which needs to be unique for each attribute.
DynamoDB supports two types of primary keys:
- Hash primary key : Each DynamoDB table must have a hash primary key that is unique for each item.DynamoDB builds an unordered hash index on this key that allows us to uniquely identify any given item for a given table. So choose an attribute that would have a unique value for each attribute.
- Hash and range primary key : Along with the hash key, DynamoDB also supports another primary key called range key, which can be used in combination with the hash key In this case, DynamoDB created an unordered hash index on the hash key and sorted the range key index on a range key attribute Range key should be selected in such a manner that it will evenly distribute the data load across partitions A good example to choose a hash and range type primary key for the Person table would be choosing birth years as the hash key and SSN as the range key. Here, we can narrow down our search with the birth year and then search in a specific birth year range partition for a specific SSN
Secondary indexes
sometimes there might be a need to search and access items from attributes that are not part of the primary key. For all such needs, DynamoDB supports secondary indexes that can be created on attributes other than the primary key and can be accessed and searched in a manner similar to the primary key.
We can create multiple secondary indexes for a given table. If we don’t create secondary indexes, the only option to get the item for a certain non-primary key attribute is to scan the complete table, which is a very expensive operation.
DynamoDB supports two types of secondary indexes:
- Local secondary index
Global secondary index
Basically, local secondary indexes give you more range query options other than your table range key attribute. So to define the local secondary index, we can say that it is an index which has the same hash key as a table but a different range key.
aws dynamodb delete-table –table-name messages
findById
Forgot password => findByEmail
Login => findByEmail
Authenticate => findByIdEx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32usersTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: Users
AttributeDefinitions:
- AttributeName: id
AttributeType: S
- AttributeName: email
AttributeType: S
- AttributeName: password
AttributeType: S
- AttributeName: salt
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
GlobalSecondaryIndexes:
- IndexName: UserAuthIndex
KeySchema:
- AttributeName: email
KeyType: HASH
Projection:
NonKeyAttributes:
- user_id
- user_organization_id
ProjectionType: INCLUDE
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
1 | var params = { |
1 | var params = { |
Ref
- http://www.markomedia.com.au/dynamodb-for-javascript-cheatsheet/
- http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.03.html
- https://egkatzioura.com/2016/06/23/create-dynamodb-tables-with-node-js/
- https://egkatzioura.com/2016/07/02/query-dynamodb-items-with-node-js/