Java Code Examples for software.amazon.awssdk.services.dynamodb.DynamoDbClient#createTable()

The following examples show how to use software.amazon.awssdk.services.dynamodb.DynamoDbClient#createTable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DynamoDBUtils.java    From ShedLock with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a locking table with the given name.
 * <p>
 * This method does not check if a table with the given name exists already.
 *
 * @param ddbClient  v2 of DynamoDBClient
 * @param tableName  table to be used
 * @param throughput AWS {@link ProvisionedThroughput throughput requirements} for the given lock setup
 * @return           the table name
 *
 * @throws ResourceInUseException
 *         The operation conflicts with the resource's availability. You attempted to recreate an
 *         existing table.
 */
public static String createLockTable(
        DynamoDbClient ddbClient,
        String tableName,
        ProvisionedThroughput throughput
) {

    CreateTableRequest request = CreateTableRequest.builder()
            .tableName(tableName)
            .keySchema(KeySchemaElement.builder()
                    .attributeName(ID)
                    .keyType(KeyType.HASH)
                    .build())
            .attributeDefinitions(AttributeDefinition.builder()
                    .attributeName(ID)
                    .attributeType(ScalarAttributeType.S)
                    .build())
            .provisionedThroughput(throughput)
            .build();
    ddbClient.createTable(request);
    return tableName;
}
 
Example 2
Source File: MetaStore.java    From aws-dynamodb-encryption-java with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a DynamoDB Table with the correct properties to be used with a ProviderStore.
 *
 * @param ddb interface for accessing DynamoDB
 * @param tableName name of table that stores the meta data of the material.
 * @param provisionedThroughput required provisioned throughput of the this table.
 * @return result of create table request.
 */
public static CreateTableResponse createTable(final DynamoDbClient ddb, final String tableName,
                                              final ProvisionedThroughput provisionedThroughput) {
    return ddb.createTable(
        CreateTableRequest.builder()
                          .tableName(tableName)
                          .attributeDefinitions(Arrays.asList(
                              AttributeDefinition.builder()
                                                 .attributeName(DEFAULT_HASH_KEY)
                                                 .attributeType(ScalarAttributeType.S)
                                                 .build(),
                              AttributeDefinition.builder()
                                                 .attributeName(DEFAULT_RANGE_KEY)
                                                 .attributeType(ScalarAttributeType.N).build()))
                          .keySchema(Arrays.asList(
                              KeySchemaElement.builder()
                                              .attributeName(DEFAULT_HASH_KEY)
                                              .keyType(KeyType.HASH)
                                              .build(),
                              KeySchemaElement.builder()
                                              .attributeName(DEFAULT_RANGE_KEY)
                                              .keyType(KeyType.RANGE)
                                              .build()))
                          .provisionedThroughput(provisionedThroughput).build());
}
 
Example 3
Source File: Aws2Test.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
private static void createTable(final DynamoDbClient dbClient, final String tableName) {
  final String partitionKeyName = tableName + "Id";
  final CreateTableRequest createTableRequest = CreateTableRequest.builder()
    .tableName(tableName)
    .keySchema(KeySchemaElement.builder().attributeName(partitionKeyName).keyType(KeyType.HASH).build())
    .attributeDefinitions(AttributeDefinition.builder().attributeName(partitionKeyName).attributeType("S").build())
    .provisionedThroughput(ProvisionedThroughput.builder().readCapacityUnits(10L).writeCapacityUnits(5L).build()).build();

  dbClient.createTable(createTableRequest);
}
 
Example 4
Source File: Aws2ITest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
private static void createTable(final DynamoDbClient dbClient, final String tableName) {
  final String partitionKeyName = tableName + "-pk";
  final CreateTableRequest createTableRequest = CreateTableRequest
    .builder().tableName(tableName)
    .keySchema(KeySchemaElement.builder().attributeName(partitionKeyName).keyType(KeyType.HASH).build())
    .attributeDefinitions(AttributeDefinition.builder().attributeName(partitionKeyName).attributeType("S").build())
    .provisionedThroughput(ProvisionedThroughput.builder().readCapacityUnits(10L).writeCapacityUnits(5L).build())
    .build();
  dbClient.createTable(createTableRequest);
  System.out.println("Table " + tableName + " created");
}
 
Example 5
Source File: DynamoDBUtils.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public static boolean createTableIfNotExists(final DynamoDbClient dynamo, final String tableName) {
    try {
        dynamo.createTable(createTableRequest(tableName));
        return waitUntilTableActive(dynamo, tableName);
    } catch (ResourceInUseException e) {
        LOG.info("Reused existing table");
    }
    return true;
}
 
Example 6
Source File: TableUtils.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the table and ignores any errors if it already exists.
 * @param dynamo The Dynamo client to use.
 * @param createTableRequest The create table request.
 * @return True if created, false otherwise.
 */
public static boolean createTableIfNotExists(final DynamoDbClient dynamo, final CreateTableRequest createTableRequest) {
    try {
        dynamo.createTable(createTableRequest);
        return true;
    } catch (final ResourceInUseException e) {
        if (log.isTraceEnabled()) {
            log.trace("Table " + createTableRequest.tableName() + " already exists", e);
        }
    }
    return false;
}
 
Example 7
Source File: CreateTable.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static String createTable(DynamoDbClient ddb, String tableName, String key) {

        // Create the CreateTableRequest object
        CreateTableRequest request = CreateTableRequest.builder()
                .attributeDefinitions(AttributeDefinition.builder()
                        .attributeName(key)
                        .attributeType(ScalarAttributeType.S)
                        .build())
                .keySchema(KeySchemaElement.builder()
                        .attributeName(key)
                        .keyType(KeyType.HASH)
                        .build())
                .provisionedThroughput(ProvisionedThroughput.builder()
                        .readCapacityUnits(new Long(10))
                        .writeCapacityUnits(new Long(10))
                        .build())
                .tableName(tableName)
                .build();

        String newTable ="";
        try {
            CreateTableResponse response = ddb.createTable(request);
            newTable = response.tableDescription().tableName();
            return newTable;
        } catch (DynamoDbException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        // snippet-end:[dynamodb.java2.create_table.main]
        return "";
    }
 
Example 8
Source File: CreateTableCompositeKey.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static String createTableComKey(DynamoDbClient ddb, String tableName) {
    CreateTableRequest request = CreateTableRequest.builder()
            .attributeDefinitions(
                    AttributeDefinition.builder()
                            .attributeName("Language")
                            .attributeType(ScalarAttributeType.S)
                            .build(),
                    AttributeDefinition.builder()
                            .attributeName("Greeting")
                            .attributeType(ScalarAttributeType.S)
                            .build())
            .keySchema(
                    KeySchemaElement.builder()
                            .attributeName("Language")
                            .keyType(KeyType.HASH)
                            .build(),
                    KeySchemaElement.builder()
                            .attributeName("Greeting")
                            .keyType(KeyType.RANGE)
                            .build())
            .provisionedThroughput(
                    ProvisionedThroughput.builder()
                            .readCapacityUnits(new Long(10))
                            .writeCapacityUnits(new Long(10)).build())
            .tableName(tableName)
            .build();


   String tableId = "";

   try {
        CreateTableResponse result = ddb.createTable(request);
        tableId = result.tableDescription().tableId();
        return tableId;
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    // snippet-end:[dynamodb.java2.create_table_composite_key.main]
    return "";
}