0% found this document useful (0 votes)
18 views10 pages

Exp2 - Computing Lab - BookReview - Rollno - 3

MTech

Uploaded by

Fahma Famzin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views10 pages

Exp2 - Computing Lab - BookReview - Rollno - 3

MTech

Uploaded by

Fahma Famzin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Computing Lab 2

Submitted by
Aswathy V G
Roll no:3

Experiment 3:Design Data Model using NoSQL Databases such as Hive/HBase/


Cassandra/ DynamoDB.

In this experiment data models are created using DynamoDB.

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.

Setting up DynamoDB local (downloadable version)

DynamoDB local is available as a download and it requires JRE 8 or newer versions.


We also have to download AWS Command Line Interface.

To install Dynamo DB locally on our computer do the following steps


1.Download DynamoDB for free from one of the given locations.

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

DynamoDB Management Console Running Instance Screenshot


4.Before you can access DynamoDB programmatically or through the AWS Command
Line Interface (AWS CLI), you must configure your credentials to enable authorization
for your applications. Downloadable DynamoDB requires any credentials to work, as
shown in the following example.
AWS Access Key ID: "fakeMyKeyId"
AWS Secret Access Key: "fakeSecretAccessKey"

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.

DynamoDB supports two kinds of indexes:

● 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.

Data Model for a sample book review website

In this experiment we are creating data model for a sample book review website.The
structure of the table is as follows

Table Name: BookReviews

● Partition Key: ReviewId (String)


● Sort Key: BookId (Number)
Attributes:

1. ReviewId (String): The unique identifier for each review.


2. BookId (Number): The identifier for the book being reviewed.
3. UserId (String): The identifier for the user who wrote the review.
4. Rating (Number): The rating given to the book (on a scale of 1 to 5, for example).
5. Comment (String): The text comment or review content.
6. Timestamp (Number or String): The timestamp indicating when the review was
created.

Global Secondary Index:

1. Index Name: BookReviewsByUser


● Partition Key: UserId
● Sort Key: ReviewId

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 AND OUTPUT FOR CREATING A TABLE USING DYNAMODB

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.

Output in JSON format


{
"TableDescription": {
"AttributeDefinitions": [
{
"AttributeName": "ReviewId",
"AttributeType": "S"
},
{
"AttributeName": "BookId",
"AttributeType": "N"
},
{
"AttributeName": "UserId",
"AttributeType": "S"
}
],
"TableName": "BookReviews",
"KeySchema": [
{
"AttributeName": "ReviewId",
"KeyType": "HASH"
},
{
"AttributeName": "BookId",
"KeyType": "RANGE"
}
],
"TableStatus": "ACTIVE",
"CreationDateTime": "2023-05-20T21:03:24.888000+05:30",
"ProvisionedThroughput": {
"LastIncreaseDateTime": "1970-01-01T05:30:00+05:30",
"LastDecreaseDateTime": "1970-01-01T05:30:00+05:30",
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
},
"TableSizeBytes": 0,
"ItemCount": 0,
"TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/BookReviews",
"GlobalSecondaryIndexes": [
{
"IndexName": "BookReviewsByUser",
"KeySchema": [
{
"AttributeName": "UserId",
"KeyType": "HASH"
},
{
"AttributeName": "ReviewId",
"KeyType": "RANGE"
}
],
"Projection": {
"ProjectionType": "INCLUDE",
"NonKeyAttributes": [
"ReviewId"
]
},
"IndexStatus": "ACTIVE",
"ProvisionedThroughput": {
"ReadCapacityUnits": 10,
"WriteCapacityUnits": 5
},
"IndexSizeBytes": 0,
"ItemCount": 0,
"IndexArn":
"arn:aws:dynamodb:ddblocal:000000000000:table/BookReviews/index/BookReviewsBy
User"
}
]
}
}

Screenshots
To add the remaining attributes the following is the code

C:\Users\aswat>aws dynamodb put-item ^


More? --table-name BookReviews ^
More? --item ^
More? "{\"ReviewId\": {\"S\": \"review123\"}, \"BookId\": {\"N\": \"12345\"}, \"UserId\":
{\"S\": \"user123\"}, \"Rating\": {\"N\": \"4\"}, \"Comment\": {\"S\": \"This book is
great!\"},\"Timestamp\": {\"N\": \"1621478400\"}}" --endpoint-url http://localhost:8000

Screenshot

View table after adding new items


C:\Users\aswat>aws dynamodb scan --table-name BookReviews --endpoint-url
http://localhost:8000
{
"Items": [
{
"Comment": {
"S": "This book is great!"
},
"ReviewId": {
"S": "review123"
},
"BookId": {
"N": "12345"
},
"Timestamp": {
"N": "1621478400"
},
"UserId": {
"S": "user123"
},
"Rating": {
"N": "4"
}
}
],
"Count": 1,
"ScannedCount": 1,
"ConsumedCapacity": null
}
Screenshot

You might also like