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

The following examples show how to use com.amazonaws.services.dynamodbv2.document.DynamoDB#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: TryDaxHelper.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 2
Source File: MoviesCreateTable.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        
        client.setEndpoint("http://localhost:8000");
        DynamoDB dynamoDB = new DynamoDB(client);
        
        String tableName = "Movies";
        Table table = dynamoDB.createTable(tableName,
                Arrays.asList(
                        new KeySchemaElement("year", KeyType.HASH),
                        new KeySchemaElement("title", KeyType.RANGE)), 
                Arrays.asList(
                        new AttributeDefinition("year", ScalarAttributeType.N),
                        new AttributeDefinition("title", ScalarAttributeType.S)), 
                new ProvisionedThroughput(10L, 10L));

        try {
            TableUtils.waitUntilActive(client, tableName);
            System.out.println("Table status: " + table.getDescription().getTableStatus());
        } catch (AmazonClientException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
 
Example 3
Source File: MoviesCreateTable.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
            .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
            .build();

        DynamoDB dynamoDB = new DynamoDB(client);

        String tableName = "Movies";

        try {
            System.out.println("Attempting to create table; please wait...");
            Table table = dynamoDB.createTable(tableName,
                Arrays.asList(new KeySchemaElement("year", KeyType.HASH), // Partition
                                                                          // key
                    new KeySchemaElement("title", KeyType.RANGE)), // Sort key
                Arrays.asList(new AttributeDefinition("year", ScalarAttributeType.N),
                    new AttributeDefinition("title", ScalarAttributeType.S)),
                new ProvisionedThroughput(10L, 10L));
            table.waitForActive();
            System.out.println("Success.  Table status: " + table.getDescription().getTableStatus());

        }
        catch (Exception e) {
            System.err.println("Unable to create table: ");
            System.err.println(e.getMessage());
        }

    }
 
Example 4
Source File: DynamoDBUtils.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
public static TableDescription createTable(AmazonDynamoDB client, String tableName) throws InterruptedException {
    CreateTableRequest tableReq = new CreateTableRequest().withTableName(tableName)
            .withKeySchema(new KeySchemaElement("Id", KeyType.HASH))
            .withAttributeDefinitions(new AttributeDefinition("Id", ScalarAttributeType.N))
            .withProvisionedThroughput(new ProvisionedThroughput(10L, 10L))
            .withStreamSpecification(new StreamSpecification().withStreamEnabled(true).withStreamViewType(StreamViewType.NEW_AND_OLD_IMAGES));

    DynamoDB dynamoDB = new DynamoDB(client);
    Table table = dynamoDB.createTable(tableReq);
    return table.waitForActive();
}
 
Example 5
Source File: ITAbstractDynamoDBTest.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
    FileInputStream fis = new FileInputStream(CREDENTIALS_FILE);
    final PropertiesCredentials credentials = new PropertiesCredentials(fis);
    amazonDynamoDBClient = new AmazonDynamoDBClient(credentials);
    dynamoDB = new DynamoDB(amazonDynamoDBClient);
    amazonDynamoDBClient.setRegion(Region.getRegion(Regions.US_WEST_2));

    ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions
        .add(new AttributeDefinition().withAttributeName("hashS").withAttributeType("S"));
    attributeDefinitions
        .add(new AttributeDefinition().withAttributeName("rangeS").withAttributeType("S"));

    ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
    keySchema.add(new KeySchemaElement().withAttributeName("hashS").withKeyType(KeyType.HASH));
    keySchema.add(new KeySchemaElement().withAttributeName("rangeS").withKeyType(KeyType.RANGE));

    CreateTableRequest request = new CreateTableRequest()
        .withTableName(stringHashStringRangeTableName)
        .withKeySchema(keySchema)
        .withAttributeDefinitions(attributeDefinitions)
        .withProvisionedThroughput(new ProvisionedThroughput()
            .withReadCapacityUnits(5L)
            .withWriteCapacityUnits(6L));
    Table stringHashStringRangeTable = dynamoDB.createTable(request);
    stringHashStringRangeTable.waitForActive();

    attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions
        .add(new AttributeDefinition().withAttributeName("hashN").withAttributeType("N"));
    attributeDefinitions
        .add(new AttributeDefinition().withAttributeName("rangeN").withAttributeType("N"));

    keySchema = new ArrayList<KeySchemaElement>();
    keySchema.add(new KeySchemaElement().withAttributeName("hashN").withKeyType(KeyType.HASH));
    keySchema.add(new KeySchemaElement().withAttributeName("rangeN").withKeyType(KeyType.RANGE));

    request = new CreateTableRequest()
        .withTableName(numberHashNumberRangeTableName)
        .withKeySchema(keySchema)
        .withAttributeDefinitions(attributeDefinitions)
        .withProvisionedThroughput(new ProvisionedThroughput()
            .withReadCapacityUnits(5L)
            .withWriteCapacityUnits(6L));
    Table numberHashNumberRangeTable = dynamoDB.createTable(request);
    numberHashNumberRangeTable.waitForActive();

    attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions
        .add(new AttributeDefinition().withAttributeName("hashN").withAttributeType("N"));

    keySchema = new ArrayList<KeySchemaElement>();
    keySchema.add(new KeySchemaElement().withAttributeName("hashN").withKeyType(KeyType.HASH));

    request = new CreateTableRequest()
        .withTableName(numberHashOnlyTableName)
        .withKeySchema(keySchema)
        .withAttributeDefinitions(attributeDefinitions)
        .withProvisionedThroughput(new ProvisionedThroughput()
            .withReadCapacityUnits(5L)
            .withWriteCapacityUnits(6L));
    Table numberHashOnlyTable = dynamoDB.createTable(request);
    numberHashOnlyTable.waitForActive();

}
 
