예: AWS SDK for Java 문서 API를 사용하는 테이블 생성, 업데이트, 삭제 및 나열 - Amazon DynamoDB

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

예: AWS SDK for Java 문서 API를 사용하는 테이블 생성, 업데이트, 삭제 및 나열

다음 코드 예제에서는 AWS SDK for Java 문서 API를 사용하여 Amazon DynamoDB 테이블(ExampleTable)을 생성하고, 업데이트하고, 삭제합니다. 표 업데이트의 일부로 할당 처리량 값이 올라갑니다. 이 예제는 또한 계정에 속한 테이블을 모두 나열하고 특정 테이블에 대한 정보를 가져오기도 합니다. 다음 예제를 실행하는 step-by-step 방법에 대한 지침은 을 참조하십시오Java 코드 예.

package com.amazonaws.codesamples.document; import java.util.ArrayList; import java.util.Iterator; 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.Table; import com.amazonaws.services.dynamodbv2.document.TableCollection; import com.amazonaws.services.dynamodbv2.model.AttributeDefinition; import com.amazonaws.services.dynamodbv2.model.CreateTableRequest; import com.amazonaws.services.dynamodbv2.model.KeySchemaElement; import com.amazonaws.services.dynamodbv2.model.KeyType; import com.amazonaws.services.dynamodbv2.model.ListTablesResult; import com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput; import com.amazonaws.services.dynamodbv2.model.TableDescription; public class DocumentAPITableExample { static AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); static DynamoDB dynamoDB = new DynamoDB(client); static String tableName = "ExampleTable"; public static void main(String[] args) throws Exception { createExampleTable(); listMyTables(); getTableInformation(); updateExampleTable(); deleteExampleTable(); } static void createExampleTable() { try { List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N")); List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>(); keySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH)); // Partition // key CreateTableRequest request = new CreateTableRequest().withTableName(tableName).withKeySchema(keySchema) .withAttributeDefinitions(attributeDefinitions).withProvisionedThroughput( new ProvisionedThroughput().withReadCapacityUnits(5L).withWriteCapacityUnits(6L)); System.out.println("Issuing CreateTable request for " + tableName); Table table = dynamoDB.createTable(request); System.out.println("Waiting for " + tableName + " to be created...this may take a while..."); table.waitForActive(); getTableInformation(); } catch (Exception e) { System.err.println("CreateTable request failed for " + tableName); System.err.println(e.getMessage()); } } static void listMyTables() { TableCollection<ListTablesResult> tables = dynamoDB.listTables(); Iterator<Table> iterator = tables.iterator(); System.out.println("Listing table names"); while (iterator.hasNext()) { Table table = iterator.next(); System.out.println(table.getTableName()); } } static void getTableInformation() { System.out.println("Describing " + tableName); TableDescription tableDescription = dynamoDB.getTable(tableName).describe(); System.out.format( "Name: %s:\n" + "Status: %s \n" + "Provisioned Throughput (read capacity units/sec): %d \n" + "Provisioned Throughput (write capacity units/sec): %d \n", tableDescription.getTableName(), tableDescription.getTableStatus(), tableDescription.getProvisionedThroughput().getReadCapacityUnits(), tableDescription.getProvisionedThroughput().getWriteCapacityUnits()); } static void updateExampleTable() { Table table = dynamoDB.getTable(tableName); System.out.println("Modifying provisioned throughput for " + tableName); try { table.updateTable(new ProvisionedThroughput().withReadCapacityUnits(6L).withWriteCapacityUnits(7L)); table.waitForActive(); } catch (Exception e) { System.err.println("UpdateTable request failed for " + tableName); System.err.println(e.getMessage()); } } static void deleteExampleTable() { Table table = dynamoDB.getTable(tableName); try { System.out.println("Issuing DeleteTable request for " + tableName); table.delete(); System.out.println("Waiting for " + tableName + " to be deleted...this may take a while..."); table.waitForDelete(); } catch (Exception e) { System.err.println("DeleteTable request failed for " + tableName); System.err.println(e.getMessage()); } } }