Java Code Examples for com.amazonaws.services.dynamodbv2.AmazonDynamoDB#createTable()

The following examples show how to use com.amazonaws.services.dynamodbv2.AmazonDynamoDB#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: CRUDTest.java    From dynamo-cassandra-proxy with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreate() {
    AmazonDynamoDB proxyClient = getProxyClient();
    AmazonDynamoDB awsClient = getAwsClient();

    CreateTableRequest req = new CreateTableRequest()
            .withTableName("foo")
            .withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(100L).withWriteCapacityUnits(100L))
            .withKeySchema(new KeySchemaElement("Name", KeyType.HASH))
            .withAttributeDefinitions(new AttributeDefinition("Name", ScalarAttributeType.S));

    proxyClient.createTable(req);
    awsClient.createTable(req);

    DescribeTableResult r = proxyClient.describeTable("foo");
    DescribeTableResult r2 = proxyClient.describeTable("foo");

    Date now = new Date();
    r.getTable().withCreationDateTime(now);
    r2.getTable().withCreationDateTime(now);

    Assert.assertEquals(r, r2);
}
 
Example 2
Source File: SpringLocalstackDockerRunnerTest.java    From spring-localstack with Apache License 2.0 6 votes vote down vote up
@Test
public void testDynamo() throws Exception {
    AmazonDynamoDB dynamoDB = amazonDockerClientsHolder.amazonDynamoDB();

    ListTablesResult tablesResult = dynamoDB.listTables();
    assertThat(tablesResult.getTableNames().size(), is(0));

    CreateTableRequest createTableRequest = new CreateTableRequest()
        .withTableName("test.table")
        .withKeySchema(new KeySchemaElement("identifier", KeyType.HASH))
        .withAttributeDefinitions(new AttributeDefinition("identifier", ScalarAttributeType.S))
        .withProvisionedThroughput(new ProvisionedThroughput(10L, 10L));
    dynamoDB.createTable(createTableRequest);

    tablesResult = dynamoDB.listTables();
    assertThat(tablesResult.getTableNames(), hasItem("test.table"));
}
 
Example 3
Source File: DynaliteContainerTest.java    From testcontainers-java with MIT License 6 votes vote down vote up
private void runTest(AmazonDynamoDB client) {
    CreateTableRequest request = new CreateTableRequest()
            .withAttributeDefinitions(new AttributeDefinition(
                    "Name", ScalarAttributeType.S))
            .withKeySchema(new KeySchemaElement("Name", KeyType.HASH))
            .withProvisionedThroughput(new ProvisionedThroughput(
                10L, 10L))
            .withTableName("foo");


    client.createTable(request);

    final TableDescription tableDescription = client.describeTable("foo").getTable();

    assertNotNull("the description is not null", tableDescription);
    assertEquals("the table has the right name", "foo", tableDescription.getTableName());
    assertEquals("the name has the right primary key", "Name", tableDescription.getKeySchema().get(0).getAttributeName());
}
 
Example 4
Source File: DynamoDBEmbeddedTest.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
private static CreateTableResult createTable(AmazonDynamoDB ddb, String tableName, String hashKeyName) {
    List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions.add(new AttributeDefinition(hashKeyName, ScalarAttributeType.S));

    List<KeySchemaElement> ks = new ArrayList<KeySchemaElement>();
    ks.add(new KeySchemaElement(hashKeyName, KeyType.HASH));

    ProvisionedThroughput provisionedthroughput = new ProvisionedThroughput(1000L, 1000L);

    CreateTableRequest request =
        new CreateTableRequest()
            .withTableName(tableName)
            .withAttributeDefinitions(attributeDefinitions)
            .withKeySchema(ks)
            .withProvisionedThroughput(provisionedthroughput);

    return ddb.createTable(request);
}
 
Example 5
Source File: AwsTest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
private static void createTable(final AmazonDynamoDB dbClient, final String tableName) {
  final String partitionKeyName = tableName + "Id";
  final CreateTableRequest createTableRequest = new CreateTableRequest()
    .withTableName(tableName)
    .withKeySchema(new KeySchemaElement().withAttributeName(partitionKeyName).withKeyType(KeyType.HASH))
    .withAttributeDefinitions(new AttributeDefinition().withAttributeName(partitionKeyName).withAttributeType("S"))
    .withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(10L).withWriteCapacityUnits(5L));

  dbClient.createTable(createTableRequest);
}
 
Example 6
Source File: Aws1ITest.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
private static void createTable(final AmazonDynamoDB dbClient, final String tableName) {
  final String partitionKeyName = tableName + "-pk";
  final CreateTableRequest createTableRequest = new CreateTableRequest()
    .withTableName(tableName)
    .withKeySchema(new KeySchemaElement().withAttributeName(partitionKeyName).withKeyType(KeyType.HASH))
    .withAttributeDefinitions(new AttributeDefinition().withAttributeName(partitionKeyName).withAttributeType("S"))
    .withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(10L).withWriteCapacityUnits(5L));
  dbClient.createTable(createTableRequest);
  System.out.println("Table " + tableName + " created");
}
 
Example 7
Source File: StreamsAdapterDemoHelper.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
/**
 * @return StreamArn
 */
