示例#1
0
    def test_query_on_common_key(self):
        yield self.create_table()
        for iteration in range(0, 10):
            payload = {
                'RequestItems': {
                    self.definition['TableName']: [{
                        'PutRequest': {
                            'Item': utils.marshall(self.new_item_value())
                        }
                    } for _row in range(0, 25)]
                },
                'ReturnConsumedCapacity': 'TOTAL',
                'ReturnItemCollectionMetrics': 'SIZE'
            }
            yield self.client.execute('BatchWriteItem', payload)

        items = []
        kwargs = {'limit': 5}
        while True:
            result = yield self.client.scan(self.definition['TableName'],
                                            **kwargs)
            items += result['Items']
            if not result.get('LastEvaluatedKey'):
                break
            kwargs['exclusive_start_key'] = result['LastEvaluatedKey']
        self.assertEqual(len(items), 250)
示例#2
0
    def get_item(self,
                 table_name,
                 key_dict,
                 consistent_read=False,
                 expression_attribute_names=None,
                 projection_expression=None,
                 return_consumed_capacity=None):
        """
        Invoke the `GetItem`_ function.

        :param str table_name: table to retrieve the item from
        :param dict key_dict: key to use for retrieval.  This will
            be marshalled for you so a native :class:`dict` works.
        :param bool consistent_read: Determines the read consistency model: If
            set to :py:data`True`, then the operation uses strongly consistent
            reads; otherwise, the operation uses eventually consistent reads.
        :param dict expression_attribute_names: One or more substitution tokens
            for attribute names in an expression.
        :param str projection_expression: A string that identifies one or more
            attributes to retrieve from the table. These attributes can include
            scalars, sets, or elements of a JSON document. The attributes in
            the expression must be separated by commas. If no attribute names
            are specified, then all attributes will be returned. If any of the
            requested attributes are not found, they will not appear in the
            result.
        :param str return_consumed_capacity: Determines the level of detail
            about provisioned throughput consumption that is returned in the
            response:

              - INDEXES: The response includes the aggregate consumed
                capacity for the operation, together with consumed capacity for
                each table and secondary index that was accessed. Note that
                some operations, such as *GetItem* and *BatchGetItem*, do not
                access any indexes at all. In these cases, specifying INDEXES
                will only return consumed capacity information for table(s).
              - TOTAL: The response includes only the aggregate consumed
                capacity for the operation.
              - NONE: No consumed capacity details are included in the
                response.
        :rtype: tornado.concurrent.Future

        .. _GetItem: http://docs.aws.amazon.com/amazondynamodb/
           latest/APIReference/API_GetItem.html

        """
        payload = {
            'TableName': table_name,
            'Key': utils.marshall(key_dict),
            'ConsistentRead': consistent_read
        }
        if expression_attribute_names:
            payload['ExpressionAttributeNames'] = expression_attribute_names
        if projection_expression:
            payload['ProjectionExpression'] = projection_expression
        if return_consumed_capacity:
            _validate_return_consumed_capacity(return_consumed_capacity)
            payload['ReturnConsumedCapacity'] = return_consumed_capacity
        return self.execute('GetItem', payload)
