Dynamodb 简明教程
DynamoDB - Query Table
查询一个表主要需要选择一个表,指定一个分区键并执行查询;可以使用二级索引和通过扫描操作执行更深层次的过滤。
Querying a table primarily requires selecting a table, specifying a partition key, and executing the query; with the options of using secondary indexes and performing deeper filtering through scan operations.
使用 GUI 控制台、Java 或其他选项来执行任务。
Utilize the GUI Console, Java, or another option to perform the task.
Query Table using the GUI Console
使用先前创建的表执行一些简单查询。首先,在 https://console.aws.amazon.com/dynamodb 处打开控制台。
Perform some simple queries using the previously created tables. First, open the console at https://console.aws.amazon.com/dynamodb
从导航窗格中选择 Tables 并从表格列表中选择 Reply 。然后选择 Items 选项卡以查看已加载的数据。
Choose Tables from the navigation pane and select Reply from the table list. Then select the Items tab to see the loaded data.
选择 Create Item 按钮下方的数据过滤链接(“扫描:[表] 答复”)。
Select the data filtering link (“Scan: [Table] Reply”) beneath the Create Item button.
在过滤界面中,为操作选择查询。输入适当的分区键值,然后单击 Start 。
In the filtering screen, select Query for the operation. Enter the appropriate partition key value, and click Start.
然后, Reply 表返回匹配的项目。
The Reply table then returns matching items.
Query Table using Java
在 Java 中使用查询方法来执行数据检索操作。它需要指定分区键值,排序键为可选项。
Use the query method in Java to perform data retrieval operations. It requires specifying the partition key value, with the sort key as optional.
首先创建一个描述参数的 querySpec object 以对 Java 查询进行编码。然后将对象传递给查询方法。我们使用前面示例中的分区键。
Code a Java query by first creating a querySpec object describing parameters. Then pass the object to the query method. We use the partition key from the previous examples.
您可以查看以下示例 -
You can review the following example −
import java.util.HashMap;
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.ItemCollection;
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.NameMap;
public class ProductsQuery {
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");
HashMap<String, String> nameMap = new HashMap<String, String>();
nameMap.put("#ID", "ID");
HashMap<String, Object> valueMap = new HashMap<String, Object>();
valueMap.put(":xxx", 122);
QuerySpec querySpec = new QuerySpec()
.withKeyConditionExpression("#ID = :xxx")
.withNameMap(new NameMap().with("#ID", "ID"))
.withValueMap(valueMap);
ItemCollection<QueryOutcome> items = null;
Iterator<Item> iterator = null;
Item item = null;
try {
System.out.println("Product with the ID 122");
items = table.query(querySpec);
iterator = items.iterator();
while (iterator.hasNext()) {
item = iterator.next();
System.out.println(item.getNumber("ID") + ": "
+ item.getString("Nomenclature"));
}
} catch (Exception e) {
System.err.println("Cannot find products with the ID number 122");
System.err.println(e.getMessage());
}
}
}
请注意,查询使用分区键,但是二级索引为查询提供了另一种备选方法。它们的灵活性允许查询非键属性,本教程稍后将讨论该主题。
Note that the query uses the partition key, however, secondary indexes provide another option for queries. Their flexibility allows querying of non-key attributes, a topic which will be discussed later in this tutorial.
扫描方法还通过收集所有表数据来支持检索操作。 optional .withFilterExpression 阻止指定条件之外的项目出现在结果中。
The scan method also supports retrieval operations by gathering all the table data. The optional .withFilterExpression prevents items outside of specified criteria from appearing in results.
在接下来的教程中,我们将详细讨论 scanning 。现在,看看下面的例子−
Later in this tutorial, we will discuss scanning in detail. Now, take a look at the following example −
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.ItemCollection;
import com.amazonaws.services.dynamodbv2.document.ScanOutcome;
import com.amazonaws.services.dynamodbv2.document.Table;
import com.amazonaws.services.dynamodbv2.document.spec.ScanSpec;
import com.amazonaws.services.dynamodbv2.document.utils.NameMap;
import com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
public class ProductsScan {
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");
ScanSpec scanSpec = new ScanSpec()
.withProjectionExpression("#ID, Nomenclature , stat.sales")
.withFilterExpression("#ID between :start_id and :end_id")
.withNameMap(new NameMap().with("#ID", "ID"))
.withValueMap(new ValueMap().withNumber(":start_id", 120)
.withNumber(":end_id", 129));
try {
ItemCollection<ScanOutcome> items = table.scan(scanSpec);
Iterator<Item> iter = items.iterator();
while (iter.hasNext()) {
Item item = iter.next();
System.out.println(item.toString());
}
} catch (Exception e) {
System.err.println("Cannot perform a table scan:");
System.err.println(e.getMessage());
}
}
}