[boto3] DynamoDB Query

rang-dev·2022년 2월 9일
0

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 to
  • ge = greater than
  • gte = greater than or equal to
  • le = less than
  • lte = less than or equal to
  • between
  • begins_with

또한 결과로 원하는 어트리뷰트를 특정하고 싶다면 ProjectionExpression를 추가해주면 된다.

참고

profile
지금 있는 곳에서, 내가 가진 것으로, 할 수 있는 일을 하기 🐢

0개의 댓글