ebook img

Amazon DynamoDB Developer Guide - Amazon Web Services PDF

723 Pages·2014·6.7 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 Web Services

Amazon DynamoDB Developer Guide API Version 2012-08-10 Amazon DynamoDB Developer Guide Amazon DynamoDB: Developer Guide Copyright © 2014 Amazon Web Services, Inc. and/or its affiliates. All rights reserved. The following are trademarks of Amazon Web Services, Inc.: Amazon, Amazon Web Services Design, AWS, Amazon CloudFront, Cloudfront, CloudTrail, Amazon DevPay, DynamoDB, ElastiCache, Amazon EC2, Amazon Elastic Compute Cloud, Amazon Glacier, Kinesis, Kindle, Kindle Fire, AWS Marketplace Design, Mechanical Turk, Amazon Redshift, Amazon Route 53, Amazon S3, Amazon VPC. In addition, Amazon.com graphics, logos, page headers, button icons, scripts, and service names are trademarks, or trade dress of Amazon in the U.S. and/or other countries. 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 Service Highlights ................................................................................................................. 2 Data Model .......................................................................................................................... 3 Data Model Concepts - Tables, Items, and Attributes............................................................ 4 Primary Key ................................................................................................................. 5 Secondary Indexes........................................................................................................ 6 Data Types................................................................................................................... 6 Supported Operations ............................................................................................................ 8 Table Operations ........................................................................................................... 9 Item Operations ............................................................................................................ 9 Query and Scan............................................................................................................ 9 Data Read and Consistency Considerations....................................................................... 9 Conditional Updates and Concurrency Control.................................................................. 10 Provisioned Throughput ........................................................................................................ 10 Read Capacity Units .................................................................................................... 11 Write Capacity Units..................................................................................................... 11 Accessing DynamoDB.......................................................................................................... 11 Regions and Endpoints for DynamoDB............................................................................ 12 Getting Started............................................................................................................................ 13 Step 1: Before You Begin....................................................................................................... 13 Step 1 of 6: Sign Up..................................................................................................... 13 Download AWS SDK.................................................................................................... 13 Step 2: Create Example Tables............................................................................................... 14 Use case 1: Product Catalog.......................................................................................... 14 Use case 2: Forum Application....................................................................................... 15 Creating Tables ........................................................................................................... 15 Step 3: Load Sample Data..................................................................................................... 19 Load Data into Tables - Java.......................................................................................... 20 Load Data into Tables - .NET......................................................................................... 28 Load Data into Tables - PHP.......................................................................................... 38 Verify Data Load.......................................................................................................... 44 Step 4:Try a Query.............................................................................................................. 45 Try a Query - Console .................................................................................................. 45 Try a Query - Java....................................................................................................... 46 Try a Query - .NET....................................................................................................... 49 Try a Query - PHP....................................................................................................... 54 Step 5: Delete Example Tables............................................................................................... 55 Where Do I Go from Here?.................................................................................................... 56 Working with Tables ..................................................................................................................... 57 Specifying the Primary Key.................................................................................................... 57 Read and Write Requirements for Tables................................................................................. 58 Capacity Units Calculations for Various Operations.................................................................... 60 Item Size Calculations .................................................................................................. 60 Read Operation and Consistency ................................................................................... 61 Listing and Describing Tables................................................................................................. 62 Guidelines for Working with Tables.......................................................................................... 62 Design For Uniform Data Access Across Items In Your Tables.............................................. 62 Understand Partition Behavior........................................................................................ 64 Use Burst Capacity Sparingly ........................................................................................ 65 Distribute Write Activity During Data Upload..................................................................... 65 Understand Access Patterns for Time Series Data............................................................. 66 Cache Popular Items.................................................................................................... 67 Consider Workload Uniformity When Adjusting Provisioned Throughput................................ 67 Test Your Application At Scale........................................................................................ 68 Working with Tables - Java Low-Level API................................................................................ 68 API Version 2012-08-10 iii Amazon DynamoDB Developer Guide Creating a Table .......................................................................................................... 69 Updating a Table.......................................................................................................... 70 Deleting a Table........................................................................................................... 71 Listing Tables .............................................................................................................. 71 Example: Create, Update, Delete and List Tables - Java Low-Level API................................. 72 Working with Tables - .NET Low-Level API............................................................................... 75 Creating a Table .......................................................................................................... 76 Updating a Table.......................................................................................................... 77 Deleting a Table........................................................................................................... 78 Listing Tables .............................................................................................................. 78 Example: Create, Update, Delete and List Tables - .NET Low-Level API................................ 79 Working with Tables - PHP Low-Level API................................................................................ 82 Creating a Table .......................................................................................................... 83 Updating a Table.......................................................................................................... 84 Deleting a Table........................................................................................................... 85 Listing Tables .............................................................................................................. 85 Example: Create, Update, Delete and List Tables - PHP Low-Level API................................. 86 Working with Items....................................................................................................................... 89 Overview............................................................................................................................ 89 Reading an Item.................................................................................................................. 90 Read Consistency........................................................................................................ 91 Writing an Item.................................................................................................................... 91 Batch Operations................................................................................................................. 92 Atomic Counters.................................................................................................................. 92 Conditional Writes................................................................................................................ 92 Reading and Writing Items Using Expressions.......................................................................... 94 Case Study: A ProductCatalog Item................................................................................ 95 Accessing Item Attributes.............................................................................................. 96 Specifying Conditions................................................................................................... 98 Modifying Items and Attributes ..................................................................................... 101 Guidelines for Working with Items......................................................................................... 107 Use One-to-Many Tables Instead Of Large Set Attributes.................................................. 108 Use Multiple Tables to Support Varied Access Patterns..................................................... 108 Compress Large Attribute Values.................................................................................. 110 Store Large Attribute Values in Amazon S3..................................................................... 110 Break Up Large Attributes Across Multiple Items............................................................. 110 Working with Items - Java Low-Level API............................................................................... 112 Putting an Item.......................................................................................................... 112 Getting an Item.......................................................................................................... 114 Batch Write: Putting and Deleting Multiple Items.............................................................. 115 Batch Get: Getting Multiple Items.................................................................................. 116 Updating an Item ....................................................................................................... 118 Deleting an Item ....................................................................................................... 120 Example: CRUD Operations - Java Low-Level API........................................................... 122 Example: Batch Operations - Java Low-Level API............................................................ 127 Example: Handling Binary Type Attributes - Java Low-Level API......................................... 133 Working with Items - .NET Low-Level API............................................................................... 136 Putting an Item.......................................................................................................... 137 Getting an Item.......................................................................................................... 138 Updating an Item ....................................................................................................... 139 Atomic Counter.......................................................................................................... 141 Deleting an Item ....................................................................................................... 142 Batch Write: Putting and Deleting Multiple Items.............................................................. 143 Batch Get: Getting Multiple Items.................................................................................. 144 Example: CRUD Operations - .NET Low-Level API........................................................... 147 Example: Batch Operations - .NET Low-Level API........................................................... 154 Example: Handling Binary Type Attributes - .NET Low-Level API........................................ 165 Working with Items - PHP Low-Level API............................................................................... 170 API Version 2012-08-10 iv Amazon DynamoDB Developer Guide Putting an Item.......................................................................................................... 171 Getting an Item.......................................................................................................... 172 Batch Write: Putting and Deleting Multiple Items.............................................................. 173 Batch Get: Getting Multiple Items.................................................................................. 174 Updating an Item ....................................................................................................... 176 Atomic Counter.......................................................................................................... 178 Deleting an Item ....................................................................................................... 179 Example: CRUD Operations - PHP Low-Level API........................................................... 180 Example: Batch Operations-PHP SDK........................................................................... 183 Query and Scan ........................................................................................................................ 185 Using a Filter With Query and Scan....................................................................................... 186 Capacity Units Consumed by Query and Scan........................................................................ 186 Paginating the Results ........................................................................................................ 187 Count and ScannedCount ................................................................................................... 187 Limit ................................................................................................................................ 187 Read Consistency for Query and Scan.................................................................................. 188 Query and Scan Performance.............................................................................................. 188 Parallel Scan..................................................................................................................... 189 Guidelines for Query and Scan ............................................................................................ 190 Avoid Sudden Bursts of Read Activity............................................................................ 191 Take Advantage of Parallel Scans................................................................................. 193 Querying Tables................................................................................................................. 194 Querying Tables - Java Low-Level API........................................................................... 194 Querying Tables - .NET Low-Level API........................................................................... 201 Querying Tables - PHP Low-Level API........................................................................... 214 Scanning Tables ................................................................................................................ 218 Scanning Tables - Java Low-Level API........................................................................... 218 Scanning Tables - .NET Low-Level API.......................................................................... 228 Scanning Tables - PHP Low-Level API........................................................................... 238 Improving Data Access with Secondary Indexes.............................................................................. 246 Local Secondary Indexes .................................................................................................... 251 Attribute Projections ................................................................................................... 253 Creating a Local Secondary Index ................................................................................ 255 Querying a Local Secondary Index................................................................................ 255 Item Writes and Local Secondary Indexes...................................................................... 256 Provisioned Throughput Considerations for Local Secondary Indexes.................................. 257 Storage Considerations for Local Secondary Indexes....................................................... 258 Item Collections......................................................................................................... 259 Guidelines for Local Secondary Indexes......................................................................... 262 Local Secondary Indexes - Java Low-Level API............................................................... 264 Local Secondary Indexes - .NET Low-Level API.............................................................. 276 Local Secondary Indexes - PHP Low-Level API............................................................... 290 Global Secondary Indexes................................................................................................... 299 Attribute Projections ................................................................................................... 302 Creating a Global Secondary Index............................................................................... 303 Querying a Global Secondary Index.............................................................................. 303 Data Synchronization Between Tables and Global Secondary Indexes................................. 304 Provisioned Throughput Considerations for Global Secondary Indexes................................ 304 Storage Considerations for Global Secondary Indexes...................................................... 306 Guidelines for Global Secondary Indexes....................................................................... 306 Global Secondary Indexes - Java Low-Level API............................................................. 308 Global Secondary Indexes - .NET Low-Level API............................................................. 318 Global Secondary Indexes - PHP Low-Level API............................................................. 329 Best Practices Summary............................................................................................................. 339 Table Best Practices........................................................................................................... 339 Item Best Practices ............................................................................................................ 339 Query and Scan Best Practices............................................................................................ 340 Local Secondary Index Best Practices................................................................................... 340 API Version 2012-08-10 v Amazon DynamoDB Developer Guide Global Secondary Index Best Practices.................................................................................. 340 DynamoDB Streams Preview ....................................................................................................... 341 Accessing the DynamoDB Streams Preview........................................................................... 341 For More Information........................................................................................................... 342 The DynamoDB Console............................................................................................................. 343 Working with Items and Attributes......................................................................................... 345 Adding an Item.......................................................................................................... 345 Deleting an Item ........................................................................................................ 348 Updating an Item ....................................................................................................... 348 Copying an Item ........................................................................................................ 349 Monitoring Tables............................................................................................................... 351 Setting up CloudWatch Alarms............................................................................................. 351 Exporting and Importing Data............................................................................................... 352 Using the AWS SDKs ................................................................................................................. 354 Using the AWS SDK for Java............................................................................................... 354 The Java API Organization.......................................................................................... 355 Running Java Examples ............................................................................................. 356 Using the AWS SDK for .NET............................................................................................... 356 Running .NET Examples ............................................................................................. 358 Using the AWS SDK for PHP............................................................................................... 360 Running PHP Examples.............................................................................................. 360 Higher-Level Programming Interfaces for DynamoDB....................................................................... 362 Java: Object Persistence Model............................................................................................ 362 Supported Data Types ................................................................................................ 365 Java Annotations for DynamoDB .................................................................................. 366 The DynamoDBMapper Class...................................................................................... 370 Optimistic Locking With Version Number ........................................................................ 379 Mapping Arbitrary Data............................................................................................... 381 Example: CRUD Operations......................................................................................... 384 Example: Batch Write Operations.................................................................................. 386 Example: Query and Scan........................................................................................... 391 .NET: Document Model....................................................................................................... 399 Operations Not Supported by the Document Model.......................................................... 399 Working with Items - .NET Document Model................................................................... 399 Getting an Item - Table.GetItem ................................................................................... 403 Deleting an Item - Table.DeleteItem .............................................................................. 404 Updating an Item - Table.UpdateItem............................................................................. 405 Batch Write - Putting and Deleting Multiple Items............................................................. 406 Example: CRUD Operations - .NET Document Model....................................................... 408 Example: Batch Operations-.NET Document Model API.................................................... 414 Querying Tables - .NET Document Model....................................................................... 417 .NET: Object Persistence Model ........................................................................................... 430 DynamoDB Attributes ................................................................................................. 431 DynamoDBContext Class ............................................................................................ 434 Supported Data Types ................................................................................................ 439 Optimistic Locking Using Version Number....................................................................... 440 Mapping Arbitrary Data............................................................................................... 442 Batch Operations ....................................................................................................... 446 Example: CRUD Operations - .NET Object Persistence Model........................................... 450 Example: Batch Write Operation................................................................................... 453 Example: Query and Scan - .NET Object Persistence Model.............................................. 459 Using the DynamoDB API ........................................................................................................... 466 JSON Data Format............................................................................................................. 466 JSON Is for the Transport Protocol Only......................................................................... 467 Transferring Binary Data Type Values in JSON................................................................ 468 Making HTTP Requests ...................................................................................................... 468 HTTP Header Contents............................................................................................... 468 HTTP Body Content ................................................................................................... 469 API Version 2012-08-10 vi Amazon DynamoDB Developer Guide Handling HTTP Responses.......................................................................................... 469 Sample DynamoDB JSON Request and Response.......................................................... 470 Handling Errors ................................................................................................................. 471 Error Types ............................................................................................................... 471 API Error Codes ........................................................................................................ 471 Catching Errors ......................................................................................................... 475 Error Retries and Exponential Backoff ........................................................................... 476 Batch Operations and Error Handling ............................................................................ 477 Operations in DynamoDB.................................................................................................... 477 Example Application Using AWS SDK for Python (Boto)................................................................... 479 Step 1: Deploy and Test Locally Using DynamoDB Local........................................................... 480 1.1: Download and Install Required Packages................................................................. 480 1.2:Test the Game Application..................................................................................... 481 Step 2: Examine the Data Model and Implementation Details..................................................... 483 2.1: Basic Data Model................................................................................................. 483 2.2: Application in Action (Code Walkthrough)................................................................. 485 Step 3: Deploy in Production................................................................................................ 490 3.1: Create an IAM Role for Amazon EC2....................................................................... 491 3.2: Create the Games Table in Amazon DynamoDB........................................................ 492 3.3: Bundle and Deploy Tic-Tac-Toe Application Code....................................................... 492 3.4: Set Up the AWS Elastic Beanstalk Environment........................................................ 493 Step 4: Clean Up Resources................................................................................................ 496 Additional Tools and Resources For DynamoDB.............................................................................. 497 DynamoDB Local............................................................................................................... 497 Downloading and Running DynamoDB Local.................................................................. 498 Using DynamoDB Local .............................................................................................. 499 Usage Notes ............................................................................................................. 499 Differences Between DynamoDB Local and DynamoDB................................................... 499 JavaScript Shell for DynamoDB Local.................................................................................... 500 Tutorial..................................................................................................................... 501 Code Editor .............................................................................................................. 502 The AWS Command Line Interface for DynamoDB................................................................... 504 Downloading and Configuring the AWS CLI.................................................................... 504 Using the AWS CLI with DynamoDB.............................................................................. 505 Using the AWS CLI with DynamoDB Local..................................................................... 506 Integration with Other Services..................................................................................................... 507 Monitoring DynamoDB with CloudWatch................................................................................ 507 AWS Management Console......................................................................................... 508 Command Line Interface (CLI)...................................................................................... 508 API.......................................................................................................................... 508 DynamoDB Metrics .................................................................................................... 509 Dimensions for DynamoDB Metrics............................................................................... 513 Using IAM to Control Access to DynamoDB Resources............................................................ 514 Amazon Resource Names (ARNs) for DynamoDB........................................................... 515 DynamoDB Actions .................................................................................................... 515 Condition Types and Operators..................................................................................... 516 IAM Policy Keys......................................................................................................... 516 Example Policies for API Actions and Resource Access.................................................... 518 Fine-Grained Access Control for DynamoDB .................................................................. 522 Example Policies for Fine-Grained Access Control........................................................... 525 Using Web Identity Federation...................................................................................... 531 Exporting, Importing and Transforming Data Using AWS Data Pipeline........................................ 537 Using the AWS Management Console to Export and Import Data....................................... 537 Predefined Templates for AWS Data Pipeline and DynamoDB............................................ 548 Querying and Joining Tables Using Amazon Elastic MapReduce................................................ 549 Prerequisites for Integrating Amazon EMR ..................................................................... 550 Step 1: Create a Key Pair............................................................................................ 551 Step 2: Create a Cluster.............................................................................................. 551 API Version 2012-08-10 vii Amazon DynamoDB Developer Guide Step 3: SSH into the Master Node................................................................................. 559 Step 4: Set Up a Hive Table to Run Hive Commands........................................................ 561 Hive Command Examples for Exporting, Importing, and Querying Data............................... 566 Optimizing Performance .............................................................................................. 573 Walkthrough: Using DynamoDB and Amazon Elastic MapReduce...................................... 575 Loading Data From DynamoDB Into Amazon Redshift.............................................................. 584 Limits....................................................................................................................................... 586 Document History ...................................................................................................................... 591 Appendix .................................................................................................................................. 597 Example Tables and Data.................................................................................................... 597 ProductCatalog Table - Sample Data............................................................................. 598 Forum Table - Sample Data ......................................................................................... 600 Thread Table - Sample Data......................................................................................... 601 Reply Sample Data .................................................................................................... 601 Creating Example Tables and Uploading Data......................................................................... 602 Creating Example Tables and Uploading Data - Java........................................................ 603 Creating Example Tables and Uploading Data - .NET....................................................... 613 Creating Example Tables and Uploading Data - PHP........................................................ 631 Reserved Words in DynamoDB ............................................................................................ 639 Legacy Conditional Parameters............................................................................................ 650 Simple Conditions ...................................................................................................... 650 Using Multiple Conditions............................................................................................ 653 Other Conditional Operators ........................................................................................ 654 Current API Version (2012-08-10)......................................................................................... 656 Previous API Version (2011-12-05)........................................................................................ 657 BatchGetItem ............................................................................................................ 657 BatchWriteItem.......................................................................................................... 662 CreateTable .............................................................................................................. 668 DeleteItem ................................................................................................................ 672 DeleteTable............................................................................................................... 676 DescribeTables.......................................................................................................... 679 GetItem.................................................................................................................... 682 ListTables ................................................................................................................. 684 PutItem .................................................................................................................... 686 Query ...................................................................................................................... 691 Scan........................................................................................................................ 697 UpdateItem ............................................................................................................... 705 UpdateTable.............................................................................................................. 710 AWS Glossary........................................................................................................................... 715 API Version 2012-08-10 viii Amazon DynamoDB Developer Guide What is Amazon DynamoDB? Topics • Service Highlights (p.2) • DynamoDB Data Model (p.3) • Supported Operations in DynamoDB (p.8) • Provisioned Throughput in Amazon DynamoDB (p.10) • Accessing DynamoDB (p.11) Welcome to the Amazon DynamoDB Developer Guide. DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. If you are a developer, you can use DynamoDB to create a database table that can store and retrieve any amount of data, and serve any level of request traffic. DynamoDB automatically spreads the data and traffic for the table over a sufficient number of servers to handle the request capacity specified by the customer and the amount of data stored, while maintaining consistent and fast performance. All data items are stored on solid state disks (SSDs) and are automatically replicated across multiple Availability Zones in a Region to provide built-in high availability and data durability. If you are a database administrator, you can create a new DynamoDB database table, scale up or down your request capacity for the table without downtime or performance degradation, and gain visibility into resource utilization and performance metrics, all through the AWS Management Console.With DynamoDB, you can offload the administrative burdens of operating and scaling distributed databases to AWS, so you don't have to worry about hardware provisioning, setup and configuration, replication, software patching, or cluster scaling. If you are a first-time user of DynamoDB, we recommend that you begin by reading the following sections: • What is DynamoDB—The rest of this section describes the underlying data model, the operations it supports, and the class libraries that you can use to develop applications that use DynamoDB. • Getting Started (p.13)—The Getting Started section walks you through the process of creating sample tables, uploading data, and performing some basic database operations. Beyond getting started, you'll probably want to learn more about application development with DynamoDB. The following sections provide additional information. • Working with DynamoDB—The following sections provide in-depth information about the key Dy- namoDB concepts: • Working with Tables (p.57) API Version 2012-08-10 1 Amazon DynamoDB Developer Guide Service Highlights • Working with Items (p.89) • Query and Scan Operations (p.185) • Improving Data Access with Secondary Indexes (p.246) • Using AWS SDKs—AWS provides SDKs for you to develop applications using DynamoDB.These SDKs provide low-level API methods that correspond closely to the underlying DynamoDB operations. The .NET SDK also provides a document model to further simplify your development work. In addition, the AWS SDKs for Java and .NET also provide an object persistence model API that you can use to map your client-side classes to DynamoDB tables.This allows you to call object methods instead of making low-level API calls. For more information, including working samples, see Using the AWS SDKs with DynamoDB (p.354). In addition to .NET, Java, and PHP examples provided in this guide, the other AWS SDKs also support DynamoDB, including JavaScript, Python, Android, iOS, and Ruby. For links to the complete set of AWS SDKs, see Start Developing with Amazon Web Services. Service Highlights Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable per- formance with seamless scalability.With a few clicks in the AWS Management Console, customers can create a new DynamoDB database table, scale up or down their request capacity for the table without downtime or performance degradation, and gain visibility into resource utilization and performance metrics. DynamoDB enables customers to offload the administrative burdens of operating and scaling distributed databases to AWS, so they don't have to worry about hardware provisioning, setup and configuration, replication, software patching, or cluster scaling. DynamoDB is designed to address the core problems of database management, performance, scalability, and reliability. Developers can create a database table and grow its request traffic or storage without limit. DynamoDB automatically spreads the data and traffic for the table over a sufficient number of servers to handle the request capacity specified by the customer and the amount of data stored, while maintaining consistent, fast performance. All data items are stored on Solid State Disks (SSDs) and are automatically replicated across multiple Availability Zones in a Region to provide built-in high availability and data durability. DynamoDB enables customers to offload the administrative burden of operating and scaling a highly available distributed database cluster while only paying a low variable price for the resources they consume. The following are some of the major DynamoDB features: • Scalable — DynamoDB is designed for seamless throughput and storage scaling. • Provisioned Throughput —When creating a table, simply specify how much throughput capacity you require. DynamoDB allocates dedicated resources to your table to meet your performance re- quirements, and automatically partitions data over a sufficient number of servers to meet your request capacity. If your application requirements change, simply update your table throughput capacity using the AWS Management Console or the DynamoDB APIs.You are still able to achieve your prior throughput levels while scaling is underway. • Automated Storage Scaling —There is no limit to the amount of data you can store in an DynamoDB table, and the service automatically allocates more storage, as you store more data using the Dy- namoDB write APIs. • Fully Distributed, Shared Nothing Architecture — DynamoDB scales horizontally and seamlessly scales a single table over hundreds of servers. • Fast, Predictable Performance— Average service-side latencies for DynamoDB are typically single- digit milliseconds.The service runs on solid state disks, and is built to maintain consistent, fast latencies at any scale. API Version 2012-08-10 2

Description:
Aug 10, 2012 Kinesis, Kindle, Kindle Fire, AWS Marketplace Design, Mechanical Turk, Amazon.com graphics, logos, page headers, button icons, scripts,
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.