◇ 공부 기록용으로 작성하였으니 틀린점, 피드백 주시면 감사하겠습니다 ◇
GetItem
과 BatchGetItem
은 DynamoDB에서 데이터베이스의 항목을 조회하는 데 사용되는 API이다.
TL;DR
GetItem
: 하나의 항목을 가져올 때 사용.
BatchGetItem
: 여러 항목을 한 번에 가져올 때 사용.
GetItem
GetItem
은 DynamoDB에서 단일 항목(아이템)을 조회하는 API이다.
요청 시 지정한 기본 키(primary key)로 단일 아이템을 검색한다.
aws dynamodb get-item \
--table-name YourTableName \
--key '{"YourPrimaryKeyAttributeName": {"S": "YourPrimaryKeyValue"}}'
https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html
GetItem - Amazon DynamoDB
Tokens that begin with the : character are expression attribute values, which are placeholders for the actual value at runtime.
docs.aws.amazon.com
BatchGetItem
BatchGetItem
은 여러 아이템을 동시에 조회하는 API이다.
여러 테이블에서 최대 100개 아이템까지 요청할 수 있다.
한 번의 요청으로 가져오기 때문에 효율적이다.
🛑 제한 사항: 최대 아이템 = 100개, 총 데이터 크기 = 16MB
aws dynamodb batch-get-item \
--request-items '{
"YourTableName": {
"Keys": [
{"YourPrimaryKeyAttributeName": {"S": "Value1"}},
{"YourPrimaryKeyAttributeName": {"S": "Value2"}}
]
}
}'
https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchGetItem.html
BatchGetItem - Amazon DynamoDB
If DynamoDB returns any unprocessed items, you should retry the batch operation on those items. However, we strongly recommend that you use an exponential backoff algorithm. If you retry the batch operation immediately, the underlying read or write request
docs.aws.amazon.com
사용 예시
데이터 예시: DynamoDB의 "Users" 테이블
UserID | Name | Age |
user1 | John | 30 |
user2 | Alice | 25 |
user3 | Bob | 35 |
user4 | Carol | 40 |
GetItem
사용 예시:
UserID가 "user1"인 사용자의 정보를 가져오기
import boto3
# DynamoDB 클라이언트 생성
dynamodb = boto3.client('dynamodb')
# 'Users' 테이블에서 UserID가 'user1'인 항목을 조회
response = dynamodb.get_item(
TableName='Users',
Key={
'UserID': {'S': 'user1'}
}
)
# 반환된 항목 출력
if 'Item' in response:
print("User data:", response['Item'])
else:
print("Item not found")
출력 예시
User data: {'UserID': {'S': 'user1'}, 'Name': {'S': 'John'}, 'Age': {'N': '30'}}
BatchGetItem
사용 예시:
UserID가 "user1", "user2", "user3"인 사용자를 한 번에 조회하기
import boto3
# DynamoDB 클라이언트 생성
dynamodb = boto3.client('dynamodb')
# 여러 항목을 한 번에 조회
response = dynamodb.batch_get_item(
RequestItems={
'Users': {
'Keys': [
{'UserID': {'S': 'user1'}},
{'UserID': {'S': 'user2'}},
{'UserID': {'S': 'user3'}}
]
}
}
)
# 반환된 항목 출력
if 'Responses' in response:
for item in response['Responses']['Users']:
print(item)
else:
print("Items not found")
출력 예시
{'UserID': {'S': 'user1'}, 'Name': {'S': 'John'}, 'Age': {'N': '30'}}
{'UserID': {'S': 'user2'}, 'Name': {'S': 'Alice'}, 'Age': {'N': '25'}}
{'UserID': {'S': 'user3'}, 'Name': {'S': 'Bob'}, 'Age': {'N': '35'}}
🤔 문제
개발자가 Amazon DynamoDB를 사용하는 애플리케이션을 구축하고 있습니다. 개발자는 데이터베이스에서 여러 개의 특정 항목을 단일 API 호출로 검색하고자 합니다.
데이터베이스에 미치는 최소한의 영향으로 이러한 요구 사항을 충족할 수 있는 DynamoDB API 호출은 무엇인가요?
- BatchGetItem
- GetItem
- Scan
- Query
정답
정답. 1번
'클라우드(AWS)' 카테고리의 다른 글
[AWS] S3 버킷을 사용하여 웹사이트를 호스팅하는 방법 쉽게 정리 (0) | 2025.01.20 |
---|---|
[AWS] Lambda@Edge와 CloudFront Function란? 차이점까지 쉽게 정리 (1) | 2024.11.23 |
[AWS] Lambda Layer란? 쉽게 정리 (Lambda에서 필요한 라이브러리 사용하기) (1) | 2024.11.09 |
[AWS] API Gateway 개발 환경 별로 (예: Dev, Prod) Stages 설정하는 법 (1) | 2024.10.29 |
[AWS] Amplify란? 쉽게 정리 (Full Stack 애플리케이션 개발을 쉽게 만들어주는 서비스) (1) | 2024.10.22 |