public static String createTable(AmazonDynamoDB client, String tableName) {
    java.util.List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N"));

    java.util.List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
    keySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH)); // Partition
                                                                                             // key

    ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(2L)
        .withWriteCapacityUnits(2L);

    StreamSpecification streamSpecification = new StreamSpecification();
    streamSpecification.setStreamEnabled(true);
    streamSpecification.setStreamViewType(StreamViewType.NEW_IMAGE);
    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
        .withAttributeDefinitions(attributeDefinitions).withKeySchema(keySchema)
        .withProvisionedThroughput(provisionedThroughput).withStreamSpecification(streamSpecification);

    try {
        System.out.println("Creating table " + tableName);
        CreateTableResult result = client.createTable(createTableRequest);
        return result.getTableDescription().getLatestStreamArn();
    }
    catch (ResourceInUseException e) {
        System.out.println("Table already exists.");
        return describeTable(client, tableName).getTable().getLatestStreamArn();
    }
}
 
Example 8
Source File: MetaStore.java    From aws-dynamodb-encryption-java with Apache License 2.0 5 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 CreateTableResult createTable(final AmazonDynamoDB ddb, final String tableName,
        final ProvisionedThroughput provisionedThroughput) {
    return ddb.createTable(Arrays.asList(new AttributeDefinition(DEFAULT_HASH_KEY,
            ScalarAttributeType.S), new AttributeDefinition(DEFAULT_RANGE_KEY,
                    ScalarAttributeType.N)), tableName, Arrays.asList(new KeySchemaElement(
                            DEFAULT_HASH_KEY, KeyType.HASH), new KeySchemaElement(DEFAULT_RANGE_KEY,
                                    KeyType.RANGE)), provisionedThroughput);

}
 
Example 9
Source File: TitanGraphDatabase.java    From graphdb-benchmarks with Apache License 2.0 5 votes vote down vote up
private void open(boolean batchLoading)
{
    if(type == GraphDatabaseType.TITAN_DYNAMODB && config.getDynamodbPrecreateTables()) {
        List<CreateTableRequest> requests = new LinkedList<>();
        long wcu = config.getDynamodbTps();
        long rcu = Math.max(1, config.dynamodbConsistentRead() ? wcu : (wcu / 2));
        for(String store : Constants.REQUIRED_BACKEND_STORES) {
            final String tableName = config.getDynamodbTablePrefix() + "_" + store;
            if(BackendDataModel.MULTI == config.getDynamodbDataModel()) {
                requests.add(DynamoDBStore.createTableRequest(tableName,
                    rcu, wcu));
            } else if(BackendDataModel.SINGLE == config.getDynamodbDataModel()) {
                requests.add(DynamoDBSingleRowStore.createTableRequest(tableName, rcu, wcu));
            }
        }
        //TODO is this autocloseable?
        final AmazonDynamoDB client =
            new AmazonDynamoDBClient(Client.createAWSCredentialsProvider(config.getDynamodbCredentialsFqClassName(),
                config.getDynamodbCredentialsCtorArguments() == null ? null : config.getDynamodbCredentialsCtorArguments().split(",")));
        client.setEndpoint(config.getDynamodbEndpoint());
        for(CreateTableRequest request : requests) {
            try {
                client.createTable(request);
            } catch(ResourceInUseException ignore) {
                //already created, good
            }
        }
        client.shutdown();
    }
    titanGraph = buildTitanGraph(type, dbStorageDirectory, config, batchLoading);
}
 
Example 10
Source File: CreateTable.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args)
{
    final String USAGE = "\n" +
        "Usage:\n" +
        "    CreateTable <table>\n\n" +
        "Where:\n" +
        "    table - the table to create.\n\n" +
        "Example:\n" +
        "    CreateTable HelloTable\n";

    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);
    }

    /* Read the name from command args */
    String table_name = args[0];

    System.out.format(
        "Creating table \"%s\" with a simple primary key: \"Name\".\n",
        table_name);

    CreateTableRequest request = new CreateTableRequest()
        .withAttributeDefinitions(new AttributeDefinition(
                 "Name", ScalarAttributeType.S))
        .withKeySchema(new KeySchemaElement("Name", KeyType.HASH))
        .withProvisionedThroughput(new ProvisionedThroughput(
                 new Long(10), new Long(10)))
        .withTableName(table_name);

    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();

    try {
        CreateTableResult result = ddb.createTable(request);
        System.out.println(result.getTableDescription().getTableName());
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
 
Example 11
Source File: CreateTableCompositeKey.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args)
{
    final String USAGE = "\n" +
        "Usage:\n" +
        "    CreateTable <table>\n\n" +
        "Where:\n" +
        "    table - the table to create.\n\n" +
        "Example:\n" +
        "    CreateTable GreetingsTable\n";

    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);
    }

    /* Read the name from command args */
    String table_name = args[0];

    System.out.format("Creating table %s\n with a composite primary key:\n");
    System.out.format("* Language - partition key\n");
    System.out.format("* Greeting - sort key\n");

    CreateTableRequest request = new CreateTableRequest()
        .withAttributeDefinitions(
              new AttributeDefinition("Language", ScalarAttributeType.S),
              new AttributeDefinition("Greeting", ScalarAttributeType.S))
        .withKeySchema(
              new KeySchemaElement("Language", KeyType.HASH),
              new KeySchemaElement("Greeting", KeyType.RANGE))
        .withProvisionedThroughput(
              new ProvisionedThroughput(new Long(10), new Long(10)))
        .withTableName(table_name);

    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();

    try {
        CreateTableResult result = ddb.createTable(request);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}