是否存在用于将批处理获取请求提交到AWS DynamoDB的Python API? [英] Is there a Python API for submitting batch get requests to AWS DynamoDB?
问题描述
boto3
软件包-亚马逊的Python官方AWS API包装器-对将项目批量上传到DynamoDB具有强大的支持。看起来像这样:
The package boto3
- Amazon's official AWS API wrapper for python - has great support for uploading items to DynamoDB in bulk. It looks like this:
db = boto3.resource("dynamodb", region_name = "my_region").Table("my_table")
with db.batch_writer() as batch:
for item in my_items:
batch.put_item(Item = item)
此处 my_items
是Python字典的列表,每个字典都必须具有表的主键( s)。这种情况并不完美-例如,没有安全机制可以阻止您超出吞吐量限制-但这仍然相当不错。
Here my_items
is a list of Python dictionaries each of which must have the table's primary key(s). The situation isn't perfect - for instance, there is no safety mechanism to prevent you from exceeding your throughput limits - but it's still pretty good.
但是,没有似乎是从数据库读取的任何副本。我能找到的最接近的是 DynamoDB.Client.batch_get_item()
,但是这里的API非常复杂。这是请求两个项目的样子:
However, there does not appear to be any counterpart for reading from the database. The closest I can find is DynamoDB.Client.batch_get_item()
, but here the API is extremely complicated. Here's what requesting two items looks like:
db_client = boto3.client("dynamodb", "my_region")
db_client.batch_get_item(
RequestItems = {
"my_table": {
"Keys": [
{"my_primary_key": {"S": "my_key1"}},
{"my_primary_key": {"S": "my_key2"}}
]
}
}
)
这是可以容忍的,但响应具有相同的问题:所有值都是字典,其键为数据类型( S
表示字符串, N
表示数字, M
表示映射等),并且不得不解析所有内容,这有点烦人。所以我的问题是:
This might be tolerable, but the response has the same problem: all values are dictionaries whose keys are data types ("S"
for string, "N"
for number, "M"
for mapping, etc.) and it is more than a little annoying to have to parse everything. So my questions are:
是否有本地的
boto3
支持从批处理读取DynamoDb,类似于上面的batch_writer
函数?
Is there any native
boto3
support for batch reading from DynamoDb, similar to thebatch_writer
function above?
失败了,
boto3
是否提供任何内置方法来自动反序列化对DynamoDB.Client.batch_get_item()
函数?
Does
boto3
provide any built-in way to automatically deserialize the responses to theDynamoDB.Client.batch_get_item()
function?
我还要补充一点函数 boto3.resource( dynamodb)。Table()。get_item()
具有我认为是正确的API,因为没有类型解析输入或输出所必需。因此,似乎这是开发人员的某种监督,我想我正在寻找一种解决方法。
I'll also add that the function boto3.resource("dynamodb").Table().get_item()
has what I would consider to be the "correct" API, in that no type-parsing is necessary for inputs or outputs. So it seems that this is some sort of oversight by the developers, and I suppose I'm looking for a workaround.
推荐答案
因此,值得庆幸的是,您可能会发现有用的东西-非常类似于 json
模块,该模块具有 json.dumps
和 json.loads
,boto3有一个类型模块,其中包含序列化程序和反序列化程序。请参见TypeSerializer / TypeDeserializer 。如果您查看源代码,则序列化/反序列化是递归的,并且对于您的用例而言应该是完美的。
So thankfully there is something that you might find useful - much like the json
module which has json.dumps
and json.loads
, boto3 has a types module that includes a serializer and deserializer. See TypeSerializer/TypeDeserializer. If you look at the source code, the serialization/deserialization is recursive and should be perfect for your use case.
注意:建议您使用二进制
/ 十进制
,而不是仅使用常规的旧python float / int进行往返转换。
Note: Its recommended that you use Binary
/Decimal
instead of just using a regular old python float/int for round trip conversions.
serializer = TypeSerializer()
serializer.serialize('awesome') # returns {'S' : 'awesome' }
deser = TypeDeserializer()
deser.deserialize({'S' : 'awesome'}) # returns u'awesome'
希望这会有所帮助!
这篇关于是否存在用于将批处理获取请求提交到AWS DynamoDB的Python API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!