- DynamoDB 教程
- DynamoDB - 首頁
- DynamoDB - 概述
- DynamoDB - 基本概念
- DynamoDB - 環境
- DynamoDB - 操作工具
- DynamoDB - 資料型別
- DynamoDB - 建立表
- DynamoDB - 載入表
- DynamoDB - 查詢表
- DynamoDB - 刪除表
- DynamoDB - API 介面
- DynamoDB - 建立專案
- DynamoDB - 獲取專案
- DynamoDB - 更新專案
- DynamoDB - 刪除專案
- DynamoDB - 批次寫入
- DynamoDB - 批次檢索
- DynamoDB - 查詢
- DynamoDB - 掃描
- DynamoDB - 索引
- 全域性二級索引
- 本地二級索引
- DynamoDB - 聚合
- DynamoDB - 訪問控制
- DynamoDB - 許可權 API
- DynamoDB - 條件
- Web 身份聯合
- DynamoDB - 資料管道
- DynamoDB - 資料備份
- DynamoDB - 監控
- DynamoDB - CloudTrail
- DynamoDB - MapReduce
- DynamoDB - 表格活動
- DynamoDB - 錯誤處理
- DynamoDB - 最佳實踐
- DynamoDB 有用資源
- DynamoDB - 快速指南
- DynamoDB - 有用資源
- DynamoDB - 討論
DynamoDB - 建立專案
在 DynamoDB 中建立專案主要包括專案和屬性規範,以及指定條件的選項。每個專案都作為一個屬性集存在,每個屬性都有名稱並分配一個特定型別的值。
值型別包括標量、文件或集合。專案的尺寸限制為 400KB,並且可以容納任何數量的屬性,只要它們適合該限制即可。名稱和值的尺寸(二進位制和 UTF-8 長度)決定了專案的尺寸。使用簡短的屬性名稱有助於最小化專案尺寸。
注意 - 您必須指定所有主鍵屬性,主鍵只需要分割槽鍵;複合鍵需要分割槽鍵和排序鍵。
此外,請記住表沒有預定義的模式。您可以在一個表中儲存截然不同的資料集。
使用 GUI 控制檯、Java 或其他工具執行此任務。
如何使用 GUI 控制檯建立專案?
導航到控制檯。在左側的導航窗格中,選擇表格。選擇用作目標的表名,然後選擇專案選項卡,如下面的螢幕截圖所示。
選擇建立專案。建立專案螢幕提供了一個介面,用於輸入所需的屬性值。還必須輸入任何二級索引。
如果您需要更多屬性,請選擇訊息左側的操作選單。然後選擇追加和所需的資料型別。
輸入所有必要資訊後,選擇儲存以新增專案。
如何在專案建立中使用 Java?
在專案建立操作中使用 Java 包括建立 DynamoDB 類例項、Table 類例項、Item 類例項,並指定要建立的專案的 primary key 和屬性。然後使用 putItem 方法新增新專案。
示例
DynamoDB dynamoDB = new DynamoDB (new AmazonDynamoDBClient(
new ProfileCredentialsProvider()));
Table table = dynamoDB.getTable("ProductList");
// Spawn a related items list
List<Number> RELItems = new ArrayList<Number>();
RELItems.add(123);
RELItems.add(456);
RELItems.add(789);
//Spawn a product picture map
Map<String, String> photos = new HashMap<String, String>();
photos.put("Anterior", "http://xyz.com/products/101_front.jpg");
photos.put("Posterior", "http://xyz.com/products/101_back.jpg");
photos.put("Lateral", "http://xyz.com/products/101_LFTside.jpg");
//Spawn a product review map
Map<String, List<String>> prodReviews = new HashMap<String, List<String>>();
List<String> fiveStarRVW = new ArrayList<String>();
fiveStarRVW.add("Shocking high performance.");
fiveStarRVW.add("Unparalleled in its market.");
prodReviews.put("5 Star", fiveStarRVW);
List<String> oneStarRVW = new ArrayList<String>();
oneStarRVW.add("The worst offering in its market.");
prodReviews.put("1 Star", oneStarRVW);
// Generate the item
Item item = new Item()
.withPrimaryKey("Id", 101)
.withString("Nomenclature", "PolyBlaster 101")
.withString("Description", "101 description")
.withString("Category", "Hybrid Power Polymer Cutter")
.withString("Make", "Brand – XYZ")
.withNumber("Price", 50000)
.withString("ProductCategory", "Laser Cutter")
.withBoolean("Availability", true)
.withNull("Qty")
.withList("ItemsRelated", RELItems)
.withMap("Images", photos)
.withMap("Reviews", prodReviews);
// Add item to the table
PutItemOutcome outcome = table.putItem(item);
您還可以檢視以下更大的示例。
注意 - 以下示例可能假設之前已建立資料來源。在嘗試執行之前,請獲取支援庫並建立必要的資料來源(具有所需特徵的表或其他引用的源)。
以下示例還使用 Eclipse IDE、AWS 憑證檔案以及 Eclipse AWS Java 專案中的 AWS 工具包。
package com.amazonaws.codesamples.document;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.DeleteItemOutcome;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome;
import com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec;
import com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec;
import com.amazonaws.services.dynamodbv2.document.utils.NameMap;
import com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
import com.amazonaws.services.dynamodbv2.model.ReturnValue;
public class CreateItemOpSample {
static DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient (
new ProfileCredentialsProvider()));
static String tblName = "ProductList";
public static void main(String[] args) throws IOException {
createItems();
retrieveItem();
// Execute updates
updateMultipleAttributes();
updateAddNewAttribute();
updateExistingAttributeConditionally();
// Item deletion
deleteItem();
}
private static void createItems() {
Table table = dynamoDB.getTable(tblName);
try {
Item item = new Item()
.withPrimaryKey("ID", 303)
.withString("Nomenclature", "Polymer Blaster 4000")
.withStringSet( "Manufacturers",
new HashSet<String>(Arrays.asList("XYZ Inc.", "LMNOP Inc.")))
.withNumber("Price", 50000)
.withBoolean("InProduction", true)
.withString("Category", "Laser Cutter");
table.putItem(item);
item = new Item()
.withPrimaryKey("ID", 313)
.withString("Nomenclature", "Agitatatron 2000")
.withStringSet( "Manufacturers",
new HashSet<String>(Arrays.asList("XYZ Inc,", "CDE Inc.")))
.withNumber("Price", 40000)
.withBoolean("InProduction", true)
.withString("Category", "Agitator");
table.putItem(item);
} catch (Exception e) {
System.err.println("Cannot create items.");
System.err.println(e.getMessage());
}
}
}
廣告