You can return to the original look by selecting English in the language selector
above.

Step 4: Query and Scan the Data

You can use the query method to retrieve data from a table. You must
specify a partition key value. The sort key is optional.

The primary key for the Movies table is composed of the following:

year – The partition key. The attribute type is
number.

title – The sort key. The attribute type is
string.

To find all movies released during a year, you need to specify only the
year. You can also provide the title to retrieve a subset
of movies based on some condition (on the sort key). For example, you can find movies
released in 2014 that have a title starting with the letter "A".

In addition to the query method, you can use the scan method
to retrieve all the table data.

The preceding program shows how to query a table by its primary key
attributes. In DynamoDB, you can optionally create one or more secondary indexes on
a table and query those indexes in the same way that you query a table.
Secondary indexes give your applications additional flexibility by allowing
queries on non-key attributes. For more information, see Improving Data Access with Secondary Indexes.

Step 4.2: Query - All Movies Released in a
Year with Certain Titles

The program included in this step retrieves all movies released in
year 1992 with title beginning with the letter "A"
through the letter "L".

Copy the following program and paste it into a file named
MoviesQuery02.py.

Step 4.3: Scan

The scan method reads every item in the entire table and returns all
the data in the table. You can provide an optional filter_expression so
that only the items matching your criteria are returned. However, the filter is
applied only after the entire table has been scanned.

The following program scans the entire Movies table, which contains
approximately 5,000 items. The scan specifies the optional filter to retrieve only
the movies from the 1950s (approximately 100 items) and discard all the
others.

Copy the following program and paste it into a file named
MoviesScan.py.

ProjectionExpression specifies the attributes you
want in the scan result.

FilterExpression specifies a condition that returns only items that satisfy the condition. All other
items are discarded.

The scan method returns a subset of the items
each time, called a page. The
LastEvaluatedKey value in the response is then
passed to the scan method via the
ExclusiveStartKey parameter. When the last page is
returned, LastEvaluatedKey is not part of the
response.

Note

ExpressionAttributeNames provides name
substitution. We use this because year is a
reserved word in DynamoDB—you can't use it directly in any
expression, including KeyConditionExpression. You
can use the expression attribute name #yr to
address this.

ExpressionAttributeValues provides value
substitution. You use this because you can't use literals in any
expression, including KeyConditionExpression. You
can use the expression attribute value :yyyy to
address this.