AWS Command Line Interface v2를 사용한 DynamoDB 로컬 보조 인덱스 작업 - Amazon DynamoDB

AWS Command Line Interface v2를 사용한 DynamoDB 로컬 보조 인덱스 작업

다음 코드 예제에서는 로컬 보조 인덱스가 있는 테이블을 생성하고 쿼리하는 방법을 보여줍니다.

  • 로컬 보조 인덱스(LSI)가 있는 테이블을 생성합니다.

  • 다양한 프로젝션 유형의 여러 LSI가 있는 테이블을 생성합니다.

  • LSI를 사용하여 데이터를 쿼리합니다.

Bash
Bash 스크립트와 함께 AWS CLI사용

로컬 보조 인덱스가 있는 테이블을 생성합니다.

# Create a table with a Local Secondary Index aws dynamodb create-table \ --table-name CustomerOrders \ --attribute-definitions \ AttributeName=CustomerID,AttributeType=S \ AttributeName=OrderID,AttributeType=S \ AttributeName=OrderDate,AttributeType=S \ --key-schema \ AttributeName=CustomerID,KeyType=HASH \ AttributeName=OrderID,KeyType=RANGE \ --local-secondary-indexes \ "IndexName=OrderDateIndex,\ KeySchema=[{AttributeName=CustomerID,KeyType=HASH},{AttributeName=OrderDate,KeyType=RANGE}],\ Projection={ProjectionType=ALL}" \ --billing-mode PAY_PER_REQUEST

여러 LSI가 있는 테이블을 생성합니다.

# Create a table with multiple Local Secondary Indexes aws dynamodb create-table \ --table-name CustomerDetails \ --attribute-definitions \ AttributeName=CustomerID,AttributeType=S \ AttributeName=Name,AttributeType=S \ AttributeName=Email,AttributeType=S \ AttributeName=RegistrationDate,AttributeType=S \ --key-schema \ AttributeName=CustomerID,KeyType=HASH \ AttributeName=Name,KeyType=RANGE \ --local-secondary-indexes \ "[ { \"IndexName\": \"EmailIndex\", \"KeySchema\": [ {\"AttributeName\":\"CustomerID\",\"KeyType\":\"HASH\"}, {\"AttributeName\":\"Email\",\"KeyType\":\"RANGE\"} ], \"Projection\": {\"ProjectionType\":\"INCLUDE\",\"NonKeyAttributes\":[\"Address\",\"Phone\"]} }, { \"IndexName\": \"RegistrationIndex\", \"KeySchema\": [ {\"AttributeName\":\"CustomerID\",\"KeyType\":\"HASH\"}, {\"AttributeName\":\"RegistrationDate\",\"KeyType\":\"RANGE\"} ], \"Projection\": {\"ProjectionType\":\"KEYS_ONLY\"} } ]" \ --billing-mode PAY_PER_REQUEST

LSI를 사용하여 데이터를 쿼리합니다.

# Query the OrderDateIndex LSI aws dynamodb query \ --table-name CustomerOrders \ --index-name OrderDateIndex \ --key-condition-expression "CustomerID = :custId AND OrderDate BETWEEN :date1 AND :date2" \ --expression-attribute-values '{ ":custId": {"S": "C1"}, ":date1": {"S": "2023-01-01"}, ":date2": {"S": "2023-02-01"} }' # Query with a filter expression aws dynamodb query \ --table-name CustomerOrders \ --index-name OrderDateIndex \ --key-condition-expression "CustomerID = :custId" \ --filter-expression "Amount > :amount" \ --expression-attribute-values '{ ":custId": {"S": "C1"}, ":amount": {"N": "150"} }'
  • API 세부 정보는 AWS CLI 명령 참조의 다음 토픽을 참조하세요.

AWS SDK 개발자 가이드 및 코드 예시의 전체 목록은 AWS SDK와 함께 DynamoDB 사용 섹션을 참조하세요. 이 주제에는 시작하기에 대한 정보와 이전 SDK 버전에 대한 세부 정보도 포함되어 있습니다.