TryDaxTests.java - Amazon DynamoDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

TryDaxTests.java

TryDaxTests.java 檔案包含對 Amazon DynamoDB 中測試資料表執行讀取操作的方法。這些方法不關心他們存取資料的方式 (使用 DynamoDB 用戶端或DAX用戶端),因此不需要修改應用程式邏輯。

您可以使用幾種方法修改程式:

  • 修改 queryTest 方法,使其使用不同的 KeyConditionExpression

  • ScanFilter 新增至 scanTest 方法,使其只傳回一部分的項目。

注意

若要執行此程式,您可以將 Maven 設定為使用DAXSDK適用於 Java 的 用戶端,並將 AWS SDK for Java 作為相依性。如需詳細資訊,請參閱使用用戶端做為 Apache Maven 依存項目

或者,您可以下載 DAX Java 用戶端和 並將其包含在 classpath AWS SDK for Java 中。請參閱 Java 和 DAX 以取得設定您 CLASSPATH 變數的範例。

import java.util.Iterator; 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.ScanOutcome; import com.amazonaws.services.dynamodbv2.document.Table; import com.amazonaws.services.dynamodbv2.document.spec.QuerySpec; public class TryDaxTests { void getItemTest(String tableName, DynamoDB client, int pk, int sk, int iterations) { long startTime, endTime; System.out.println("GetItem test - partition key " + pk + " and sort keys 1-" + sk); Table table = client.getTable(tableName); for (int i = 0; i < iterations; i++) { startTime = System.nanoTime(); try { for (Integer ipk = 1; ipk <= pk; ipk++) { for (Integer isk = 1; isk <= sk; isk++) { table.getItem("pk", ipk, "sk", isk); } } } catch (Exception e) { System.err.println("Unable to get item:"); e.printStackTrace(); } endTime = System.nanoTime(); printTime(startTime, endTime, pk * sk); } } void queryTest(String tableName, DynamoDB client, int pk, int sk1, int sk2, int iterations) { long startTime, endTime; System.out.println("Query test - partition key " + pk + " and sort keys between " + sk1 + " and " + sk2); Table table = client.getTable(tableName); HashMap<String, Object> valueMap = new HashMap<String, Object>(); valueMap.put(":pkval", pk); valueMap.put(":skval1", sk1); valueMap.put(":skval2", sk2); QuerySpec spec = new QuerySpec() .withKeyConditionExpression("pk = :pkval and sk between :skval1 and :skval2") .withValueMap(valueMap); for (int i = 0; i < iterations; i++) { startTime = System.nanoTime(); ItemCollection<QueryOutcome> items = table.query(spec); try { Iterator<Item> iter = items.iterator(); while (iter.hasNext()) { iter.next(); } } catch (Exception e) { System.err.println("Unable to query table:"); e.printStackTrace(); } endTime = System.nanoTime(); printTime(startTime, endTime, iterations); } } void scanTest(String tableName, DynamoDB client, int iterations) { long startTime, endTime; System.out.println("Scan test - all items in the table"); Table table = client.getTable(tableName); for (int i = 0; i < iterations; i++) { startTime = System.nanoTime(); ItemCollection<ScanOutcome> items = table.scan(); try { Iterator<Item> iter = items.iterator(); while (iter.hasNext()) { iter.next(); } } catch (Exception e) { System.err.println("Unable to scan table:"); e.printStackTrace(); } endTime = System.nanoTime(); printTime(startTime, endTime, iterations); } } public void printTime(long startTime, long endTime, int iterations) { System.out.format("\tTotal time: %.3f ms - ", (endTime - startTime) / (1000000.0)); System.out.format("Avg time: %.3f ms\n", (endTime - startTime) / (iterations * 1000000.0)); } }