Example 6
Source File: RedshiftIT.java    From digdag with Apache License 2.0 4 votes vote down vote up
@Test
public void loadFromDynamoDB()
        throws Exception
{
    DynamoDB dynamoDB = new DynamoDB(dynamoClient);

    ArrayList<AttributeDefinition> attributeDefinitions= new ArrayList<>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N"));

    ArrayList<KeySchemaElement> keySchema = new ArrayList<>();
    keySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH));

    CreateTableRequest request = new CreateTableRequest()
            .withTableName(dynamoTableName)
            .withKeySchema(keySchema)
            .withAttributeDefinitions(attributeDefinitions)
            .withProvisionedThroughput(new ProvisionedThroughput()
                    .withReadCapacityUnits(1L)
                    .withWriteCapacityUnits(1L));

    ImmutableList<Item> items = ImmutableList.of(
            new Item().withPrimaryKey("Id", 0).withString("Name", "foo").withNumber("Score", 3.14f),
            new Item().withPrimaryKey("Id", 1).withString("Name", "bar").withNumber("Score", 1.23f),
            new Item().withPrimaryKey("Id", 2).withString("Name", "baz").withNumber("Score", 5.0f)
    );

    ImmutableList<Map<String, Object>> expected = ImmutableList.of(
            ImmutableMap.of("id", 0, "name", "foo", "score", 3.14f),
            ImmutableMap.of("id", 1, "name", "bar", "score", 1.23f),
            ImmutableMap.of("id", 2, "name", "baz", "score", 5.0f),
            ImmutableMap.of("id", 9, "name", "zzz", "score", 9.99f)
    );

    Table table = null;
    try {
        table = dynamoDB.createTable(request);
        table.waitForActive();

        items.forEach(table::putItem);

        runDigdagWithDynamoDB(configFile, "acceptance/redshift/load_from_dynamodb.dig", redshiftUser, Optional.absent());

        assertTableContents(DEST_TABLE, expected);
    }
    finally {
        if (table != null) {
            table.delete();
            table.waitForDelete();
        }
    }
}
 
Example 7
Source File: ITAbstractDynamoDBTest.java    From nifi with Apache License 2.0 4 votes vote down vote up
@BeforeClass
public static void beforeClass() throws Exception {
    FileInputStream fis = new FileInputStream(CREDENTIALS_FILE);
    final PropertiesCredentials credentials = new PropertiesCredentials(fis);
    amazonDynamoDBClient = new AmazonDynamoDBClient(credentials);
    dynamoDB = new DynamoDB(amazonDynamoDBClient);
    amazonDynamoDBClient.setRegion(Region.getRegion(Regions.US_WEST_2));

    ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions
        .add(new AttributeDefinition().withAttributeName("hashS").withAttributeType("S"));
    attributeDefinitions
        .add(new AttributeDefinition().withAttributeName("rangeS").withAttributeType("S"));

    ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
    keySchema.add(new KeySchemaElement().withAttributeName("hashS").withKeyType(KeyType.HASH));
    keySchema.add(new KeySchemaElement().withAttributeName("rangeS").withKeyType(KeyType.RANGE));

    CreateTableRequest request = new CreateTableRequest()
        .withTableName(stringHashStringRangeTableName)
        .withKeySchema(keySchema)
        .withAttributeDefinitions(attributeDefinitions)
        .withProvisionedThroughput(new ProvisionedThroughput()
            .withReadCapacityUnits(5L)
            .withWriteCapacityUnits(6L));
    Table stringHashStringRangeTable = dynamoDB.createTable(request);
    stringHashStringRangeTable.waitForActive();

    attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions
        .add(new AttributeDefinition().withAttributeName("hashN").withAttributeType("N"));
    attributeDefinitions
        .add(new AttributeDefinition().withAttributeName("rangeN").withAttributeType("N"));

    keySchema = new ArrayList<KeySchemaElement>();
    keySchema.add(new KeySchemaElement().withAttributeName("hashN").withKeyType(KeyType.HASH));
    keySchema.add(new KeySchemaElement().withAttributeName("rangeN").withKeyType(KeyType.RANGE));

    request = new CreateTableRequest()
        .withTableName(numberHashNumberRangeTableName)
        .withKeySchema(keySchema)
        .withAttributeDefinitions(attributeDefinitions)
        .withProvisionedThroughput(new ProvisionedThroughput()
            .withReadCapacityUnits(5L)
            .withWriteCapacityUnits(6L));
    Table numberHashNumberRangeTable = dynamoDB.createTable(request);
    numberHashNumberRangeTable.waitForActive();

    attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions
        .add(new AttributeDefinition().withAttributeName("hashN").withAttributeType("N"));

    keySchema = new ArrayList<KeySchemaElement>();
    keySchema.add(new KeySchemaElement().withAttributeName("hashN").withKeyType(KeyType.HASH));

    request = new CreateTableRequest()
        .withTableName(numberHashOnlyTableName)
        .withKeySchema(keySchema)
        .withAttributeDefinitions(attributeDefinitions)
        .withProvisionedThroughput(new ProvisionedThroughput()
            .withReadCapacityUnits(5L)
            .withWriteCapacityUnits(6L));
    Table numberHashOnlyTable = dynamoDB.createTable(request);
    numberHashOnlyTable.waitForActive();

}