DynamoDB Database Cheatsheet

DynamoDB is a fully managed NoSQL database service provided by Amazon Web Services (AWS). It’s known for its seamless scalability, low-latency performance, and flexibility. Whether you’re a beginner or an experienced developer, having a cheatsheet can be invaluable for quickly referencing essential commands and operations. In this cheatsheet, we’ll cover some of the fundamental DynamoDB concepts and commands.

1. Table Operations

Creating a Table

import boto3

dynamodb = boto3.resource('dynamodb')
table = dynamodb.create_table(
    TableName='YourTableName',
    KeySchema=[
        {'AttributeName': 'PartitionKey', 'KeyType': 'HASH'},
        {'AttributeName': 'SortKey', 'KeyType': 'RANGE'}
    ],
    AttributeDefinitions=[
        {'AttributeName': 'PartitionKey', 'AttributeType': 'S'},
        {'AttributeName': 'SortKey', 'AttributeType': 'N'}
    ],
    ProvisionedThroughput={
        'ReadCapacityUnits': 5,
        'WriteCapacityUnits': 5
    }
)
table.wait_until_exists()

Deleting a Table

table = dynamodb.Table('YourTableName')
table.delete()
table.wait_until_not_exists()

2. Item Operations

Putting an Item

table.put_item(
    Item={
        'PartitionKey': 'Value1',
        'SortKey': 123,
        'Attribute1': 'AttributeValue1',
        'Attribute2': 'AttributeValue2'
    }
)

Getting an Item

response = table.get_item(
    Key={
        'PartitionKey': 'Value1',
        'SortKey': 123
    }
)
item = response.get('Item', {})

Updating an Item

table.update_item(
    Key={
        'PartitionKey': 'Value1',
        'SortKey': 123
    },
    UpdateExpression='SET Attribute1 = :value1',
    ExpressionAttributeValues={
        ':value1': 'NewValue1'
    }
)

Deleting an Item

table.delete_item(
    Key={
        'PartitionKey': 'Value1',
        'SortKey': 123
    }
)

3. Query and Scan Operations

Querying Items

response = table.query(
    KeyConditionExpression=Key('PartitionKey').eq('Value1')
)
items = response.get('Items', [])

Scanning Items

response = table.scan(
    FilterExpression=Attr('Attribute1').begins_with('Prefix')
)
items = response.get('Items', [])

4. Index Operations

Creating an Index

table.update(
    AttributeDefinitions=[
        {'AttributeName': 'NewAttribute', 'AttributeType': 'S'}
    ],
    GlobalSecondaryIndexUpdates=[
        {
            'Create': {
                'IndexName': 'NewIndex',
                'KeySchema': [
                    {'AttributeName': 'NewAttribute', 'KeyType': 'HASH'}
                ],
                'Projection': {'ProjectionType': 'ALL'},
                'ProvisionedThroughput': {
                    'ReadCapacityUnits': 5,
                    'WriteCapacityUnits': 5
                }
            }
        }
    ]
)

Querying Using an Index

response = table.query(
    IndexName='NewIndex',
    KeyConditionExpression=Key('NewAttribute').eq('Value1')
)
items = response.get('Items', [])

This DynamoDB cheatsheet provides a quick reference for essential operations, allowing you to work efficiently with DynamoDB in your applications. Whether you’re creating tables, manipulating items, or querying with indexes, these snippets should serve as a handy guide for your DynamoDB development journey. Remember to adapt these examples to your specific use case and always refer to the official DynamoDB documentation for comprehensive details.

FAQ

1. What is DynamoDB?

DynamoDB is a fully managed NoSQL database service provided by AWS. It differs from traditional databases by offering seamless scalability, low-latency performance, and a flexible schema, making it well-suited for applications with unpredictable workloads.

2. How do I provision throughput in DynamoDB?

DynamoDB allows you to provision read and write capacity units to handle your application’s traffic. You can set these values when creating a table and adjust them later based on your application’s needs.

3. What are the key components of a DynamoDB table?

A DynamoDB table consists of a primary key, which can be either a single attribute or a combination of two attributes (partition key and sort key). Attribute definitions, secondary indexes, and provisioned throughput are also important components.

4. How does querying work in DynamoDB?

Querying in DynamoDB involves searching for items based on their primary key attributes. A query is more efficient than a scan, as it retrieves specific items using indexed attributes. A scan, on the other hand, reads all items in a table and is less efficient for large datasets.

5. Can I use DynamoDB with other AWS services?

Yes, DynamoDB seamlessly integrates with other AWS services. It can be used in conjunction with AWS Lambda for serverless computing, Amazon S3 for storing and retrieving large objects, and Amazon CloudWatch for monitoring and logging, among others. This integration enhances the overall capabilities of your AWS-powered applications