Exp2 - Computing Lab - BookReview - Rollno - 3
Exp2 - Computing Lab - BookReview - Rollno - 3
Submitted by
Aswathy V G
Roll no:3
Amazon DynamoDB
Amazon DynamoDB is a fully managed NoSQL database service that provides fast and
predictable performance with seamless scalability. DynamoDB lets you offload the
administrative burdens of operating and scaling a distributed database so that you don't
have to worry about hardware provisioning, setup and configuration, replication,
software patching, or cluster scaling. DynamoDB also offers encryption at rest, which
eliminates the operational burden and complexity involved in protecting sensitive data.
With DynamoDB, you can create database tables that can store and retrieve any
amount of data and serve any level of request traffic. You can scale up or scale down
your tables' throughput capacity without downtime or performance degradation.
2.After you download the archive, extract the contents and copy the extracted directory
to a location of your choice.
3.To start DynamoDB on your computer, open a command prompt window, navigate to
the directory where you extracted DynamoDBLocal.jar, and enter the following
command.
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
5.Now we can start writing applications. To access DynamoDB running locally with
the AWS CLI, use the --endpoint-url parameter
Data Modelling
With a NoSQL database such as DynamoDB, the join functionality does not exist, we're
invited to put all our entities in the same table. This is the concept of Single-Table
Design. By removing joins, we write more efficient queries, which reduces the needs of
computing power. However, this comes at a cost: the data modeling exercise is slightly
more complicated. The entity diagram does not give the model anymore. The ways your
application will interact with data - the access patterns - are now a key building block of
the modelisation.
Once we have defined all of your access patterns, you can start the modelisation. The
primary key is a core concept in DynamoDB, and almost the single piece of structure
imposed on the table. It is thus at the core of the modeling exercise.
DynamoDB supports two different kinds of primary keys:
1.Partition key – A simple primary key, composed of one attribute known as the
partition key.
DynamoDB uses the partition key's value as input to an internal hash function. The
output from the hash function determines the partition (physical storage internal to
DynamoDB) in which the item will be stored.
In a table that has only a partition key, no two items can have the same partition key
value.
2.Partition key and sort key – Referred to as a composite primary key, this type of key
is composed of two attributes. The first attribute is the partition key, and the second
attribute is the sort key.
DynamoDB uses the partition key value as input to an internal hash function. The output
from the hash function determines the partition (physical storage internal to DynamoDB)
in which the item will be stored. All items with the same partition key value are stored
together, in sorted order by sort key value.
Secondary indexes
We can create one or more secondary indexes on a table. A secondary index lets you
query the data in the table using an alternate key, in addition to queries against the
primary key. DynamoDB doesn't require that you use indexes, but they give your
applications more flexibility when querying your data. After you create a secondary
index on a table, you can read data from the index in much the same way as you do
from the table.
● Global secondary index – An index with a partition key and sort key that can be
different from those on the table.
● Local secondary index – An index that has the same partition key as the table,
but a different sort key.
Each table in DynamoDB has a quota of 20 global secondary indexes (default quota)
and 5 local secondary indexes.
In this experiment we are creating data model for a sample book review website.The
structure of the table is as follows
With this model, we can perform queries such as retrieving all reviews for a specific
book, retrieving all reviews by a particular user, or retrieving a specific review by its
ReviewId.
We added a global secondary index named "BookReviewsByUser" to support querying
reviews by users. The index has a composite key schema with UserId as the partition
key and ReviewId as the sort key.
Code
C:\Users\aswat>aws dynamodb create-table ^
More? --table-name BookReviews ^
More? --attribute-definitions ^
More? AttributeName=ReviewId,AttributeType=S ^
More? AttributeName=BookId,AttributeType=N ^
More? AttributeName=UserId,AttributeType=S ^
More? --key-schema ^
More? AttributeName=ReviewId,KeyType=HASH ^
More? AttributeName=BookId,KeyType=RANGE ^
More? --provisioned-throughput ^
More? ReadCapacityUnits=5,WriteCapacityUnits=5 ^
More? --global-secondary-indexes ^
More? "[{\"IndexName\": \"BookReviewsByUser\",\"KeySchema\":
[{\"AttributeName\":\"UserId\",\"KeyType\":\"HASH\"},{\"AttributeName\":\"ReviewId\",\"Ke
yType\":\"RANGE\"}],
\"Projection\":{\"ProjectionType\":\"INCLUDE\",\"NonKeyAttributes\":[\"ReviewId\"]},\"Prov
isionedThroughput\":{\"ReadCapacityUnits\": 10,\"WriteCapacityUnits\": 5}}]"
--table-class STANDARD --endpoint-url http://localhost:8000
This command creates a table named BookReviews in DynamoDB for a sample book
review website.
The --attribute-definitions option specifies the attribute names and their respective
types. In this example, ReviewId is a string attribute (S), BookId is a numeric attribute
(N), and UserId is a string attribute (S).
The --key-schema option defines the primary key structure of the table. In this example,
ReviewId is the partition key (HASH), and BookId is the sort key (RANGE).
The --provisioned-throughput option specifies the read and write capacity units for the
table. In this example, both read and write capacity are set to 5 units.
The --global-secondary-indexes option is used to create a global secondary index
named UserReviews. This index allows querying reviews by user. The index has a
composite key schema with UserId as the partition key and ReviewId as the sort key.
Screenshots
To add the remaining attributes the following is the code
Screenshot