当前位置: 首页>>代码示例>>Python>>正文


Python DynamoDBConnection.get_item方法代码示例

本文整理汇总了Python中boto.dynamodb2.layer1.DynamoDBConnection.get_item方法的典型用法代码示例。如果您正苦于以下问题:Python DynamoDBConnection.get_item方法的具体用法?Python DynamoDBConnection.get_item怎么用?Python DynamoDBConnection.get_item使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在boto.dynamodb2.layer1.DynamoDBConnection的用法示例。


在下文中一共展示了DynamoDBConnection.get_item方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Table

# 需要导入模块: from boto.dynamodb2.layer1 import DynamoDBConnection [as 别名]
# 或者: from boto.dynamodb2.layer1.DynamoDBConnection import get_item [as 别名]

#.........这里部分代码省略.........
            True

        """
        self.connection.delete_table(self.table_name)
        return True

    def _encode_keys(self, keys):
        """
        Given a flat Python dictionary of keys/values, converts it into the
        nested dictionary DynamoDB expects.

        Converts::

            {
                'username': 'john',
                'tags': [1, 2, 5],
            }

        ...to...::

            {
                'username': {'S': 'john'},
                'tags': {'NS': ['1', '2', '5']},
            }

        """
        raw_key = {}

        for key, value in keys.items():
            raw_key[key] = self._dynamizer.encode(value)

        return raw_key

    def get_item(self, consistent=False, **kwargs):
        """
        Fetches an item (record) from a table in DynamoDB.

        To specify the key of the item you'd like to get, you can specify the
        key attributes as kwargs.

        Optionally accepts a ``consistent`` parameter, which should be a
        boolean. If you provide ``True``, it will perform
        a consistent (but more expensive) read from DynamoDB.
        (Default: ``False``)

        Returns an ``Item`` instance containing all the data for that record.

        Example::

            # A simple hash key.
            >>> john = users.get_item(username='johndoe')
            >>> john['first_name']
            'John'

            # A complex hash+range key.
            >>> john = users.get_item(username='johndoe', last_name='Doe')
            >>> john['first_name']
            'John'

            # A consistent read (assuming the data might have just changed).
            >>> john = users.get_item(username='johndoe', consistent=True)
            >>> john['first_name']
            'Johann'

            # With a key that is an invalid variable name in Python.
            # Also, assumes a different schema than previous examples.
开发者ID:kolencherry,项目名称:dd-agent,代码行数:70,代码来源:table.py

示例2: DynamoDBv2Layer1Test

# 需要导入模块: from boto.dynamodb2.layer1 import DynamoDBConnection [as 别名]
# 或者: from boto.dynamodb2.layer1.DynamoDBConnection import get_item [as 别名]

#.........这里部分代码省略.........
                else:
                    time.sleep(5)
        else:
            return result

    def test_integrated(self):
        result = self.create_table(
            self.table_name,
            self.attributes,
            self.schema,
            self.provisioned_throughput,
            self.lsi
        )
        self.assertEqual(
            result['TableDescription']['TableName'],
            self.table_name
        )

        description = self.dynamodb.describe_table(self.table_name)
        self.assertEqual(description['Table']['ItemCount'], 0)

        # Create some records.
        record_1_data = {
            'username': {'S': 'johndoe'},
            'first_name': {'S': 'John'},
            'last_name': {'S': 'Doe'},
            'date_joined': {'N': '1366056668'},
            'friend_count': {'N': '3'},
            'friends': {'SS': ['alice', 'bob', 'jane']},
        }
        r1_result = self.dynamodb.put_item(self.table_name, record_1_data)

        # Get the data.
        record_1 = self.dynamodb.get_item(self.table_name, key={
            'username': {'S': 'johndoe'},
            'date_joined': {'N': '1366056668'},
        }, consistent_read=True)
        self.assertEqual(record_1['Item']['username']['S'], 'johndoe')
        self.assertEqual(record_1['Item']['first_name']['S'], 'John')
        self.assertEqual(record_1['Item']['friends']['SS'], [
            'alice', 'bob', 'jane'
        ])

        # Now in a batch.
        self.dynamodb.batch_write_item({
            self.table_name: [
                {
                    'PutRequest': {
                        'Item': {
                            'username': {'S': 'jane'},
                            'first_name': {'S': 'Jane'},
                            'last_name': {'S': 'Doe'},
                            'date_joined': {'N': '1366056789'},
                            'friend_count': {'N': '1'},
                            'friends': {'SS': ['johndoe']},
                        },
                    },
                },
            ]
        })

        # Now a query.
        lsi_results = self.dynamodb.query(
            self.table_name,
            index_name='MostRecentIndex',
            key_conditions={
开发者ID:0t3dWCE,项目名称:boto,代码行数:70,代码来源:test_layer1.py

示例3: Table

# 需要导入模块: from boto.dynamodb2.layer1 import DynamoDBConnection [as 别名]
# 或者: from boto.dynamodb2.layer1.DynamoDBConnection import get_item [as 别名]

#.........这里部分代码省略.........
            True

        """
        self.connection.delete_table(self.table_name)
        return True

    def _encode_keys(self, keys):
        """
        Given a flat Python dictionary of keys/values, converts it into the
        nested dictionary DynamoDB expects.

        Converts::

            {
                'username': 'john',
                'tags': [1, 2, 5],
            }

        ...to...::

            {
                'username': {'S': 'john'},
                'tags': {'NS': ['1', '2', '5']},
            }

        """
        raw_key = {}

        for key, value in keys.items():
            raw_key[key] = self._dynamizer.encode(value)

        return raw_key

    def get_item(self, consistent=False, attributes=None, **kwargs):
        """
        Fetches an item (record) from a table in DynamoDB.

        To specify the key of the item you'd like to get, you can specify the
        key attributes as kwargs.

        Optionally accepts a ``consistent`` parameter, which should be a
        boolean. If you provide ``True``, it will perform
        a consistent (but more expensive) read from DynamoDB.
        (Default: ``False``)

        Optionally accepts an ``attributes`` parameter, which should be a
        list of fieldname to fetch. (Default: ``None``, which means all fields
        should be fetched)

        Returns an ``Item`` instance containing all the data for that record.

        Example::

            # A simple hash key.
            >>> john = users.get_item(username='johndoe')
            >>> john['first_name']
            'John'

            # A complex hash+range key.
            >>> john = users.get_item(username='johndoe', last_name='Doe')
            >>> john['first_name']
            'John'

            # A consistent read (assuming the data might have just changed).
            >>> john = users.get_item(username='johndoe', consistent=True)
            >>> john['first_name']
开发者ID:Revmetrix,项目名称:boto,代码行数:70,代码来源:table.py


注:本文中的boto.dynamodb2.layer1.DynamoDBConnection.get_item方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。