示例#3
0
 def test_complex_document(self):
     uuid_value = uuid.uuid4()
     dt_value = datetime.datetime.utcnow().replace(tzinfo=UTC())
     value = {
         'key1': 'str',
         'key2': 10,
         'key3': {
             'sub-key1': 20,
             'sub-key2': True,
             'sub-key3': 'value',
             'sub-key4': ''
         },
         'key4': None,
         'key5': ['one', 'two', 'three', 4, None, True],
         'key6': {'a', 'b', 'c'},
         'key7': {1, 2, 3, 4},
         'key9': uuid_value,
         'key10': b'\0x01\0x02\0x03',
         'key11': {b'\0x01\0x02\0x03', b'\0x04\0x05\0x06'},
         'key12': dt_value,
         'key13': '',
         'key14': b''
     }
     expectation = {
         'key1': {
             'S': 'str'
         },
         'key2': {
             'N': '10'
         },
         'key3': {
             'M': {
                 'sub-key1': {
                     'N': '20'
                 },
                 'sub-key2': {
                     'BOOL': True
                 },
                 'sub-key3': {
                     'S': 'value'
                 },
                 'sub-key4': {
                     'NULL': True
                 }
             }
         },
         'key4': {
             'NULL': True
         },
         'key5': {
             'L': [{
                 'S': 'one'
             }, {
                 'S': 'two'
             }, {
                 'S': 'three'
             }, {
                 'N': '4'
             }, {
                 'NULL': True
             }, {
                 'BOOL': True
             }]
         },
         'key6': {
             'SS': ['a', 'b', 'c']
         },
         'key7': {
             'NS': ['1', '2', '3', '4']
         },
         'key9': {
             'S': str(uuid_value)
         },
         'key10': {
             'B': base64.b64encode(b'\0x01\0x02\0x03').decode('ascii')
         },
         'key11': {
             'BS': [
                 base64.b64encode(b'\0x01\0x02\0x03').decode('ascii'),
                 base64.b64encode(b'\0x04\0x05\0x06').decode('ascii')
             ]
         },
         'key12': {
             'S': dt_value.isoformat()
         },
         'key13': {
             'NULL': True
         },
         'key14': {
             'NULL': True
         }
     }
     self.assertDictEqual(expectation, utils.marshall(value))
示例#4
0
    def scan(self,
             table_name,
             index_name=None,
             consistent_read=None,
             projection_expression=None,
             filter_expression=None,
             expression_attribute_names=None,
             expression_attribute_values=None,
             segment=None,
             total_segments=None,
             select=None,
             limit=None,
             exclusive_start_key=None,
             return_consumed_capacity=None):
        """The `Scan`_ operation returns one or more items and item attributes
        by accessing every item in a table or a secondary index.

        If the total number of scanned items exceeds the maximum data set size
        limit of 1 MB, the scan stops and results are returned to the user as a
        ``LastEvaluatedKey`` value to continue the scan in a subsequent
        operation. The results also include the number of items exceeding the
        limit. A scan can result in no table data meeting the filter criteria.

        By default, Scan operations proceed sequentially; however, for faster
        performance on a large table or secondary index, applications can
        request a parallel *Scan* operation by providing the ``segment`` and
        ``total_segments`` parameters. For more information, see
        `Parallel Scan <http://docs.aws.amazon.com/amazondynamodb/latest/
        developerguide/QueryAndScan.html#QueryAndScanParallelScan>`_ in the
        Amazon DynamoDB Developer Guide.

        By default, *Scan* uses eventually consistent reads when accessing the
        data in a table; therefore, the result set might not include the
        changes to data in the table immediately before the operation began. If
        you need a consistent copy of the data, as of the time that the *Scan*
        begins, you can set the ``consistent_read`` parameter to ``True``.

        :rtype: dict

        .. _Scan: http://docs.aws.amazon.com/amazondynamodb/
           latest/APIReference/API_Scan.html

        """
        payload = {'TableName': table_name}
        if index_name:
            payload['IndexName'] = index_name
        if consistent_read is not None:
            payload['ConsistentRead'] = consistent_read
        if filter_expression:
            payload['FilterExpression'] = filter_expression
        if expression_attribute_names:
            payload['ExpressionAttributeNames'] = expression_attribute_names
        if expression_attribute_values:
            payload['ExpressionAttributeValues'] = \
                utils.marshall(expression_attribute_values)
        if projection_expression:
            payload['ProjectionExpression'] = projection_expression
        if segment:
            payload['Segment'] = segment
        if total_segments:
            payload['TotalSegments'] = total_segments
        if select:
            _validate_select(select)
            payload['Select'] = select
        if exclusive_start_key:
            payload['ExclusiveStartKey'] = utils.marshall(exclusive_start_key)
        if limit:
            payload['Limit'] = limit
        if return_consumed_capacity:
            _validate_return_consumed_capacity(return_consumed_capacity)
            payload['ReturnConsumedCapacity'] = return_consumed_capacity
        return self.execute('Scan', payload)
