Thêm một Global secondary index (GSI)

Thêm một Global secondary index (GSI)

  1. Truy cập AWS DynamoDB Console.
  • Click Tables
  • Click TravelBuddyTripSectors Add a Global Secondary Index (GSI)
  1. Chọn tab Indexes
  • Click Create Index Add a Global Secondary Index (GSI)
  1. Tại mục Partition Key, nhập originCity Add a Global Secondary Index (GSI)
  • Kéo màn hình xuống dưới, click Create index
  1. Kiểm tra Index đã được tạo thành công như hình dưới. Add a Global Secondary Index (GSI)
  2. Click Actions
  • Click Create item Add a Global Secondary Index (GSI)
  1. Tại mục date , hãy nhập thời gian tính bằng mili giây, đại diện cho một ngày trong tương lai. Ví dụ: 1655686284000 là ngày 20 tháng 6 năm 2022. Bạn có thể sử dụng https://www.epochconverter.com để chuyển đổi thời gian dễ dàng hơn.
  • Tại mục originCity, nhập Melbourne
  • Click Add new attribute
  • Click String Add a Global Secondary Index (GSI)
  1. Tại mục Attribute name của thuộc tính mới, điền destinationCity
  1. Bạn hãy download tập tin populate_tripsector.sh vào thư mục Downloads trong máy ảo Windows
  • Mở Command Prompt
  • Chạy lệnh dưới đây để chạy file populate_tripsector.sh
cd Downloads
populate_tripsector.sh

Add a Global Secondary Index (GSI)

  • Nội dung script như dưới đây, để các bạn tham khảo:
#!/bin/bash
echo Adding record to DynamoDB
aws dynamodb put-item --profile aws-lab-env --table-name TravelBuddyTripSectors --item '{"date": {"N": "1610175636000"},"originCity": {"S": "Melbourne"},"destinationCity": {"S": "Sydney"}}';
echo Adding record to DynamoDB
aws dynamodb put-item --profile aws-lab-env --table-name TravelBuddyTripSectors --item '{"date": {"N": "1610175633000"},"originCity": {"S": "Melbourne"},"destinationCity": {"S": "Perth"}}';
echo Adding record to DynamoDB
aws dynamodb put-item --profile aws-lab-env --table-name TravelBuddyTripSectors --item '{"date": {"N": "1610190033000"},"originCity": {"S": "Melbourne"},"destinationCity": {"S": "Canberra"}}';
echo Adding record to DynamoDB
aws dynamodb put-item --profile aws-lab-env --table-name TravelBuddyTripSectors --item '{"date": {"N": "1610294800000"},"originCity": {"S": "Sydney"},"destinationCity": {"S": "Perth"}}';
echo Adding record to DynamoDB
aws dynamodb put-item --profile aws-lab-env --table-name TravelBuddyTripSectors --item '{"date": {"N": "1610327200000"},"originCity": {"S": "Sydney"},"destinationCity": {"S": "Melbourne"}}';
echo Adding record to DynamoDB
aws dynamodb put-item --profile aws-lab-env --table-name TravelBuddyTripSectors --item '{"date": {"N": "1610356000000"},"originCity": {"S": "Perth"},"destinationCity": {"S": "Darwin"}}';
echo Adding record to DynamoDB
aws dynamodb put-item --profile aws-lab-env --table-name TravelBuddyTripSectors --item '{"date": {"N": "1610374000000"},"originCity": {"S": "Perth"},"destinationCity": {"S": "Singapore"}}';
echo Adding record to DynamoDB
aws dynamodb put-item --profile aws-lab-env --table-name TravelBuddyTripSectors --item '{"date": {"N": "1610402800000"},"originCity": {"S": "Perth"},"destinationCity": {"S": "Sydney"}}';
echo Adding record to DynamoDB
aws dynamodb put-item --profile aws-lab-env --table-name TravelBuddyTripSectors --item '{"date": {"N": "1610446000000"},"originCity": {"S": "Melbourne"},"destinationCity": {"S": "Sydney"}}';
echo Adding record to DynamoDB
aws dynamodb put-item --profile aws-lab-env --table-name TravelBuddyTripSectors --item '{"date": {"N": "1610500000000"},"originCity": {"S": "Darwin"},"destinationCity": {"S": "Sydney"}}';
echo Done!
  1. Kiểm tra các item trên DynamoDB table để chắc chắn rằng việc thực thi script đã thành công. Add a Global Secondary Index (GSI)