Dynamodb 简明教程
DynamoDB - Getting Items
在 DynamoDB 中检索项目需要使用 GetItem,并指定表名和项目主键。请务必包含一个完整的主键,而不是省略一部分。
Retrieving an item in DynamoDB requires using GetItem, and specifying the table name and item primary key. Be sure to include a complete primary key rather than omitting a portion.
例如,省略复合键的排序键。
For example, omitting the sort key of a composite key.
GetItem 行为符合三个默认值−
GetItem behaviour conforms to three defaults −
-
It executes as an eventually consistent read.
-
It provides all attributes.
-
It does not detail its capacity unit consumption.
这些参数允许您覆盖默认的 GetItem 行为。
These parameters allow you to override the default GetItem behaviour.
Retrieve an Item
DynamoDB 通过在多个服务器上维护多个副本确保可靠性。每次成功的写操作都会创建这些副本,但执行需要大量时间;这意味着最终一致性。这意味着您不能在写一个条目后立即尝试读取。
DynamoDB ensures reliability through maintaining multiple copies of items across multiple servers. Each successful write creates these copies, but takes substantial time to execute; meaning eventually consistent. This means you cannot immediately attempt a read after writing an item.
然而,您可以更改 GetItem 的默认最终一致读取,但获取最新数据的成本仍然是消耗更多的预留单元;具体来说,消耗的资源是原来的两倍。请注意,DynamoDB 通常在一秒内使每个副本保持一致。
You can change the default eventually consistent read of GetItem, however, the cost of more current data remains consumption of more capacity units; specifically, two times as much. Note DynamoDB typically achieves consistency across every copy within a second.
您可以使用 GUI 控制台、Java 或其他工具执行此任务。
You can use the GUI console, Java, or another tool to perform this task.
Item Retrieval Using Java
在项目检索操作中使用 Java 需要创建一个 DynamoDB 类实例、表类实例,并调用表的 getItem 方法。然后指定项目的主键。
Using Java in item retrieval operations requires creating a DynamoDB Class Instance, Table Class Instance, and calling the Table instance’s getItem method. Then specify the primary key of the item.
您可以查看以下示例 -
You can review the following example −
DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient(
new ProfileCredentialsProvider()));
Table table = dynamoDB.getTable("ProductList");
Item item = table.getItem("IDnum", 109);
在某些情况下,您需要为此操作指定参数。
In some cases, you need to specify the parameters for this operation.
以下示例使用 .withProjectionExpression 和 GetItemSpec 作为检索规范−
The following example uses .withProjectionExpression and GetItemSpec for retrieval specifications −
GetItemSpec spec = new GetItemSpec()
.withPrimaryKey("IDnum", 122)
.withProjectionExpression("IDnum, EmployeeName, Department")
.withConsistentRead(true);
Item item = table.getItem(spec);
System.out.println(item.toJSONPretty());
您还可以查看以下更大的示例以更好地理解。
You can also review a the following bigger example for better understanding.
Note − 以下示例可能假设之前创建过数据源。在尝试执行之前,获取支持库并创建必要的数据源(具有所需特性的表,或其他参考源)。
Note − The following sample may assume a previously created data source. Before attempting to execute, acquire supporting libraries and create necessary data sources (tables with required characteristics, or other referenced sources).
此示例还使用 Eclipse IDE、AWS 凭据文件和 Eclipse AWS Java 项目中的 AWS 工具包。
This sample also uses Eclipse IDE, an AWS credentials file, and the AWS Toolkit within an Eclipse AWS Java Project.
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 GetItemOpSample {
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());
}
}
private static void retrieveItem() {
Table table = dynamoDB.getTable(tableName);
try {
Item item = table.getItem("ID", 303, "ID, Nomenclature, Manufacturers", null);
System.out.println("Displaying retrieved items...");
System.out.println(item.toJSONPretty());
} catch (Exception e) {
System.err.println("Cannot retrieve items.");
System.err.println(e.getMessage());
}
}
}