TryDaxHelper.java - Amazon DynamoDB

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

TryDaxHelper.java

TryDaxHelper.java 檔案包含公用程式方法。

getDynamoDBClientgetDaxClient方法提供 Amazon DynamoDB 和 DynamoDB Accelerator (DAX) 用戶端。針對控制平面操作 (CreateTableDeleteTable) 及寫入操作,程式會使用 DynamoDB 用戶端。如果您指定DAX叢集端點,則主要程式會建立DAX用戶端來執行讀取操作 (GetItemQueryScan)。

其他 TryDaxHelper 方法 (createTablewriteDatadeleteTable) 用於設定及卸除 DynamoDB 資料表和其資料。

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

  • 針對資料表使用不同的佈建輸送量設定。

  • 修改每個寫入項目的大小 (請參閱 stringSize 方法中的 writeData 變數)。

  • 修改 GetItemQueryScan 測試的數字及其參數。

  • 將包含 helper.CreateTablehelper.DeleteTable 的行變更為註解 (若您不希望每次執行程式時都建立和刪除資料表的話)。

注意

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

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

import com.amazon.dax.client.dynamodbv2.AmazonDaxClientBuilder; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; 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.model.AttributeDefinition; import com.amazonaws.services.dynamodbv2.model.KeySchemaElement; import com.amazonaws.services.dynamodbv2.model.KeyType; import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType; import com.amazonaws.util.EC2MetadataUtils; public class TryDaxHelper { private static final String region = EC2MetadataUtils.getEC2InstanceRegion(); DynamoDB getDynamoDBClient() { System.out.println("Creating a DynamoDB client"); AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard() .withRegion(region) .build(); return new DynamoDB(client); } DynamoDB getDaxClient(String daxEndpoint) { System.out.println("Creating a DAX client with cluster endpoint " + daxEndpoint); AmazonDaxClientBuilder daxClientBuilder = AmazonDaxClientBuilder.standard(); daxClientBuilder.withRegion(region).withEndpointConfiguration(daxEndpoint); AmazonDynamoDB client = daxClientBuilder.build(); return new DynamoDB(client); } void createTable(String tableName, DynamoDB client) { Table table = client.getTable(tableName); try { System.out.println("Attempting to create table; please wait..."); table = client.createTable(tableName, Arrays.asList( new KeySchemaElement("pk", KeyType.HASH), // Partition key new KeySchemaElement("sk", KeyType.RANGE)), // Sort key Arrays.asList( new AttributeDefinition("pk", ScalarAttributeType.N), new AttributeDefinition("sk", ScalarAttributeType.N)), new ProvisionedThroughput(10L, 10L)); table.waitForActive(); System.out.println("Successfully created table. Table status: " + table.getDescription().getTableStatus()); } catch (Exception e) { System.err.println("Unable to create table: "); e.printStackTrace(); } } void writeData(String tableName, DynamoDB client, int pkmax, int skmax) { Table table = client.getTable(tableName); System.out.println("Writing data to the table..."); int stringSize = 1000; StringBuilder sb = new StringBuilder(stringSize); for (int i = 0; i < stringSize; i++) { sb.append('X'); } String someData = sb.toString(); try { for (Integer ipk = 1; ipk <= pkmax; ipk++) { System.out.println(("Writing " + skmax + " items for partition key: " + ipk)); for (Integer isk = 1; isk <= skmax; isk++) { table.putItem(new Item() .withPrimaryKey("pk", ipk, "sk", isk) .withString("someData", someData)); } } } catch (Exception e) { System.err.println("Unable to write item:"); e.printStackTrace(); } } void deleteTable(String tableName, DynamoDB client) { Table table = client.getTable(tableName); try { System.out.println("\nAttempting to delete table; please wait..."); table.delete(); table.waitForDelete(); System.out.println("Successfully deleted table."); } catch (Exception e) { System.err.println("Unable to delete table: "); e.printStackTrace(); } } }