TryDaxHelper. 자바 - Amazon DynamoDB

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

TryDaxHelper. 자바

TryDaxHelper.java 파일에는 유틸리티 메서드가 포함되어 있습니다.

getDynamoDBClientgetDaxClient 메서드는 Amazon DynamoDB 및 DynamoDB 액셀러레이터(DAX) 클라이언트를 제공합니다. 제어 플레인 작업(CreateTable, DeleteTable) 및 쓰기 작업의 경우 프로그램은 DynamoDB 클라이언트를 사용합니다. DAX 클러스터 엔드포인트를 지정하면 기본 프로그램에서 DAX 클라이언트를 생성하여 읽기 작업(GetItem, Query, Scan)을 수행합니다.

다른 TryDaxHelper 메서드(createTable, writeData, deleteTable)는 DynamoDB 테이블 및 해당 데이터 설정 및 삭제용입니다.

여러 가지 방법으로 프로그램을 수정할 수 있습니다.

  • 테이블에 다른 할당 처리량 설정을 사용합니다.

  • 기록된 각 항목의 크기를 수정합니다(writeData 메서드의 stringSize 변수 참조).

  • GetItem, QueryScan 테스트 개수를 수정하고 해당 파라미터를 수정합니다.

  • helper.CreateTablehelper.DeleteTable을 포함하는 행을 주석으로 처리합니다(프로그램을 실행할 때마다 테이블을 생성 및 삭제하지 않으려는 경우).

참고

이 프로그램을 실행하려면 DAX SDK for Java 클라이언트와 AWS SDK for Java를 종속 항목으로 사용하도록 Maven을 설정합니다. 자세한 설명은 클라이언트를 Apache Maven 종속 항목으로 사용 섹션을 참조하세요.

또는 DAX Java 클라이언트와 AWS SDK for Java를 모두 다운로드하여 클래스 경로(classpath)에 포함할 수 있습니다. 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(); } } }