在给定主分区键值列表的情况下

在给定主分区键值列表的情况下

本文介绍了在给定主分区键值列表的情况下,如何一次批处理多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,所以我有一个带有主分区键列 foo_id 的dynamodb表,没有主排序键。我有一个 foo_id 值的列表,并希望获得与此ID列表相关的观察结果。

So, so I have a dynamodb table with a primary partition key column, foo_id and no primary sort key. I have a list of foo_id values, and want to get the observations associated with this list of ids.

I想出做到这一点的最佳方法(?)是使用 batch_get_item(),但这对我来说不起作用。

I figured the best way to do this (?) is to use batch_get_item(), but it's not working out for me.

    # python code
    import boto3
    client = boto3.client('dynamodb')

    # ppk_values = list of `foo_id` values (strings) (< 100 in this example)
    x = client.batch_get_item(
        RequestItems={
            'my_table_name':
                {'Keys': [{'foo_id': {'SS': [id for id in ppk_values]}}]}
        })

我使用的是 SS ,因为我传递了一个字符串列表( foo_id 值的列表),但我得到:

I'm using SS because I'm passing a list of strings (list of foo_id values), but I'm getting:

ClientError: An error occurred (ValidationException) when calling the
BatchGetItem operation: The provided key element does not match the
schema


$ b不匹配$ b

所以我认为这意味着它在考虑 foo_id 包含列表值而不是字符串值,这是错误的。

So I assume that means it's thinking foo_id contains list values instead of string values, which is wrong.

->这种解释正确吗?批量查询一堆主分区键值的最佳方法是什么?

推荐答案

应给定键如下所述。

基本上,您可以将DynamoDB String数据类型与String(即不与SS)进行比较。每个项目都单独处理。 与查询中的SQL不相似

Basically, you can compare the DynamoDB String datatype with String (i.e. not with SS). Each item is handled separately. It is not similar to SQL in query.

'Keys': [
            {
                'foo_id': key1
            },
            {
                'foo_id': key2
            }
],

示例代码:-

您可能需要更改表名和键值。

You may need to change the table name and key values.

from __future__ import print_function # Python 2/3 compatibility
import boto3
import json
import decimal
from boto3.dynamodb.conditions import Key, Attr
from botocore.exceptions import ClientError

# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            if o % 1 > 0:
                return float(o)
            else:
                return int(o)
        return super(DecimalEncoder, self).default(o)

dynamodb = boto3.resource("dynamodb", region_name='us-west-2', endpoint_url="http://localhost:8000")

email1 = "[email protected]"
email2 = "[email protected]"

try:
    response = dynamodb.batch_get_item(
        RequestItems={
            'users': {
                'Keys': [
                    {
                        'email': email1
                    },
                    {
                        'email': email2
                    },
                ],
                'ConsistentRead': True
            }
        },
        ReturnConsumedCapacity='TOTAL'
    )
except ClientError as e:
    print(e.response['Error']['Message'])
else:
    item = response['Responses']
    print("BatchGetItem succeeded:")
    print(json.dumps(item, indent=4, cls=DecimalEncoder))

这篇关于在给定主分区键值列表的情况下,如何一次批处理多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 00:17