示例#5
0
    def query(self,
              table_name,
              index_name=None,
              consistent_read=None,
              key_condition_expression=None,
              filter_expression=None,
              expression_attribute_names=None,
              expression_attribute_values=None,
              projection_expression=None,
              select=None,
              exclusive_start_key=None,
              limit=None,
              scan_index_forward=True,
              return_consumed_capacity=None):
        """A `Query`_ operation uses the primary key of a table or a secondary
        index to directly access items from that table or index.

        :param str table_name: The name of the table containing the requested
            items.
        :param bool consistent_read: Determines the read consistency model: If
            set to ``True``, then the operation uses strongly consistent reads;
            otherwise, the operation uses eventually consistent reads. Strongly
            consistent reads are not supported on global secondary indexes. If
            you query a global secondary index with ``consistent_read`` set to
            ``True``, you will receive a
            :exc:`~sprockets_dynamodb.exceptions.ValidationException`.
        :param dict exclusive_start_key: The primary key of the first
            item that this operation will evaluate. Use the value that was
            returned for ``LastEvaluatedKey`` in the previous operation. In a
            parallel scan, a *Scan* request that includes
            ``exclusive_start_key`` must specify the same segment whose
            previous *Scan* returned the corresponding value of
            ``LastEvaluatedKey``.
        :param dict expression_attribute_names: One or more substitution tokens
            for attribute names in an expression.
        :param dict expression_attribute_values: One or more values that can be
            substituted in an expression.
        :param str key_condition_expression: The condition that specifies the
            key value(s) for items to be retrieved by the *Query* action. The
            condition must perform an equality test on a single partition key
            value, but can optionally perform one of several comparison tests
            on a single sort key value. The partition key equality test is
            required. For examples see `KeyConditionExpression
            <https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/
            Query.html#Query.KeyConditionExpressions>.
        :param str filter_expression: A string that contains conditions that
            DynamoDB applies after the *Query* operation, but before the data
            is returned to you. Items that do not satisfy the criteria are not
            returned. Note that a filter expression is applied after the items
            have already been read; the process of filtering does not consume
            any additional read capacity units. For more information, see
            `Filter Expressions <http://docs.aws.amazon.com/amazondynamodb/
            latest/developerguide/QueryAndScan.html#FilteringResults>`_ in the
            Amazon DynamoDB Developer Guide.
        :param str projection_expression:
        :param str index_name: The name of a secondary index to query. This
            index can be any local secondary index or global secondary index.
            Note that if you use this parameter, you must also provide
            ``table_name``.
        :param int limit: The maximum number of items to evaluate (not
            necessarily the number of matching items). If DynamoDB processes
            the number of items up to the limit while processing the results,
            it stops the operation and returns the matching values up to that
            point, and a key in ``LastEvaluatedKey`` to apply in a subsequent
            operation, so that you can pick up where you left off. Also, if the
            processed data set size exceeds 1 MB before DynamoDB reaches this
            limit, it stops the operation and returns the matching values up to
            the limit, and a key in ``LastEvaluatedKey`` to apply in a
            subsequent operation to continue the operation. For more
            information, see `Query and Scan <http://docs.aws.amazon.com/amazo
            ndynamodb/latest/developerguide/QueryAndScan.html>`_ in the Amazon
            DynamoDB Developer Guide.
        :param str return_consumed_capacity: Determines the level of detail
            about provisioned throughput consumption that is returned in the
            response:

              - ``INDEXES``: The response includes the aggregate consumed
                capacity for the operation, together with consumed capacity for
                each table and secondary index that was accessed. Note that
                some operations, such as *GetItem* and *BatchGetItem*, do not
                access any indexes at all. In these cases, specifying
                ``INDEXES`` will only return consumed capacity information for
                table(s).
              - ``TOTAL``: The response includes only the aggregate consumed
                capacity for the operation.
              - ``NONE``: No consumed capacity details are included in the
                response.
        :param bool scan_index_forward: Specifies the order for index
            traversal: If ``True`` (default), the traversal is performed in
            ascending order; if ``False``, the traversal is performed in
            descending order. Items with the same partition key value are
            stored in sorted order by sort key. If the sort key data type is
            *Number*, the results are stored in numeric order. For type
            *String*, the results are stored in order of ASCII character code
            values. For type *Binary*, DynamoDB treats each byte of the binary
            data as unsigned. If set to ``True``, DynamoDB returns the results
            in the order in which they are stored (by sort key value). This is
            the default behavior. If set to ``False``, DynamoDB reads the
            results in reverse order by sort key value, and then returns the
            results to the client.
        :param str select: The attributes to be returned in the result. You can
            retrieve all item attributes, specific item attributes, the count
            of matching items, or in the case of an index, some or all of the
            attributes projected into the index. Possible values are:

              - ``ALL_ATTRIBUTES``: Returns all of the item attributes from the
                specified table or index. If you query a local secondary index,
                then for each matching item in the index DynamoDB will fetch
                the entire item from the parent table. If the index is
                configured to project all item attributes, then all of the data
                can be obtained from the local secondary index, and no fetching
                is required.
              - ``ALL_PROJECTED_ATTRIBUTES``: Allowed only when querying an
                index. Retrieves all attributes that have been projected into
                the index. If the index is configured to project all
                attributes, this return value is equivalent to specifying
                ``ALL_ATTRIBUTES``.
              - ``COUNT``: Returns the number of matching items, rather than
                the matching items themselves.
        :rtype: dict

        .. _Query: http://docs.aws.amazon.com/amazondynamodb/
           latest/APIReference/API_Query.html

        """
        payload = {
            'TableName': table_name,
            'ScanIndexForward': scan_index_forward
        }
        if index_name:
            payload['IndexName'] = index_name
        if consistent_read is not None:
            payload['ConsistentRead'] = consistent_read
        if key_condition_expression:
            payload['KeyConditionExpression'] = key_condition_expression
        if filter_expression:
            payload['FilterExpression'] = filter_expression
        if expression_attribute_names:
            payload['ExpressionAttributeNames'] = expression_attribute_names
        if expression_attribute_values:
            payload['ExpressionAttributeValues'] = \
                utils.marshall(expression_attribute_values)
        if projection_expression:
            payload['ProjectionExpression'] = projection_expression
        if select:
            _validate_select(select)
            payload['Select'] = select
        if exclusive_start_key:
            payload['ExclusiveStartKey'] = utils.marshall(exclusive_start_key)
        if limit:
            payload['Limit'] = limit
        if return_consumed_capacity:
            _validate_return_consumed_capacity(return_consumed_capacity)
            payload['ReturnConsumedCapacity'] = return_consumed_capacity
        return self.execute('Query', payload)
