简体   繁体   中英

How to query all rows of one column in DynamoDB?

I just work with AWS DynamoDB in a short of time. I am wondering how can I get the same result with this statement (without WHERE clause):

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() ...

I tried with resp = table.scan() already, but the response data is too many fields while I only need column1

Thanks.

You should definitely use Scan operation . Check the documentation to implement it with 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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM