ebook img

Amazon DynamoDB - Developer Guide PDF

959 Pages·2017·9.18 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Amazon DynamoDB - Developer Guide

Amazon DynamoDB Developer Guide API Version 2012-08-10 Amazon DynamoDB Developer Guide Amazon DynamoDB: Developer Guide Copyright © 2022 Amazon Web Services, Inc. and/or its affiliates. All rights reserved. Amazon's trademarks and trade dress may not be used in connection with any product or service that is not Amazon's, in any manner that is likely to cause confusion among customers, or in any manner that disparages or discredits Amazon. All other trademarks not owned by Amazon are the property of their respective owners, who may or may not be affiliated with, connected to, or sponsored by Amazon. Amazon DynamoDB Developer Guide Table of Contents What is Amazon DynamoDB? ............................................................................................................... 1 High availability and durability .................................................................................................... 1 Getting started with DynamoDB.................................................................................................. 1 DynamoDB tutorials................................................................................................................... 2 How it works............................................................................................................................. 2 Cheat sheet....................................................................................................................... 3 Core components............................................................................................................... 6 DynamoDB API................................................................................................................ 12 Supported data types and naming rules .............................................................................. 15 Read consistency.............................................................................................................. 19 Read/write capacity mode ................................................................................................. 20 Table classes.................................................................................................................... 24 Partitions and data distribution .......................................................................................... 25 From SQL to NoSQL ................................................................................................................. 27 Relational or NoSQL? ........................................................................................................ 28 Characteristics of databases ............................................................................................... 29 Creating a table ............................................................................................................... 31 Getting information about a table ...................................................................................... 33 Writing data to a table ..................................................................................................... 34 Reading data from a table ................................................................................................. 37 Managing indexes............................................................................................................. 43 Modifying data in a table .................................................................................................. 46 Deleting data from a table ................................................................................................ 49 Removing a table ............................................................................................................. 50 Additional Amazon DynamoDB resources .................................................................................... 51 Blog posts, repositories, and guides .................................................................................... 51 Data modeling and design pattern presentations ................................................................. 52 Training courses............................................................................................................... 52 Tools for coding and visualization ...................................................................................... 52 Setting up DynamoDB...................................................................................................................... 53 Setting up DynamoDB local (downloadable version) ..................................................................... 53 Deploying ........................................................................................................................ 53 Usage notes..................................................................................................................... 57 Setting up DynamoDB (web service) ........................................................................................... 60 Signing up for AWS .......................................................................................................... 60 Getting an AWS access key ................................................................................................ 60 Configuring your credentials .............................................................................................. 61 Accessing DynamoDB........................................................................................................................ 62 Using the console ..................................................................................................................... 62 Using the AWS CLI ................................................................................................................... 63 Downloading and configuring the AWS CLI .......................................................................... 63 Using the AWS CLI with DynamoDB .................................................................................... 63 Using the AWS CLI with downloadable DynamoDB ............................................................... 64 Using the API.......................................................................................................................... 65 Using the NoSQL workbench ..................................................................................................... 65 IP address ranges..................................................................................................................... 65 Getting started with DynamoDB........................................................................................................ 66 Basic concepts......................................................................................................................... 66 Prerequisites............................................................................................................................ 66 Step 1: Create a table ............................................................................................................... 66 Step 2: Write data .................................................................................................................... 70 Step 3: Read data.................................................................................................................... 74 Step 4: Update data................................................................................................................. 76 Step 5: Query data ................................................................................................................... 78 API Version 2012-08-10 iii Amazon DynamoDB Developer Guide Step 6: Create a global secondary index ...................................................................................... 81 Step 7: Query the global secondary index ................................................................................... 84 Step 8: (Optional) clean up........................................................................................................ 87 Next steps............................................................................................................................... 87 Getting started with DynamoDB and the AWS SDKs ............................................................................. 88 Create a table.......................................................................................................................... 88 Create a DynamoDB table using an AWS SDK ...................................................................... 88 Write an item.......................................................................................................................... 99 Write an item to a DynamoDB table using an AWS SDK ......................................................... 99 Read an item ......................................................................................................................... 109 Read an item from a DynamoDB table using an AWS SDK .................................................... 109 Update an item...................................................................................................................... 118 Update an item in a DynamoDB table using an AWS SDK ..................................................... 118 Delete an item....................................................................................................................... 128 Delete an item in a DynamoDB table using an AWS SDK ...................................................... 128 Query a table ......................................................................................................................... 138 Query a DynamoDB table using an AWS SDK ..................................................................... 138 Scan a table.......................................................................................................................... 150 Scan a DynamoDB table using an AWS SDK ....................................................................... 138 Working with AWS SDKs ......................................................................................................... 159 Programming with DynamoDB......................................................................................................... 161 Overview of AWS SDK support for DynamoDB ........................................................................... 161 Programmatic interfaces.......................................................................................................... 163 Low-level interfaces........................................................................................................ 164 Document interfaces....................................................................................................... 165 Object persistence interface ............................................................................................. 165 Low-level API......................................................................................................................... 167 Request format.............................................................................................................. 169 Response format............................................................................................................ 169 Data type descriptors ...................................................................................................... 170 Numeric data................................................................................................................. 171 Binary data.................................................................................................................... 171 Error handling........................................................................................................................ 171 Error components........................................................................................................... 171 Transactional errors........................................................................................................ 172 Error messages and codes ............................................................................................... 172 Error handling in your application .................................................................................... 175 Error retries and exponential backoff ................................................................................ 176 Batch operations and error handling ................................................................................. 176 Higher-level programming interfaces ........................................................................................ 177 Java: DynamoDBMapper.................................................................................................. 178 .NET: Document model ................................................................................................... 225 .NET: Object persistence model ........................................................................................ 248 Running the code examples ..................................................................................................... 276 Load sample data........................................................................................................... 277 Java code examples ........................................................................................................ 282 .NET code examples ........................................................................................................ 284 Working with DynamoDB................................................................................................................ 287 Working with Tables ............................................................................................................... 287 Basic Operations on Tables .............................................................................................. 287 Considerations when changing read/write Capacity Mode .................................................... 293 Considerations when choosing a table class....................................................................... 293 Provisioned Capacity Tables ............................................................................................. 294 Item Sizes and Formats ................................................................................................... 298 Managing throughput capacity with auto scaling ................................................................ 299 Working with Global Tables ............................................................................................. 313 Tagging resources........................................................................................................... 337 API Version 2012-08-10 iv Amazon DynamoDB Developer Guide Working with tables: Java ............................................................................................... 340 Working with tables: .NET ................................................................................................ 345 Working with read and write operations .................................................................................... 352 DynamoDB API............................................................................................................... 352 PartiQL query language .................................................................................................. 474 Working with indexes .............................................................................................................. 505 Global secondary indexes ................................................................................................ 508 Local Secondary Indexes .................................................................................................. 548 Working with transactions ....................................................................................................... 586 How it works ................................................................................................................. 586 Using IAM with transactions ............................................................................................ 592 Example code................................................................................................................. 594 Working with streams ............................................................................................................. 597 Options......................................................................................................................... 597 Working with Kinesis Data Streams ................................................................................... 598 Working with DynamoDB Streams .................................................................................... 607 Working with On-Demand backup and restore ........................................................................... 648 Using AWS Backup ......................................................................................................... 648 Using DynamoDB backups............................................................................................... 655 Working with point-in-time recovery ......................................................................................... 668 How it works ................................................................................................................. 668 Before you begin ............................................................................................................ 670 Restoring a table to a point in time .................................................................................. 670 In-memory acceleration with DAX ..................................................................................................... 675 Use cases for DAX .................................................................................................................. 676 DAX usage notes .................................................................................................................... 676 How it works ......................................................................................................................... 677 How DAX processes requests ............................................................................................ 678 Item cache..................................................................................................................... 679 Query cache................................................................................................................... 680 DAX cluster components ......................................................................................................... 680 Nodes........................................................................................................................... 680 Clusters......................................................................................................................... 681 Regions and availability zones .......................................................................................... 681 Parameter groups........................................................................................................... 682 Security groups.............................................................................................................. 682 Cluster ARN................................................................................................................... 682 Cluster endpoint............................................................................................................. 682 Node endpoints.............................................................................................................. 683 Subnet groups............................................................................................................... 683 Events........................................................................................................................... 683 Maintenance window...................................................................................................... 683 Creating a DAX cluster ............................................................................................................ 684 Creating an IAM service role for DAX to access DynamoDB ................................................... 684 Using the AWS CLI .......................................................................................................... 686 Using the console ........................................................................................................... 690 Consistency models................................................................................................................ 692 Consistency among DAX cluster nodes .............................................................................. 693 DAX item cache behavior ................................................................................................. 693 DAX query cache behavior ............................................................................................... 695 Strongly consistent and transactional reads ....................................................................... 696 Negative caching ............................................................................................................ 696 Strategies for writes....................................................................................................... 696 Developing with the DAX client ................................................................................................ 699 Tutorial: Running a sample application .............................................................................. 699 Modifying an existing application to use DAX ..................................................................... 731 Managing DAX clusters ............................................................................................................ 732 API Version 2012-08-10 v Amazon DynamoDB Developer Guide IAM permissions for managing a DAX cluster ..................................................................... 732 Scaling a DAX cluster ...................................................................................................... 734 Customizing DAX cluster settings ..................................................................................... 735 Configuring TTL settings ................................................................................................. 735 Tagging support for DAX ................................................................................................. 736 AWS CloudTrail integration .............................................................................................. 737 Deleting a DAX cluster .................................................................................................... 737 Monitoring DAX...................................................................................................................... 738 Monitoring tools............................................................................................................. 738 Monitoring with CloudWatch ............................................................................................ 739 Logging DAX operations using AWS CloudTrail ................................................................... 753 DAX T3/T2 burstable instances ................................................................................................ 753 DAX T2 instance family ................................................................................................... 753 DAX T3 instance family ................................................................................................... 753 DAX access control ................................................................................................................. 754 IAM service role for DAX ................................................................................................. 754 IAM policy to allow DAX cluster access .............................................................................. 755 Case study: Accessing DynamoDB and DAX ........................................................................ 756 Access to DynamoDB, but no access with DAX .................................................................... 757 Access to DynamoDB and to DAX ..................................................................................... 759 Access to DynamoDB via DAX, but no direct access to DynamoDB ......................................... 762 DAX encryption at rest ............................................................................................................ 764 Enabling encryption at rest using the AWS Management Console .......................................... 765 DAX encryption in transit ........................................................................................................ 766 Using service-linked roles for DAX ............................................................................................ 767 Service-linked role permissions for DAX ............................................................................. 767 Creating a service-linked role for DAX ............................................................................... 768 Editing a service-linked role for DAX ................................................................................. 768 Deleting a service-linked role for DAX ............................................................................... 768 Accessing DAX across AWS accounts ......................................................................................... 769 Set up IAM.................................................................................................................... 770 Set up a VPC................................................................................................................. 771 Modify the DAX client to allow cross-account access ........................................................... 773 DAX cluster sizing guide .......................................................................................................... 776 Overview....................................................................................................................... 776 Estimating traffic............................................................................................................ 777 Load testing.................................................................................................................. 777 API reference......................................................................................................................... 778 NoSQL Workbench.......................................................................................................................... 779 Download.............................................................................................................................. 779 Install.................................................................................................................................... 780 Data modeler......................................................................................................................... 783 Creating a new model ..................................................................................................... 783 Importing an existing model ............................................................................................ 791 Exporting a model .......................................................................................................... 794 Editing an existing model ................................................................................................ 796 Data visualizer........................................................................................................................ 798 Adding sample data ........................................................................................................ 799 Importing from CSV ........................................................................................................ 800 Facets........................................................................................................................... 801 Aggregate view.............................................................................................................. 805 Committing a data model ................................................................................................ 806 Operation builder................................................................................................................... 811 Connecting to datasets ................................................................................................... 811 Building operations......................................................................................................... 815 Exporting to CSV ............................................................................................................ 832 Sample data models............................................................................................................... 833 API Version 2012-08-10 vi Amazon DynamoDB Developer Guide Employee data model ..................................................................................................... 833 Discussion forum data model.......................................................................................... 833 Music library data model ................................................................................................. 834 Ski resort data model ..................................................................................................... 834 Credit card offers data model .......................................................................................... 834 Bookmarks data model................................................................................................... 835 Release history....................................................................................................................... 835 Code examples............................................................................................................................... 837 Actions.................................................................................................................................. 838 Create a table ................................................................................................................ 838 Delete a table................................................................................................................ 849 Delete an item from a table ............................................................................................ 855 Get a batch of items....................................................................................................... 864 Get an item from a table ................................................................................................ 871 Get information about a table......................................................................................... 880 List tables...................................................................................................................... 886 Put an item in a table..................................................................................................... 892 Query a table ................................................................................................................. 902 Run a PartiQL statement ................................................................................................. 914 Run batches of PartiQL statements ................................................................................... 927 Scan a table.................................................................................................................. 943 Update an item in a table............................................................................................... 952 Write a batch of items .................................................................................................... 963 Scenarios............................................................................................................................... 974 Accelerate reads with DAX ............................................................................................... 974 Get started using tables, items, and queries ....................................................................... 979 Query a table by using batches of PartiQL statements ....................................................... 1047 Query a table using PartiQL........................................................................................... 1088 Use a document model................................................................................................. 1123 Use a high-level object persistence model ........................................................................ 1133 Cross-service examples.......................................................................................................... 1139 Build an app to submit data to a DynamoDB table............................................................ 1140 Create a REST API to track COVID-19 data ....................................................................... 1141 Create a messenger application ...................................................................................... 1142 Create a web application to track DynamoDB data ............................................................ 1142 Create a websocket chat application ............................................................................... 1144 Detect PPE in images.................................................................................................... 1144 Invoke a Lambda function from a browser ....................................................................... 1145 Save EXIF and other image information........................................................................... 1146 Use API Gateway to invoke a Lambda function ................................................................. 1147 Use Step Functions to invoke Lambda functions ............................................................... 1148 Use scheduled events to invoke a Lambda function ........................................................... 1149 Security....................................................................................................................................... 1150 Data protection.................................................................................................................... 1150 Encryption at rest ......................................................................................................... 1151 Data protection in DAX ................................................................................................. 1167 Internetwork traffic privacy............................................................................................ 1167 Identity and access management ............................................................................................ 1168 Identity and access management.................................................................................... 1169 Identity and access management in DAX ......................................................................... 1198 Compliance validation........................................................................................................... 1198 Resilience............................................................................................................................. 1199 Infrastructure security........................................................................................................... 1199 Using VPC endpoints..................................................................................................... 1200 Configuration and vulnerability analysis .................................................................................. 1206 Security best practices ........................................................................................................... 1206 Preventative security best practices................................................................................. 1206 API Version 2012-08-10 vii Amazon DynamoDB Developer Guide Detective security best practices ..................................................................................... 1208 Monitoring................................................................................................................................... 1210 Logging and monitoring in DynamoDB.................................................................................... 1210 Monitoring tools........................................................................................................... 1211 Monitoring with Amazon CloudWatch............................................................................. 1212 Logging DynamoDB operations by using AWS CloudTrail ................................................... 1231 Logging and monitoring in DAX ............................................................................................. 1247 Contributor Insights.............................................................................................................. 1247 How it works................................................................................................................ 1247 Getting started............................................................................................................. 1251 Using IAM.................................................................................................................... 1255 Best practices............................................................................................................................... 1260 NoSQL design...................................................................................................................... 1262 NoSQL vs. RDBMS ......................................................................................................... 1262 Two key concepts......................................................................................................... 1263 General approach......................................................................................................... 1263 Partition key design .............................................................................................................. 1264 Burst capacity.............................................................................................................. 1264 Adaptive capacity......................................................................................................... 1264 Distributing workloads.................................................................................................. 1266 Write sharding .............................................................................................................. 1266 Uploading data efficiently.............................................................................................. 1267 Sort key design .................................................................................................................... 1268 Version control............................................................................................................. 1269 Secondary indexes................................................................................................................ 1270 General guidelines........................................................................................................ 1270 Sparse indexes............................................................................................................. 1272 Aggregation................................................................................................................. 1273 GSI overloading............................................................................................................ 1274 GSI sharding................................................................................................................ 1275 Creating a replica ......................................................................................................... 1276 Large items.......................................................................................................................... 1277 Compression................................................................................................................ 1277 Using Amazon S3......................................................................................................... 1277 Time series data................................................................................................................... 1278 Design pattern for time series data ................................................................................. 1278 Time series table examples............................................................................................ 1278 Many-to-many relationships................................................................................................... 1279 Adjacency lists.............................................................................................................. 1279 Materialized graphs....................................................................................................... 1280 Hybrid DynamoDB–RDBMS..................................................................................................... 1283 Not migrating.............................................................................................................. 1283 Hybrid system implementation....................................................................................... 1284 Relational modeling .............................................................................................................. 1284 First steps.................................................................................................................... 1286 Example...................................................................................................................... 1287 Querying and scanning .......................................................................................................... 1290 Scan performance......................................................................................................... 1290 Avoid spikes................................................................................................................. 1290 Parallel scans............................................................................................................... 1293 Cost optimization................................................................................................................. 1294 Evaluate your costs at the table level .............................................................................. 1294 Evaluate your table capacity mode ................................................................................. 1295 Evaluate your table's auto scaling settings ....................................................................... 1299 Evaluate your table class selection .................................................................................. 1305 Identifying your unused resources ................................................................................... 1306 Evaluate your table usage patterns ................................................................................. 1312 API Version 2012-08-10 viii Amazon DynamoDB Developer Guide Evaluate your Streams usage......................................................................................... 1314 Evaluate your provisioned capacity for right-sized provisioning ........................................... 1317 Integrating with other AWS services ................................................................................................ 1327 Integrating with Amazon Cognito ........................................................................................... 1327 Integrating with Amazon Redshift ........................................................................................... 1329 Integrating with Amazon EMR ................................................................................................ 1330 Overview..................................................................................................................... 1330 Tutorial: Working with Amazon DynamoDB and Apache Hive .............................................. 1331 Creating an external table in Hive................................................................................... 1337 Processing HiveQL statements ........................................................................................ 1339 Querying data in DynamoDB .......................................................................................... 1340 Copying data to and from Amazon DynamoDB ................................................................. 1341 Performance tuning ...................................................................................................... 1351 Integrating with S3............................................................................................................... 1354 Import from Amazon S3 ................................................................................................ 1355 Export to Amazon S3 .................................................................................................... 1365 Quotas and limits......................................................................................................................... 1373 Read/write capacity mode and throughput .............................................................................. 1373 Capacity unit sizes (for provisioned tables)....................................................................... 1373 Request unit sizes (for on-demand tables) ....................................................................... 1374 Throughput default quotas............................................................................................ 1374 Increasing or decreasing throughput (for provisioned tables) .............................................. 1375 Tables................................................................................................................................. 1376 Table size.................................................................................................................... 1376 Maximum number of tables per account per region ........................................................... 1376 Global tables........................................................................................................................ 1376 Secondary indexes................................................................................................................ 1377 Secondary indexes per table.......................................................................................... 1377 Projected Secondary Index attributes per table ................................................................. 1377 Partition keys and sort keys ................................................................................................... 1377 Partition key length...................................................................................................... 1377 Partition key values...................................................................................................... 1377 Sort key length ............................................................................................................ 1377 Sort key values............................................................................................................. 1377 Naming rules........................................................................................................................ 1378 Table names and Secondary Index names ........................................................................ 1378 Attribute names........................................................................................................... 1378 Data types........................................................................................................................... 1378 String.......................................................................................................................... 1378 Number....................................................................................................................... 1378 Binary......................................................................................................................... 1379 Items................................................................................................................................... 1379 Item size...................................................................................................................... 1379 Item size for tables with Local Secondary Indexes ............................................................. 1379 Attributes............................................................................................................................ 1379 Attribute name-value pairs per item............................................................................... 1379 Number of values in list, map, or set............................................................................... 1379 Attribute values............................................................................................................ 1379 Nested attribute depth .................................................................................................. 1380 Expression parameters........................................................................................................... 1380 Lengths....................................................................................................................... 1380 Operators and operands................................................................................................ 1380 Reserved words............................................................................................................ 1380 DynamoDB transactions......................................................................................................... 1380 DynamoDB Streams.............................................................................................................. 1381 Simultaneous readers of a shard in DynamoDB Streams ..................................................... 1381 Maximum write capacity for a table with DynamoDB Streams enabled ................................. 1381 API Version 2012-08-10 ix Amazon DynamoDB Developer Guide DynamoDB Accelerator (DAX) ................................................................................................. 1381 AWS region availability .................................................................................................. 1381 Nodes.......................................................................................................................... 1382 Parameter groups......................................................................................................... 1382 Subnet groups.............................................................................................................. 1382 API-specific limits................................................................................................................. 1382 DynamoDB encryption at rest ................................................................................................ 1383 Table export to Amazon S3 ................................................................................................... 1383 Backup and restore ............................................................................................................... 1383 API reference............................................................................................................................... 1384 Appendix..................................................................................................................................... 1385 Troubleshooting SSL/TLS connection establishment issues ......................................................... 1385 Testing your application or service .................................................................................. 1385 Testing your client browser ............................................................................................ 1386 Updating your software application client ........................................................................ 1386 Updating your client browser ......................................................................................... 1386 Manually updating your certificate bundle ....................................................................... 1386 Example tables and data....................................................................................................... 1387 Sample data files.......................................................................................................... 1387 Creating example tables and uploading data ............................................................................ 1396 Creating example tables and uploading data - Java ........................................................... 1396 Creating example tables and uploading data - .NET ........................................................... 1403 Example application using AWS SDK for Python (Boto3) ............................................................ 1412 Step 1: Deploy and test locally ....................................................................................... 1412 Step 2: Examine the data model and implementation details.............................................. 1416 Step 3: Deploy in production ......................................................................................... 1424 Step 4: Clean up resources............................................................................................. 1430 Integrating with AWS Data Pipeline ........................................................................................ 1431 Prerequisites to export and import data .......................................................................... 1433 Exporting data from DynamoDB to Amazon S3 ................................................................ 1437 Importing data from Amazon S3 to DynamoDB ................................................................ 1438 Troubleshooting............................................................................................................ 1439 Predefined templates for AWS Data Pipeline and DynamoDB .............................................. 1440 Amazon DynamoDB Storage Backend for Titan ......................................................................... 1440 Reserved words in DynamoDB ................................................................................................ 1441 Legacy conditional parameters ............................................................................................... 1449 AttributesToGet............................................................................................................ 1450 AttributeUpdates.......................................................................................................... 1451 ConditionalOperator...................................................................................................... 1453 Expected...................................................................................................................... 1453 KeyConditions.............................................................................................................. 1457 QueryFilter................................................................................................................... 1459 ScanFilter..................................................................................................................... 1460 Writing conditions with legacy parameters ....................................................................... 1461 Previous low-level API version (2011-12-05) ............................................................................ 1467 BatchGetItem............................................................................................................... 1467 BatchWriteItem............................................................................................................. 1472 CreateTable.................................................................................................................. 1477 DeleteItem................................................................................................................... 1482 DeleteTable.................................................................................................................. 1486 DescribeTables.............................................................................................................. 1489 GetItem....................................................................................................................... 1492 ListTables..................................................................................................................... 1495 PutItem....................................................................................................................... 1497 Query.......................................................................................................................... 1501 Scan............................................................................................................................ 1510 UpdateItem.................................................................................................................. 1520 API Version 2012-08-10 x

Description:
Amazon's trademarks and trade dress may not be used in connection with any product or service that is not Amazon's, in any manner that is likely to cause confusion among customers, or in any manner that disparages or discredits Amazon. All other trademarks not owned by Amazon are the property of
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.