本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
TryDaxHelper.java
檔案包含公用程式方法。
getDynamoDBClient
和 getDaxClient
方法提供 Amazon DynamoDB 和 DynamoDB Accelerator (DAX) 用戶端。針對控制平面操作 (CreateTable
、DeleteTable
) 及寫入操作,程式會使用 DynamoDB 用戶端。若您指定 DAX 叢集端點,主程式會建立 DAX 用戶端來執行讀取操作 (GetItem
、Query
、Scan
)。
其他 TryDaxHelper
方法 (createTable
、writeData
、deleteTable
) 用於設定及卸除 DynamoDB 資料表和其資料。
您可以使用幾種方法修改程式:
-
針對資料表使用不同的佈建輸送量設定。
-
修改每個寫入項目的大小 (請參閱
stringSize
方法中的writeData
變數)。 -
修改
GetItem
、Query
和Scan
測試的數字及其參數。 -
將包含
helper.CreateTable
和helper.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();
}
}
}