- 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 - 本地二級索引
某些應用程式僅使用主鍵執行查詢,但某些情況下可以使用備用排序鍵。透過建立單個或多個本地二級索引,允許您的應用程式進行選擇。
複雜的資料訪問需求,例如組合數百萬個專案,需要執行更高效的查詢/掃描。本地二級索引為分割槽鍵值提供備用排序鍵。它們還儲存所有或部分表屬性的副本。它們按表分割槽鍵組織資料,但使用不同的排序鍵。
使用本地二級索引無需進行整個表掃描,並且可以使用排序鍵進行簡單快速的查詢。
所有本地二級索引必須滿足某些條件 -
- 分割槽鍵與源表分割槽鍵相同。
- 僅一個標量屬性的排序鍵。
- 源表排序鍵的投影充當非鍵屬性。
所有本地二級索引自動儲存來自父表的 partition 和 sort 鍵。在查詢中,這意味著有效地收集投影屬性,以及檢索未投影的屬性。
本地二級索引的儲存限制仍然是每個分割槽鍵值 10GB,其中包括所有表專案和共享分割槽鍵值的索引專案。
投影屬性
某些操作由於複雜性而需要過多的讀取/獲取。這些操作可能會消耗大量吞吐量。投影允許您透過隔離這些屬性來避免代價高昂的獲取並執行豐富的查詢。請記住,投影包含複製到二級索引中的屬性。
建立二級索引時,您指定要投影的屬性。回顧 DynamoDB 提供的三個選項:**KEYS_ONLY、INCLUDE 和 ALL**。
在選擇投影中的某些屬性時,請考慮相關的成本權衡 -
如果您僅投影一小部分必要的屬性,則可以大幅降低儲存成本。
如果您投影經常訪問的非鍵屬性,則可以使用儲存成本抵消掃描成本。
如果您投影大部分或所有非鍵屬性,這將最大限度地提高靈活性並降低吞吐量(無檢索);但是,儲存成本會上升。
如果您為頻繁寫入/更新和不頻繁查詢投影 KEYS_ONLY,它可以最大限度地減少大小,但保留查詢準備。
本地二級索引建立
使用 CreateTable 的 **LocalSecondaryIndex** 引數建立單個或多個本地二級索引。您必須為排序鍵指定一個非鍵屬性。在表建立時,您建立本地二級索引。刪除時,您刪除這些索引。
具有本地二級索引的表每個分割槽鍵值的大小必須限制在 10GB 以內,但可以儲存任意數量的專案。
本地二級索引查詢和掃描
當索引中的多個專案共享排序鍵值時,本地二級索引上的查詢操作將返回所有具有匹配分割槽鍵值的專案。匹配的專案不會以特定順序返回。本地二級索引的查詢使用最終一致性或強一致性,強一致性讀取提供最新值。
掃描操作返回所有本地二級索引資料。掃描要求您提供表和索引名稱,並允許使用篩選表示式來丟棄資料。
專案寫入
在建立本地二級索引時,您指定排序鍵屬性及其資料型別。當您寫入專案時,如果專案定義索引鍵的屬性,則其型別必須與鍵架構的資料型別匹配。
DynamoDB 對錶專案和本地二級索引專案沒有施加一對一關係的要求。具有多個本地二級索引的表比具有較少索引的表具有更高的寫入成本。
本地二級索引中的吞吐量注意事項
查詢的讀取容量消耗取決於資料訪問的性質。查詢使用最終一致性或強一致性,強一致性讀取使用一個單元,而最終一致性讀取使用半個單元。
結果限制包括最大 1MB 的大小。結果大小來自匹配的索引專案大小(四捨五入到最接近的 4KB)和匹配的表專案大小(也四捨五入到最接近的 4KB)的總和。
寫入容量消耗保持在預配的單元內。透過查詢表寫入中消耗的單元和更新索引中消耗的單元的總和來計算總預配成本。
您還可以考慮影響成本的關鍵因素,其中一些可能是 -
當您寫入定義索引屬性的專案或更新專案以定義未定義的索引屬性時,將發生一次寫入操作。
當表更新更改索引鍵屬性值時,將發生兩次寫入以刪除然後新增專案。
當寫入導致索引屬性的刪除時,將發生一次寫入以刪除舊的專案投影。
當專案在更新之前或之後不存在於索引中時,不會發生寫入。
本地二級索引儲存
在表專案寫入時,DynamoDB 會自動將正確的屬性集複製到所需的本地二級索引。這將向您的帳戶收費。使用的空間來自表主鍵位元組大小、索引鍵屬性位元組大小、任何存在的投影屬性位元組大小以及每個索引專案 100 位元組開銷的總和。
儲存估算透過估算平均索引專案大小並乘以表專案數量來獲得。
使用 Java 處理本地二級索引
首先建立 DynamoDB 類例項來建立本地二級索引。然後,使用必要的請求資訊建立 CreateTableRequest 類例項。最後,使用 createTable 方法。
示例
DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient(
new ProfileCredentialsProvider()));
String tableName = "Tools";
CreateTableRequest createTableRequest = new
CreateTableRequest().withTableName(tableName);
//Provisioned Throughput
createTableRequest.setProvisionedThroughput (
new ProvisionedThroughput()
.withReadCapacityUnits((long)5)
.withWriteCapacityUnits(( long)5));
//Attributes
ArrayList<AttributeDefinition> attributeDefinitions =
new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("Make")
.withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("Model")
.withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("Line")
.withAttributeType("S"));
createTableRequest.setAttributeDefinitions(attributeDefinitions);
//Key Schema
ArrayList<KeySchemaElement> tableKeySchema = new
ArrayList<KeySchemaElement>();
tableKeySchema.add(new KeySchemaElement()
.withAttributeName("Make")
.withKeyType(KeyType.HASH)); //Partition key
tableKeySchema.add(new KeySchemaElement()
.withAttributeName("Model")
.withKeyType(KeyType.RANGE)); //Sort key
createTableRequest.setKeySchema(tableKeySchema);
ArrayList<KeySchemaElement> indexKeySchema = new
ArrayList<KeySchemaElement>();
indexKeySchema.add(new KeySchemaElement()
.withAttributeName("Make")
.withKeyType(KeyType.HASH)); //Partition key
indexKeySchema.add(new KeySchemaElement()
.withAttributeName("Line")
.withKeyType(KeyType.RANGE)); //Sort key
Projection projection = new Projection()
.withProjectionType(ProjectionType.INCLUDE);
ArrayList<String> nonKeyAttributes = new ArrayList<String>();
nonKeyAttributes.add("Type");
nonKeyAttributes.add("Year");
projection.setNonKeyAttributes(nonKeyAttributes);
LocalSecondaryIndex localSecondaryIndex = new LocalSecondaryIndex()
.withIndexName("ModelIndex")
.withKeySchema(indexKeySchema)
.withProjection(p rojection);
ArrayList<LocalSecondaryIndex> localSecondaryIndexes = new
ArrayList<LocalSecondaryIndex>();
localSecondaryIndexes.add(localSecondaryIndex);
createTableRequest.setLocalSecondaryIndexes(localSecondaryIndexes);
Table table = dynamoDB.createTable(createTableRequest);
System.out.println(table.getDescription());
使用 describe 方法檢索有關本地二級索引的資訊。只需建立一個 DynamoDB 類例項,建立一個 Table 類例項,並將表傳遞給 describe 方法。
示例
DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient(
new ProfileCredentialsProvider()));
String tableName = "Tools";
Table table = dynamoDB.getTable(tableName);
TableDescription tableDescription = table.describe();
List<LocalSecondaryIndexDescription> localSecondaryIndexes =
tableDescription.getLocalSecondaryIndexes();
Iterator<LocalSecondaryIndexDescription> lsiIter =
localSecondaryIndexes.iterator();
while (lsiIter.hasNext()) {
LocalSecondaryIndexDescription lsiDescription = lsiIter.next();
System.out.println("Index info " + lsiDescription.getIndexName() + ":");
Iterator<KeySchemaElement> kseIter = lsiDescription.getKeySchema().iterator();
while (kseIter.hasNext()) {
KeySchemaElement kse = kseIter.next();
System.out.printf("\t%s: %s\n", kse.getAttributeName(), kse.getKeyType());
}
Projection projection = lsiDescription.getProjection();
System.out.println("\tProjection type: " + projection.getProjectionType());
if (projection.getProjectionType().toString().equals("INCLUDE")) {
System.out.println("\t\tNon-key projected attributes: " +
projection.getNonKeyAttributes());
}
}
透過使用與表查詢相同的步驟執行查詢。只需建立一個 DynamoDB 類例項、一個 Table 類例項、一個 Index 類例項、一個查詢物件,並利用 query 方法。
示例
DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient(
new ProfileCredentialsProvider()));
String tableName = "Tools";
Table table = dynamoDB.getTable(tableName);
Index index = table.getIndex("LineIndex");
QuerySpec spec = new QuerySpec()
.withKeyConditionExpression("Make = :v_make and Line = :v_line")
.withValueMap(new ValueMap()
.withString(":v_make", "Depault")
.withString(":v_line", "SuperSawz"));
ItemCollection<QueryOutcome> items = index.query(spec);
Iterator<Item> itemsIter = items.iterator();
while (itemsIter.hasNext()) {
Item item = itemsIter.next();
System.out.println(item.toJSONPretty());
}
您還可以檢視以下示例。
**注意** - 以下示例可能假設存在先前建立的資料來源。在嘗試執行之前,請獲取支援庫並建立必要的資料來源(具有所需特徵的表或其他引用的源)。
以下示例還使用 Eclipse IDE、AWS 憑證檔案以及 Eclipse AWS Java 專案中的 AWS 工具包。
示例
import java.util.ArrayList;
import java.util.Iterator;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Index;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.ItemCollection;
import com.amazonaws.services.dynamodbv2.document.PutItemOutcome;
import com.amazonaws.services.dynamodbv2.document.QueryOutcome;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.document.spec.QuerySpec;
import com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex;
import com.amazonaws.services.dynamodbv2.model.Projection;
import com.amazonaws.services.dynamodbv2.model.ProjectionType;
import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput;
import com.amazonaws.services.dynamodbv2.model.ReturnConsumedCapacity;
import com.amazonaws.services.dynamodbv2.model.Select;
public class LocalSecondaryIndexSample {
static DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient(
new ProfileCredentialsProvider()));
public static String tableName = "ProductOrders";
public static void main(String[] args) throws Exception {
createTable();
query(null);
query("IsOpenIndex");
query("OrderCreationDateIndex");
}
public static void createTable() {
CreateTableRequest createTableRequest = new CreateTableRequest()
.withTableName(tableName)
.withProvisionedThroughput(new ProvisionedThroughput()
.withReadCapacityUnits((long) 1)
.withWriteCapacityUnits((long) 1));
// Table partition and sort keys attributes
ArrayList<AttributeDefinition> attributeDefinitions = new
ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("CustomerID")
.withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("OrderID")
.withAttributeType("N"));
// Index primary key attributes
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("OrderDate")
.withAttributeType("N"));
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("OpenStatus")
.withAttributeType("N"));
createTableRequest.setAttributeDefinitions(attributeDefinitions);
// Table key schema
ArrayList<KeySchemaElement> tableKeySchema = new
ArrayList<KeySchemaElement>();
tableKeySchema.add(new KeySchemaElement()
.withAttributeName("CustomerID")
.withKeyType(KeyType.HASH)); //Partition key
tableKeySchema.add(new KeySchemaElement()
.withAttributeName("OrderID")
.withKeyType(KeyType.RANGE)); //Sort key
createTableRequest.setKeySchema(tableKeySchema);
ArrayList<LocalSecondaryIndex> localSecondaryIndexes = new
ArrayList<LocalSecondaryIndex>();
// OrderDateIndex
LocalSecondaryIndex orderDateIndex = new LocalSecondaryIndex()
.withIndexName("OrderDateIndex");
// OrderDateIndex key schema
ArrayList<KeySchemaElement> indexKeySchema = new
ArrayList<KeySchemaElement>();
indexKeySchema.add(new KeySchemaElement()
.withAttributeName("CustomerID")
.withKeyType(KeyType.HASH)); //Partition key
indexKeySchema.add(new KeySchemaElement()
.withAttributeName("OrderDate")
.withKeyType(KeyType.RANGE)); //Sort key
orderDateIndex.setKeySchema(indexKeySchema);
// OrderCreationDateIndex projection w/attributes list
Projection projection = new Projection()
.withProjectionType(ProjectionType.INCLUDE);
ArrayList<String> nonKeyAttributes = new ArrayList<String>();
nonKeyAttributes.add("ProdCat");
nonKeyAttributes.add("ProdNomenclature");
projection.setNonKeyAttributes(nonKeyAttributes);
orderCreationDateIndex.setProjection(projection);
localSecondaryIndexes.add(orderDateIndex);
// IsOpenIndex
LocalSecondaryIndex isOpenIndex = new LocalSecondaryIndex()
.withIndexName("IsOpenIndex");
// OpenStatusIndex key schema
indexKeySchema = new ArrayList<KeySchemaElement>();
indexKeySchema.add(new KeySchemaElement()
.withAttributeName("CustomerID")
.withKeyType(KeyType.HASH)); //Partition key
indexKeySchema.add(new KeySchemaElement()
.withAttributeName("OpenStatus")
.withKeyType(KeyType.RANGE)); //Sort key
// OpenStatusIndex projection
projection = new Projection() .withProjectionType(ProjectionType.ALL);
OpenStatusIndex.setKeySchema(indexKeySchema);
OpenStatusIndex.setProjection(projection);
localSecondaryIndexes.add(OpenStatusIndex);
// Put definitions in CreateTable request
createTableRequest.setLocalSecondaryIndexes(localSecondaryIndexes);
System.out.println("Spawning table " + tableName + "...");
System.out.println(dynamoDB.createTable(createTableRequest));
// Pause for ACTIVE status
System.out.println("Waiting for ACTIVE table:" + tableName);
try {
Table table = dynamoDB.getTable(tableName);
table.waitForActive();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void query(String indexName) {
Table table = dynamoDB.getTable(tableName);
System.out.println("\n*************************************************\n");
System.out.println("Executing query on" + tableName);
QuerySpec querySpec = new QuerySpec()
.withConsistentRead(true)
.withScanIndexForward(true)
.withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
if (indexName == "OpenStatusIndex") {
System.out.println("\nEmploying index: '" + indexName
+ "' open orders for this customer.");
System.out.println(
"Returns only user-specified attribute list\n");
Index index = table.getIndex(indexName);
querySpec.withKeyConditionExpression("CustomerID = :v_custmid and
OpenStatus = :v_openstat")
.withValueMap(new ValueMap()
.withString(":v_custmid", "jane@sample.com")
.withNumber(":v_openstat", 1));
querySpec.withProjectionExpression(
"OrderDate, ProdCat, ProdNomenclature, OrderStatus");
ItemCollection<QueryOutcome> items = index.query(querySpec);
Iterator<Item> iterator = items.iterator();
System.out.println("Printing query results...");
while (iterator.hasNext()) {
System.out.println(iterator.next().toJSONPretty());
}
} else if (indexName == "OrderDateIndex") {
System.out.println("\nUsing index: '" + indexName
+ "': this customer's orders placed after 05/22/2016.");
System.out.println("Projected attributes are returned\n");
Index index = table.getIndex(indexName);
querySpec.withKeyConditionExpression("CustomerID = :v_custmid and OrderDate
>= :v_ordrdate")
.withValueMap(new ValueMap()
.withString(":v_custmid", "jane@sample.com")
.withNumber(":v_ordrdate", 20160522));
querySpec.withSelect(Select.ALL_PROJECTED_ATTRIBUTES);
ItemCollection<QueryOutcome> items = index.query(querySpec);
Iterator<Item> iterator = items.iterator();
System.out.println("Printing query results...");
while (iterator.hasNext()) {
System.out.println(iterator.next().toJSONPretty());
}
} else {
System.out.println("\nNo index: All Jane's orders by OrderID:\n");
querySpec.withKeyConditionExpression("CustomerID = :v_custmid")
.withValueMap(new ValueMap()
.withString(":v_custmid", "jane@example.com"));
ItemCollection<QueryOutcome> items = table.query(querySpec);
Iterator<Item> iterator = items.iterator();
System.out.println("Printing query results...");
while (iterator.hasNext()) {
System.out.println(iterator.next().toJSONPretty());
}
}
}
}