示例#6
0
    def delete_item(self,
                    table_name,
                    key_dict,
                    condition_expression=None,
                    expression_attribute_names=None,
                    expression_attribute_values=None,
                    return_consumed_capacity=None,
                    return_item_collection_metrics=None,
                    return_values=False):
        """Invoke the `DeleteItem`_ function that deletes a single item in a
        table by primary key. You can perform a conditional delete operation
        that deletes the item if it exists, or if it has an expected attribute
        value.

        :param str table_name: The name of the table from which to delete the
            item.
        :param dict key_dict: A map of attribute names to ``AttributeValue``
            objects, representing the primary key of the item to delete. For
            the primary key, you must provide all of the attributes. For
            example, with a simple primary key, you only need to provide a
            value for the partition key. For a composite primary key, you must
            provide values for both the partition key and the sort key.
        :param str condition_expression: A condition that must be satisfied in
            order for a conditional *DeleteItem* to succeed. See the `AWS
            documentation for ConditionExpression <http://docs.aws.amazon.com/
            amazondynamodb/latest/APIReference/API_DeleteItem.html#DDB-Delete
            Item-request-ConditionExpression>`_ for more information.
        :param dict expression_attribute_names: One or more substitution tokens
            for attribute names in an expression. See the `AWS documentation
            for ExpressionAttributeNames <http://docs.aws.amazon.com/
            amazondynamodb/latest/APIReference/API_DeleteItem.html#DDB-Delete
            Item-request-ExpressionAttributeNames>`_ for more information.
        :param dict expression_attribute_values: One or more values that can be
            substituted in an expression. See the `AWS documentation
            for ExpressionAttributeValues <http://docs.aws.amazon.com/
            amazondynamodb/latest/APIReference/API_DeleteItem.html#DDB-Delete
            Item-request-ExpressionAttributeValues>`_ for more information.
        :param str return_consumed_capacity: Determines the level of detail
            about provisioned throughput consumption that is returned in the
            response. See the `AWS documentation
            for ReturnConsumedCapacity <http://docs.aws.amazon.com/
            amazondynamodb/latest/APIReference/API_DeleteItem.html#DDB-Delete
            Item-request-ReturnConsumedCapacity>`_ for more information.
        :param str return_item_collection_metrics: Determines whether item
            collection metrics are returned.
        :param str return_values: Return the item attributes as they appeared
            before they were deleted.

        .. _DeleteItem: http://docs.aws.amazon.com/amazondynamodb/
           latest/APIReference/API_DeleteItem.html

        """
        payload = {'TableName': table_name, 'Key': utils.marshall(key_dict)}
        if condition_expression:
            payload['ConditionExpression'] = condition_expression
        if expression_attribute_names:
            payload['ExpressionAttributeNames'] = expression_attribute_names
        if expression_attribute_values:
            payload['ExpressionAttributeValues'] = \
                utils.marshall(expression_attribute_values)
        if return_consumed_capacity:
            _validate_return_consumed_capacity(return_consumed_capacity)
            payload['ReturnConsumedCapacity'] = return_consumed_capacity
        if return_item_collection_metrics:
            _validate_return_item_collection_metrics(
                return_item_collection_metrics)
            payload['ReturnItemCollectionMetrics'] = \
                return_item_collection_metrics
        if return_values:
            _validate_return_values(return_values)
            payload['ReturnValues'] = return_values
        return self.execute('DeleteItem', payload)
