DynamoDB - Bảng tải

Việc tải bảng thường bao gồm việc tạo tệp nguồn, đảm bảo tệp nguồn tuân theo cú pháp tương thích với DynamoDB, gửi tệp nguồn đến đích, sau đó xác nhận tập hợp thành công.

Sử dụng giao diện điều khiển GUI, Java hoặc tùy chọn khác để thực hiện tác vụ.

Tải bảng bằng GUI Console

Tải dữ liệu bằng cách sử dụng kết hợp dòng lệnh và bảng điều khiển. Bạn có thể tải dữ liệu theo nhiều cách, một số cách như sau:

  • Bàn điều khiển
  • Dòng lệnh
  • Mã và cả
  • Đường ống dữ liệu (một tính năng được thảo luận ở phần sau của hướng dẫn)

Tuy nhiên, đối với tốc độ, ví dụ này sử dụng cả shell và console. Đầu tiên, tải dữ liệu nguồn vào đích theo cú pháp sau:

aws dynamodb batch-write-item -–request-items file://[filename]

Ví dụ -

aws dynamodb batch-write-item -–request-items file://MyProductData.json

Xác minh sự thành công của hoạt động bằng cách truy cập bảng điều khiển tại -

https://console.aws.amazon.com/dynamodb

Chọn Tables từ ngăn dẫn hướng và chọn bảng đích từ danh sách bảng.

Chọn Itemsđể kiểm tra dữ liệu bạn đã sử dụng để điền vào bảng. Lựa chọnCancel để quay lại danh sách bảng.

Tải bảng bằng Java

Sử dụng Java bằng cách tạo một tệp nguồn trước. Tệp nguồn của chúng tôi sử dụng định dạng JSON. Mỗi sản phẩm có hai thuộc tính khóa chính (ID và Danh pháp) và một bản đồ JSON (Stat) -

[ 
   { 
      "ID" : ... , 
      "Nomenclature" : ... , 
      "Stat" : { ... }
   }, 
   { 
      "ID" : ... , 
      "Nomenclature" : ... , 
      "Stat" : { ... } 
   }, 
    ... 
]

Bạn có thể xem lại ví dụ sau:

{ 
   "ID" : 122, 
   "Nomenclature" : "Particle Blaster 5000", 
   "Stat" : { 
      "Manufacturer" : "XYZ Inc.", 
      "sales" : "1M+", 
      "quantity" : 500, 
      "img_src" : "http://www.xyz.com/manuals/particleblaster5000.jpg", 
      "description" : "A laser cutter used in plastic manufacturing." 
   } 
}

Bước tiếp theo là đặt tệp vào thư mục được ứng dụng của bạn sử dụng.

Java chủ yếu sử dụng putItempath methods để thực hiện tải.

Bạn có thể xem lại ví dụ mã sau để xử lý tệp và tải tệp đó -

import java.io.File;
import java.util.Iterator;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.Table;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.ObjectNode;

public class ProductsLoadData {  
   public static void main(String[] args) throws Exception {  
      AmazonDynamoDBClient client = new AmazonDynamoDBClient() 
         .withEndpoint("http://localhost:8000");  
      
      DynamoDB dynamoDB = new DynamoDB(client);  
      Table table = dynamoDB.getTable("Products");  
      JsonParser parser = new JsonFactory() 
         .createParser(new File("productinfo.json"));  
      
      JsonNode rootNode = new ObjectMapper().readTree(parser); 
      Iterator<JsonNode> iter = rootNode.iterator();  
      ObjectNode currentNode;  
      
      while (iter.hasNext()) { 
         currentNode = (ObjectNode) iter.next();  
         int ID = currentNode.path("ID").asInt(); 
         String Nomenclature = currentNode.path("Nomenclature").asText();  
         
         try { 
            table.putItem(new Item() 
               .withPrimaryKey("ID", ID, "Nomenclature", Nomenclature) 
               .withJSON("Stat", currentNode.path("Stat").toString()));
            System.out.println("Successful load: " + ID + " " + Nomenclature);  
         } catch (Exception e) {
            System.err.println("Cannot add product: " + ID + " " + Nomenclature);
            System.err.println(e.getMessage()); 
            break; 
         } 
      } 
      parser.close(); 
   } 
}