AWS Databases
Different AWS databases
What are the different types of AWS Databases?
In order to meet the particular requirements of various use cases, AWS provides a variety of database services, each with its own special features and advantages.
Relational databases are among the most widely used AWS database types. Amazon Aurora, Amazon RDS, and Amazon Redshift are just a few of the relational databases that AWS offers. These databases are excellent for structured data and can be applied to financial applications, e-commerce platforms, and content management systems.
Non-relational databases, also known as NoSQL databases, are also provided by AWS. These databases can easily handle high data volumes and were made for unstructured data. Amazon DocumentDB and Amazon DynamoDB are two well-known NoSQL databases that AWS offers.
The graph database is another kind of database that AWS provides. Graph databases are made to store and manage highly connected data, like that found in social networks or recommendation systems. One example of a graph database provided by AWS is Amazon Neptune.
In-memory databases are available through AWS, they offer low-latency speed for applications that require quick data access. Amazon ElastiCache is an example of an in-memory database that can be used for apps like gaming.
import boto3
# Create a DynamoDB client
dynamodb = boto3.client('dynamodb')
# Create a table in DynamoDB
table_name = 'my-table'
key_schema = [{'AttributeName': 'id', 'KeyType': 'HASH'}]
attribute_definitions = [{'AttributeName': 'id', 'AttributeType': 'S'}]
provisioned_throughput = {'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5}
dynamodb.create_table(
TableName=table_name,
KeySchema=key_schema,
AttributeDefinitions=attribute_definitions,
ProvisionedThroughput=provisioned_throughput
)
# Create an item in DynamoDB
item = {'id': {'S': '123'}, 'name': {'S': 'John'}}
dynamodb.put_item(TableName=table_name, Item=item)
# Query items in DynamoDB
response = dynamodb.query(
TableName=table_name,
KeyConditionExpression='id = :id',
ExpressionAttributeValues={':id': {'S': '123'}}
)
items = response['Items']
# Create an RDS client
rds = boto3.client('rds')
# Create a database instance in RDS
db_instance_identifier = 'my-instance'
db_instance_class = 'db.t2.micro'
engine = 'mysql'
master_username = 'admin'
master_password = 'password'
allocated_storage = 20
rds.create_db_instance(
DBInstanceIdentifier=db_instance_identifier,
DBInstanceClass=db_instance_class,
Engine=engine,
MasterUsername=master_username,
MasterUserPassword=master_password,
AllocatedStorage=allocated_storage
)
# Connect to a database instance in RDS
import mysql.connector
cnx = mysql.connector.connect(
host='my-instance.cqnkj1234abc.us-east-1.rds.amazonaws.com',
user='admin',
password='password',
database='my-database'
)
# Create a table in MySQL
cursor = cnx.cursor()
create_table_query = (
"CREATE TABLE users ("
" id INT NOT NULL AUTO_INCREMENT,"
" name VARCHAR(50) NOT NULL,"
" PRIMARY KEY (id)"
") ENGINE=InnoDB"
)
cursor.execute(create_table_query)
# Insert data into a MySQL table
insert_query = "INSERT INTO users (name) VALUES (%s)"
data = ('John',)
cursor.execute(insert_query, data)
cnx.commit()
# Query data from a MySQL table
select_query = "SELECT * FROM users"
cursor.execute(select_query)
rows = cursor.fetchall()
# Close database connections
cursor.close()
cnx.close()
# test
Quiz
What is the main difference between relational and non-relational databases?
A. Relational databases are only used for structured data, while non-relational databases are only used for unstructured data.
B. Relational databases can easily handle high data volumes, while non-relational databases cannot.
C. Relational databases are based on tables and use SQL, while non-relational databases are based on collections and use JSON-like documents.
D. Relational databases are more expensive than non-relational databases.
Which AWS database service is best suited for applications that require low-latency speed?
A. Amazon ElastiCache
B. Amazon Neptune
C. Amazon DocumentDB
D. Amazon RDS
What is the purpose of the code example provided in the lesson?
A. To demonstrate how to create a table in Amazon Aurora.
B. To show how to query data from a DynamoDB table.
C. To provide an example of how to connect to a database instance in RDS using Python.
D. To showcase how to insert data into a MySQL table.
cac