示例#7
0
    def update_item(self,
                    table_name,
                    key_dict,
                    condition_expression=None,
                    update_expression=None,
                    expression_attribute_names=None,
                    expression_attribute_values=None,
                    return_consumed_capacity=None,
                    return_item_collection_metrics=None,
                    return_values=None):
        """Invoke the `UpdateItem`_ function.

        Edits an existing item's attributes, or adds a new item to the table
        if it does not already exist. You can put, delete, or add attribute
        values. You can also perform a conditional update on an existing item
        (insert a new attribute name-value pair if it doesn't exist, or replace
        an existing name-value pair if it has certain expected attribute
        values).

        :param str table_name: The name of the table that contains the item to
            update
        :param dict key_dict: A dictionary of key/value pairs that are used to
            define the primary key values for the item. For the primary key,
            you must provide all of the attributes. For example, with a simple
            primary key, you only need to provide a value for the partition
            key. For a composite primary key, you must provide values for both
            the partition key and the sort key.
        :param str condition_expression: A condition that must be satisfied in
            order for a conditional *UpdateItem* operation to succeed. One of:
            ``attribute_exists``, ``attribute_not_exists``, ``attribute_type``,
            ``contains``, ``begins_with``, ``size``, ``=``, ``<>``, ``<``,
            ``>``, ``<=``, ``>=``, ``BETWEEN``, ``IN``, ``AND``, ``OR``, or
            ``NOT``.
        :param str update_expression: An expression that defines one or more
            attributes to be updated, the action to be performed on them, and
            new value(s) for them.
        :param dict expression_attribute_names: One or more substitution tokens
            for attribute names in an expression.
        :param dict expression_attribute_values: One or more values that can be
            substituted in an expression.
        :param str return_consumed_capacity: Determines the level of detail
            about provisioned throughput consumption that is returned in the
            response. See the `AWS documentation
            for ReturnConsumedCapacity <http://docs.aws.amazon.com/
            amazondynamodb/latest/APIReference/API_UpdateItem.html#DDB-Update
            Item-request-ReturnConsumedCapacity>`_ for more information.
        :param str return_item_collection_metrics: Determines whether item
            collection metrics are returned.
        :param str return_values: Use ReturnValues if you want to get the item
            attributes as they appeared either before or after they were
            updated. See the `AWS documentation for ReturnValues <http://docs.
            aws.amazon.com/amazondynamodb/latest/APIReference/
            API_UpdateItem.html#DDB-UpdateItem-request-ReturnValues>`_
        :rtype: tornado.concurrent.Future

        .. _UpdateItem: http://docs.aws.amazon.com/amazondynamodb/
           latest/APIReference/API_UpdateItem.html

        """
        payload = {
            'TableName': table_name,
            'Key': utils.marshall(key_dict),
            'UpdateExpression': update_expression
        }
        if condition_expression:
            payload['ConditionExpression'] = condition_expression
        if expression_attribute_names:
            payload['ExpressionAttributeNames'] = expression_attribute_names
        if expression_attribute_values:
            payload['ExpressionAttributeValues'] = \
                utils.marshall(expression_attribute_values)
        if return_consumed_capacity:
            _validate_return_consumed_capacity(return_consumed_capacity)
            payload['ReturnConsumedCapacity'] = return_consumed_capacity
        if return_item_collection_metrics:
            _validate_return_item_collection_metrics(
                return_item_collection_metrics)
            payload['ReturnItemCollectionMetrics'] = \
                return_item_collection_metrics
        if return_values:
            _validate_return_values(return_values)
            payload['ReturnValues'] = return_values
        return self.execute('UpdateItem', payload)
