简体   繁体   English

如何在DynamoDB中查询一列的所有行?

[英]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.

相关问题 使用 python 从 DynamoDB 查询所有行 - Query all rows from DynamoDB using python 如何更新SqlAlchemy中所有行的一列数据? - How to update data of one column for all rows in SqlAlchemy? Openpyxl:遍历一列的所有行 - Openpyxl: iterate through all rows of one column 多行在一个列中共享一个值,如何将所有这些行放到一个行中? - Multiple rows share a value in a column, how do I put all of these rows into one single row? 如何查询表的所有行 - How to query all rows of a table 如何将一列中的行值与组中不同列中的所有其他行进行比较? - How do I compare a row value in one column to all other rows in a different column within a group? 如何进行查询以过滤其中一列等于同一表的另一列的行? - How to make a query that filters rows in which one column equals another one of the same table? 如何在熊猫数据框中找到与另一列中的多个值相对应的列中具有值的所有行? - How can I find all rows with a value in one column which corresponds to more than one value in another column in a pandas dataframe? Sqlalchemy单个查询一个表中一列的多行 - Sqlalchemy single query for multiple rows from one column in one table 当所有行达到1时如何设置第0列中的所有行 - How to set all rows in column 0 when all rows reach 1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM