query로 dyanmoDB의 데이터를 가져오기 위해서는 우선 partition key가 필수이다. partition key로 얻은 결과에서 sort key를 기준으로 서브셋을 구할 수 있다.
import boto3
from boto3.dynamodb.conditions import Key
dynamodb = boto3.resource('dynamodb', region_name='ap-northeast-2')
table = dynamodb.Table('TABLE_NAME')
response = table.query(ProjectionExpression="key, dt, cumuyield",
KeyConditionExpression=Key('key').eq(my_key) & Key('dt').gte(date))
여기서는 key
가 partition key이고 dt
가 sort key이다.
KeyConditionExpression
으로 각 Key
에 다음과 같은 메소드들을 적용해서 검색할 수 있다.
eq
= equal toge
= greater thangte
= greater than or equal tole
= less thanlte
= less than or equal tobetween
begins_with
또한 결과로 원하는 어트리뷰트를 특정하고 싶다면 ProjectionExpression
를 추가해주면 된다.