示例#8
0
    def put_item(self,
                 table_name,
                 item,
                 condition_expression=None,
                 expression_attribute_names=None,
                 expression_attribute_values=None,
                 return_consumed_capacity=None,
                 return_item_collection_metrics=None,
                 return_values=None):
        """Invoke the `PutItem`_ function, creating a new item, or replaces an
        old item with a new item. If an item that has the same primary key as
        the new item already exists in the specified table, the new item
        completely replaces the existing item. You can perform a conditional
        put operation (add a new item if one with the specified primary key
        doesn't exist), or replace an existing item if it has certain attribute
        values.

        For more information about using this API, see Working with Items in
        the Amazon DynamoDB Developer Guide.

        :param str table_name: The table to put the item to
        :param dict item: A map of attribute name/value pairs, one for each
            attribute. Only the primary key attributes are required; you can
            optionally provide other attribute name-value pairs for the item.

            You must provide all of the attributes for the primary key. For
            example, with a simple primary key, you only need to provide a
            value for the partition key. For a composite primary key, you must
            provide both values for both the partition key and the sort key.

            If you specify any attributes that are part of an index key, then
            the data types for those attributes must match those of the schema
            in the table's attribute definition.
        :param str condition_expression: A condition that must be satisfied in
            order for a conditional *PutItem* operation to succeed. See the
            `AWS documentation for ConditionExpression <http://docs.aws.amazon.
            com/amazondynamodb/latest/APIReference/API_PutItem.html#DDB-Put
            Item-request-ConditionExpression>`_ for more information.
        :param dict expression_attribute_names: One or more substitution tokens
            for attribute names in an expression. See the `AWS documentation
            for ExpressionAttributeNames <http://docs.aws.amazon.com/amazon
            dynamodb/latest/APIReference/API_PutItem.html#DDB-PutItem-request-
            ExpressionAttributeNames>`_ for more information.
        :param dict expression_attribute_values: One or more values that can be
            substituted in an expression. See the `AWS documentation
            for ExpressionAttributeValues <http://docs.aws.amazon.com/amazon
            dynamodb/latest/APIReference/API_PutItem.html#DDB-PutItem-request-
            ExpressionAttributeValues>`_ for more information.
        :param str return_consumed_capacity: Determines the level of detail
            about provisioned throughput consumption that is returned in the
            response. Should be ``None`` or one of ``INDEXES`` or ``TOTAL``
        :param str return_item_collection_metrics: Determines whether item
            collection metrics are returned.
        :param str return_values: Use ``ReturnValues`` if you want to get the
            item attributes as they appeared before they were updated with the
            ``PutItem`` request.
        :rtype: tornado.concurrent.Future

        .. _PutItem: http://docs.aws.amazon.com/amazondynamodb/
           latest/APIReference/API_PutItem.html

        """
        payload = {'TableName': table_name, 'Item': utils.marshall(item)}
        if condition_expression:
            payload['ConditionExpression'] = condition_expression
        if expression_attribute_names:
            payload['ExpressionAttributeNames'] = expression_attribute_names
        if expression_attribute_values:
            payload['ExpressionAttributeValues'] = expression_attribute_values
        if return_consumed_capacity:
            payload['ReturnConsumedCapacity'] = return_consumed_capacity
        if return_item_collection_metrics:
            payload['ReturnItemCollectionMetrics'] = 'SIZE'
        if return_values:
            _validate_return_values(return_values)
            payload['ReturnValues'] = return_values
        return self.execute('PutItem', payload)