[英]How to query all rows of one column in DynamoDB?
I just work with AWS DynamoDB in a short of time. 我只是在短时间内使用AWS DynamoDB。 I am wondering how can I get the same result with this statement (without WHERE clause):
我想知道如何通过此语句(没有WHERE子句)获得相同的结果:
SELECT column1 FROM DynamoTable;
I tried (but failed) with: 我尝试了(但失败了):
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('DynamoTable')
from boto3.dynamodb.conditions import Key, Attr
resp = table.query(KeyConditionExpression=Key('column1'))
It requires Key().eq()
or Key().begin_with()
... 它需要
Key().eq()
或Key().begin_with()
...
I tried with resp = table.scan()
already, but the response data is too many fields while I only need column1
我已经尝试过
resp = table.scan()
,但是响应数据太多字段,而我只需要column1
Thanks. 谢谢。
You should definitely use Scan operation . 您绝对应该使用扫描操作 。 Check the documentation to implement it with python.
检查文档以使用python实现。
Regarding how to select just a specific attribute you could use: 关于如何仅选择特定属性,您可以使用:
import boto3
def getColumn1Items():
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('DynamoTable')
response = table.scan()
lst = []
for i in response['Items']:
lst.append(i['column1'])
return lst
You have to iterate over the the entire table and just fetch the column you need. 您必须遍历整个表并仅获取所需的列。
This lets you get the required column directly and you do not need to iterate over the whole dataset 这使您可以直接获取所需的列,而无需遍历整个数据集
import boto3
def getColumn1Items():
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('DynamoTable')
resp = table.scan(AttributesToGet=['column1'])
return resp['Items']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.