com.amazonaws.services.dynamodbv2.model.ScalarAttributeType Java Examples

The following examples show how to use com.amazonaws.services.dynamodbv2.model.ScalarAttributeType. 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: MapperSaveConfigCryptoIntegrationTestBase.java    From aws-dynamodb-encryption-java with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
    DynamoDBCryptoIntegrationTestBase.setUp();
    dynamoMapper = TestDynamoDBMapperFactory.createDynamoDBMapper(dynamo);

    CreateTableRequest createTableRequest = new CreateTableRequest()
            .withTableName(tableName)
            .withKeySchema(new KeySchemaElement().withAttributeName(hashKeyName).withKeyType(KeyType.HASH))
            .withKeySchema(new KeySchemaElement().withAttributeName(rangeKeyName).withKeyType(KeyType.RANGE))
            .withAttributeDefinitions(new AttributeDefinition().withAttributeName(hashKeyName)
                    .withAttributeType(ScalarAttributeType.S))
            .withAttributeDefinitions(new AttributeDefinition().withAttributeName(rangeKeyName)
                    .withAttributeType(ScalarAttributeType.N));
    createTableRequest.setProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT);

    if (TableUtils.createTableIfNotExists(dynamo, createTableRequest)) {
        TableUtils.waitUntilActive(dynamo, tableName);
    }
}
 
Example #2
Source File: DynamoDbStore.java    From dynamodb-janusgraph-storage-backend with Apache License 2.0 6 votes vote down vote up
@Override
public CreateTableRequest getTableSchema() {
    return super.getTableSchema()
        .withAttributeDefinitions(
            new AttributeDefinition()
                .withAttributeName(Constants.JANUSGRAPH_HASH_KEY)
                .withAttributeType(ScalarAttributeType.S),
            new AttributeDefinition()
                .withAttributeName(Constants.JANUSGRAPH_RANGE_KEY)
                .withAttributeType(ScalarAttributeType.S))
        .withKeySchema(
            new KeySchemaElement()
                .withAttributeName(Constants.JANUSGRAPH_HASH_KEY)
                .withKeyType(KeyType.HASH),
            new KeySchemaElement()
                .withAttributeName(Constants.JANUSGRAPH_RANGE_KEY)
                .withKeyType(KeyType.RANGE));
}
 
Example #3
Source File: DynamoDBService.java    From Doradus with Apache License 2.0 6 votes vote down vote up
@Override
public void createStoreIfAbsent(String storeName, boolean bBinaryValues) {
    String tableName = storeToTableName(storeName);
    if (!Tables.doesTableExist(m_ddbClient, tableName)) {
        // Create a table with a primary hash key named '_key', which holds a string
        m_logger.info("Creating table: {}", tableName);
        CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
            .withKeySchema(new KeySchemaElement()
                .withAttributeName(ROW_KEY_ATTR_NAME)
                .withKeyType(KeyType.HASH))
            .withAttributeDefinitions(new AttributeDefinition()
                .withAttributeName(ROW_KEY_ATTR_NAME)
                .withAttributeType(ScalarAttributeType.S))
            .withProvisionedThroughput(new ProvisionedThroughput()
                .withReadCapacityUnits(READ_CAPACITY_UNITS)
                .withWriteCapacityUnits(WRITE_CAPACITY_UNITS));
        m_ddbClient.createTable(createTableRequest).getTableDescription();
        try {
            Tables.awaitTableToBecomeActive(m_ddbClient, tableName);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);  
        }
    }
}
 
Example #4
Source File: DynamoDBIOTestHelper.java    From beam with Apache License 2.0 6 votes vote down vote up
private static CreateTableResult createDynamoTable(String tableName) {

    ImmutableList<AttributeDefinition> attributeDefinitions =
        ImmutableList.of(
            new AttributeDefinition(ATTR_NAME_1, ScalarAttributeType.S),
            new AttributeDefinition(ATTR_NAME_2, ScalarAttributeType.N));

    ImmutableList<KeySchemaElement> ks =
        ImmutableList.of(
            new KeySchemaElement(ATTR_NAME_1, KeyType.HASH),
            new KeySchemaElement(ATTR_NAME_2, KeyType.RANGE));

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

    return dynamoDBClient.createTable(request);
  }
 
Example #5
Source File: AmazonDynamoDBStubTest.java    From aws-java-sdk-stubs with Apache License 2.0 6 votes vote down vote up
private CreateTableResult createTable() throws Exception {
  List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
  AttributeDefinition attributeDefinition = new AttributeDefinition()
    .withAttributeName(TEST_ATTRIBUTE)
    .withAttributeType(ScalarAttributeType.S);
  attributeDefinitions.add(attributeDefinition);

  String tableName = TEST_TABLE_NAME;

  List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
  KeySchemaElement keySchemaElement = new KeySchemaElement()
    .withAttributeName(TEST_ATTRIBUTE)
    .withKeyType(KeyType.HASH);

  ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput()
    .withReadCapacityUnits(UNITS)
    .withWriteCapacityUnits(UNITS);

  CreateTableResult result = dynamoDb.createTable(attributeDefinitions, tableName, keySchema, provisionedThroughput);

  return result;
}
 
Example #6
Source File: DynamoDBOperations.java    From geowave with Apache License 2.0 6 votes vote down vote up
private boolean createTable(final String qName, final boolean dataIndexTable) {
  return createTable(
      qName,
      dataIndexTable
          ? () -> new CreateTableRequest().withTableName(qName).withAttributeDefinitions(
              new AttributeDefinition(
                  DynamoDBRow.GW_PARTITION_ID_KEY,
                  ScalarAttributeType.B)).withKeySchema(
                      new KeySchemaElement(DynamoDBRow.GW_PARTITION_ID_KEY, KeyType.HASH))
          : () -> new CreateTableRequest().withTableName(qName).withAttributeDefinitions(
              new AttributeDefinition(DynamoDBRow.GW_PARTITION_ID_KEY, ScalarAttributeType.B),
              new AttributeDefinition(
                  DynamoDBRow.GW_RANGE_KEY,
                  ScalarAttributeType.B)).withKeySchema(
                      new KeySchemaElement(DynamoDBRow.GW_PARTITION_ID_KEY, KeyType.HASH),
                      new KeySchemaElement(DynamoDBRow.GW_RANGE_KEY, KeyType.RANGE)));
}
 
Example #7
Source File: ScanITCase.java    From aws-dynamodb-encryption-java with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUpTestData() throws Exception {
    String keyName = "id";
    CreateTableRequest createTableRequest = new CreateTableRequest()
            .withTableName(TABLE_NAME)
            .withKeySchema(new KeySchemaElement().withAttributeName(keyName).withKeyType(KeyType.HASH))
            .withAttributeDefinitions(
                    new AttributeDefinition().withAttributeName(keyName).withAttributeType(
                            ScalarAttributeType.S));
    createTableRequest.setProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(10L)
            .withWriteCapacityUnits(5L));

    TableUtils.createTableIfNotExists(dynamo, createTableRequest);
    TableUtils.waitUntilActive(dynamo, TABLE_NAME);

    createTestData();
}
 
Example #8
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 #9
Source File: DynamoDBCryptoIntegrationTestBase.java    From aws-dynamodb-encryption-java with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
    // Create a table
    DynamoDBTestBase.setUpTestBase();
    String keyName = KEY_NAME;
    CreateTableRequest createTableRequest = new CreateTableRequest()
            .withTableName(TABLE_NAME)
            .withKeySchema(new KeySchemaElement().withAttributeName(keyName).withKeyType(KeyType.HASH))
            .withAttributeDefinitions(
                    new AttributeDefinition().withAttributeName(keyName).withAttributeType(
                            ScalarAttributeType.S));
    createTableRequest.setProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(10L)
            .withWriteCapacityUnits(5L));

    if (TableUtils.createTableIfNotExists(dynamo, createTableRequest)) {
        TableUtils.waitUntilActive(dynamo, TABLE_NAME);
    }
}
 
Example #10
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 #11
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 #12
Source File: DynamoDBServiceImpl1.java    From Serverless-Programming-Cookbook with MIT License 6 votes vote down vote up
@Override
public final Response createTable(final Request request) {

    if (tableExist(request.getTableName())) {
        return new Response(null, request.getTableName() + " already exist. Checked with version V1.");
    }

    Table table = dynamoDB.createTable(request.getTableName(),
            Arrays.asList(
                    new KeySchemaElement(request.getPartitionKey(), KeyType.HASH),  //Partition key
                    new KeySchemaElement(request.getSortKey(), KeyType.RANGE)), //Sort key
            Arrays.asList(
                    new AttributeDefinition(request.getPartitionKey(), ScalarAttributeType.S),
                    new AttributeDefinition(request.getSortKey(), ScalarAttributeType.N)),
            new ProvisionedThroughput(request.getReadCapacityUnits(), request.getWriteCapacityUnits()));

    if (request.isWaitForActive()) {
        try {
            table.waitForActive();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    return new Response(request.getTableName() + " created with API version V1.", null);
}
 
Example #13
Source File: DynamoDBServiceImpl2.java    From Serverless-Programming-Cookbook with MIT License 6 votes vote down vote up
@Override
public final Response createTable(final Request request) {

    final CreateTableRequest createTableRequest = new CreateTableRequest(
            Arrays.asList(
                    new AttributeDefinition(request.getPartitionKey(), ScalarAttributeType.S),
                    new AttributeDefinition(request.getSortKey(), ScalarAttributeType.N)),
            request.getTableName(),
            Arrays.asList(
                    new KeySchemaElement(request.getPartitionKey(), KeyType.HASH),
                    new KeySchemaElement(request.getSortKey(), KeyType.RANGE)),
            new ProvisionedThroughput(request.getReadCapacityUnits(), request.getWriteCapacityUnits()));

    TableUtils.createTableIfNotExists(this.dynamoDBClient, createTableRequest);

    try {
        TableUtils.waitUntilActive(this.dynamoDBClient, request.getTableName());
    } catch (final AmazonClientException | InterruptedException e) {
        return new Response(null, "Failed in table active check in API version V2: " + e.getMessage());
    }

    return new Response(request.getTableName() + " created with API version V2.", null);
}
 
Example #14
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 #15
Source File: DynamoDBCryptoIntegrationTestBase.java    From aws-dynamodb-encryption-java with Apache License 2.0 6 votes vote down vote up
protected static void setUpTableWithRangeAttribute() throws Exception {
    setUp();

    String keyName = DynamoDBCryptoIntegrationTestBase.KEY_NAME;
    String rangeKeyAttributeName = "rangeKey";

    CreateTableRequest createTableRequest = new CreateTableRequest()
            .withTableName(TABLE_WITH_RANGE_ATTRIBUTE)
            .withKeySchema(new KeySchemaElement().withAttributeName(keyName).withKeyType(KeyType.HASH),
                    new KeySchemaElement().withAttributeName(rangeKeyAttributeName).withKeyType(KeyType.RANGE))
            .withAttributeDefinitions(
                    new AttributeDefinition().withAttributeName(keyName).withAttributeType(
                            ScalarAttributeType.N),
                    new AttributeDefinition().withAttributeName(rangeKeyAttributeName).withAttributeType(
                            ScalarAttributeType.N));
    createTableRequest.setProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(10L)
            .withWriteCapacityUnits(5L));

    if (TableUtils.createTableIfNotExists(dynamo, createTableRequest)) {
        TableUtils.waitUntilActive(dynamo, TABLE_WITH_RANGE_ATTRIBUTE);
    }
}
 
Example #16
Source File: AutoGeneratedKeysITCase.java    From aws-dynamodb-encryption-java with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
    DynamoDBMapperCryptoIntegrationTestBase.setUp();

    // Create a table
    String keyName = DynamoDBMapperCryptoIntegrationTestBase.KEY_NAME;
    String rangeKeyAttributeName = "rangeKey";

    CreateTableRequest createTableRequest = new CreateTableRequest()
            .withTableName(TABLE_NAME)
            .withKeySchema(new KeySchemaElement().withAttributeName(keyName).withKeyType(KeyType.HASH),
                    new KeySchemaElement().withAttributeName(rangeKeyAttributeName).withKeyType(KeyType.RANGE))
            .withAttributeDefinitions(
                    new AttributeDefinition().withAttributeName(keyName).withAttributeType(
                            ScalarAttributeType.S),
                    new AttributeDefinition().withAttributeName(rangeKeyAttributeName).withAttributeType(
                            ScalarAttributeType.S));
    createTableRequest.setProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(10L)
            .withWriteCapacityUnits(5L));

    if (TableUtils.createTableIfNotExists(dynamo, createTableRequest)) {
        TableUtils.waitUntilActive(dynamo, TABLE_NAME);
    }
}
 
Example #17
Source File: DynamoDbPropertyMarshaller.java    From Cheddar with Apache License 2.0 5 votes vote down vote up
public static ScalarAttributeType getAttributeType(final Class<?> propertyClass) {
    if (propertyClass == ByteBuffer.class || propertyClass == byte[].class) {
        return ScalarAttributeType.B;
    } else if (char.class.isAssignableFrom(propertyClass)) {
        return ScalarAttributeType.S;
    } else if (propertyClass.isPrimitive() || Number.class.isAssignableFrom(propertyClass)
            || Boolean.class.isAssignableFrom(propertyClass)) {
        return ScalarAttributeType.N;
    } else {
        return ScalarAttributeType.S;
    }
}
 
Example #18
Source File: DynamoDBService2.java    From Doradus with Apache License 2.0 5 votes vote down vote up
@Override
public void createNamespace() {
    String table = getTenant().getName();
    if(Tables.doesTableExist(m_client, table)) return;
    m_logger.info("Creating table: {}", table);
    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(table)
        .withKeySchema(
        	new KeySchemaElement()
             .withAttributeName("key")
             .withKeyType(KeyType.HASH),
            new KeySchemaElement()
             .withAttributeName("column")
             .withKeyType(KeyType.RANGE))
        .withAttributeDefinitions(
        	new AttributeDefinition()
             .withAttributeName("key")
             .withAttributeType(ScalarAttributeType.S),
        	new AttributeDefinition()
             .withAttributeName("column")
             .withAttributeType(ScalarAttributeType.S))
        .withProvisionedThroughput(new ProvisionedThroughput()
            .withReadCapacityUnits(m_readCapacityUnits)
            .withWriteCapacityUnits(m_writeCapacityUnits));
    m_client.createTable(createTableRequest);
    try {
        Tables.awaitTableToBecomeActive(m_client, table);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);  
    }
}
 
Example #19
Source File: DynamoDBManagerTest.java    From aws-dynamodb-mars-json-demo with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateTableTableAlreadyExistsCorrectKeySchema() {
    final Collection<AttributeDefinition> ads = Arrays.asList(new AttributeDefinition("Hash", ScalarAttributeType.S));
    final Collection<KeySchemaElement> kses = Arrays.asList(new KeySchemaElement("Hash", KeyType.HASH));
    final TableDescription description = new TableDescription().withAttributeDefinitions(ads).withKeySchema(kses)
        .withTableName(tableName);
    final DescribeTableResult result = new DescribeTableResult().withTable(description);
    EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result);
    final CreateTableRequest request = new CreateTableRequest().withAttributeDefinitions(ads).withKeySchema(kses)
        .withTableName(tableName);
    PowerMock.replayAll();
    assertEquals(description, DynamoDBManager.createTable(dynamoDB, request));
    PowerMock.verifyAll();
}
 
Example #20
Source File: DynamoDBManagerTest.java    From aws-dynamodb-mars-json-demo with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testCreateTableTableAlreadyExistsIncorrectKeySchema() {
    final Collection<AttributeDefinition> ads = Arrays.asList(new AttributeDefinition("Hash", ScalarAttributeType.S));
    final Collection<KeySchemaElement> kses = Arrays.asList(new KeySchemaElement("Hash", KeyType.HASH));
    final TableDescription description = new TableDescription().withAttributeDefinitions(ads).withKeySchema(kses)
        .withTableName(tableName);
    final DescribeTableResult result = new DescribeTableResult().withTable(description);
    EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result);
    final Collection<AttributeDefinition> ads2 = Arrays.asList(new AttributeDefinition("Hash2", ScalarAttributeType.S));
    final Collection<KeySchemaElement> kses2 = Arrays.asList(new KeySchemaElement("Hash2", KeyType.HASH));
    final CreateTableRequest request = new CreateTableRequest().withAttributeDefinitions(ads2).withKeySchema(kses2)
        .withTableName(tableName);
    PowerMock.replayAll();
    DynamoDBManager.createTable(dynamoDB, request);
}
 
Example #21
Source File: DynamoDBManagerTest.java    From aws-dynamodb-mars-json-demo with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateTableTableDoesNotExist() {
    final Collection<AttributeDefinition> ads = Arrays.asList(new AttributeDefinition("Hash", ScalarAttributeType.S));
    final Collection<KeySchemaElement> kses = Arrays.asList(new KeySchemaElement("Hash", KeyType.HASH));
    final TableDescription description = new TableDescription().withAttributeDefinitions(ads).withKeySchema(kses)
        .withTableName(tableName);
    final CreateTableResult cTR = new CreateTableResult().withTableDescription(description);
    EasyMock.expect(dynamoDB.describeTable(tableName)).andThrow(new ResourceNotFoundException(null));
    final CreateTableRequest request = new CreateTableRequest().withAttributeDefinitions(ads).withKeySchema(kses)
        .withTableName(tableName);
    EasyMock.expect(dynamoDB.createTable(request)).andReturn(cTR);
    PowerMock.replayAll();
    assertEquals(description, DynamoDBManager.createTable(dynamoDB, request));
    PowerMock.verifyAll();
}
 
Example #22
Source File: DynamoDBUtils.java    From amazon-kinesis-connectors with Apache License 2.0 5 votes vote down vote up
/**
 * Verifies if the table has the expected schema.
 * 
 * @param client
 *        The {@link AmazonDynamoDBClient} with Amazon DynamoDB read privileges
 * @param tableName
 *        The Amazon DynamoDB table to check
 * @param key
 *        The expected hashkey for the Amazon DynamoDB table
 * @return true if the Amazon DynamoDB table exists and the expected hashkey matches the table schema,
 *         otherwise return false
 */
private static boolean tableHasCorrectSchema(AmazonDynamoDBClient client, String tableName, String key) {
    DescribeTableRequest describeTableRequest = new DescribeTableRequest();
    describeTableRequest.setTableName(tableName);
    DescribeTableResult describeTableResult = client.describeTable(describeTableRequest);
    TableDescription tableDescription = describeTableResult.getTable();
    if (tableDescription.getAttributeDefinitions().size() != 1) {
        LOG.error("The number of attribute definitions does not match the existing table.");
        return false;
    }
    AttributeDefinition attributeDefinition = tableDescription.getAttributeDefinitions().get(0);
    if (!attributeDefinition.getAttributeName().equals(key)
            || !attributeDefinition.getAttributeType().equals(ScalarAttributeType.S.toString())) {
        LOG.error("Attribute name or type does not match existing table.");
        return false;
    }
    List<KeySchemaElement> KSEs = tableDescription.getKeySchema();
    if (KSEs.size() != 1) {
        LOG.error("The number of key schema elements does not match the existing table.");
        return false;
    }
    KeySchemaElement kse = KSEs.get(0);
    if (!kse.getAttributeName().equals(key) || !kse.getKeyType().equals(KeyType.HASH.toString())) {
        LOG.error("The hash key does not match the existing table.");
        return false;
    }
    return true;

}
 
Example #23
Source File: DynamoDBOperations.java    From geowave with Apache License 2.0 5 votes vote down vote up
@Override
public MetadataWriter createMetadataWriter(final MetadataType metadataType) {
  final String tableName = getMetadataTableName(metadataType);

  synchronized (DynamoDBOperations.tableExistsCache) {
    final Boolean tableExists = DynamoDBOperations.tableExistsCache.get(tableName);
    if ((tableExists == null) || !tableExists) {
      final boolean tableCreated =
          TableUtils.createTableIfNotExists(
              client,
              new CreateTableRequest().withTableName(tableName).withAttributeDefinitions(
                  new AttributeDefinition(
                      METADATA_PRIMARY_ID_KEY,
                      ScalarAttributeType.B)).withKeySchema(
                          new KeySchemaElement(
                              METADATA_PRIMARY_ID_KEY,
                              KeyType.HASH)).withAttributeDefinitions(
                                  new AttributeDefinition(
                                      METADATA_TIMESTAMP_KEY,
                                      ScalarAttributeType.N)).withKeySchema(
                                          new KeySchemaElement(
                                              METADATA_TIMESTAMP_KEY,
                                              KeyType.RANGE)).withProvisionedThroughput(
                                                  new ProvisionedThroughput(
                                                      Long.valueOf(5),
                                                      Long.valueOf(5))));
      if (tableCreated) {
        try {
          TableUtils.waitUntilActive(client, tableName);
        } catch (TableNeverTransitionedToStateException | InterruptedException e) {
          LOGGER.error("Unable to wait for active table '" + tableName + "'", e);
        }
      }
      DynamoDBOperations.tableExistsCache.put(tableName, true);
    }
  }

  return new DynamoDBMetadataWriter(this, tableName);
}
 
Example #24
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 #25
Source File: GeoTableUtil.java    From dynamodb-geo with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * Construct a create table request object based on GeoDataManagerConfiguration. The users can update any aspect of
 * the request and call it.
 * </p>
 * Example:
 * 
 * <pre>
 * AmazonDynamoDBClient ddb = new AmazonDynamoDBClient(new ClasspathPropertiesFileCredentialsProvider());
 * Region usWest2 = Region.getRegion(Regions.US_WEST_2);
 * ddb.setRegion(usWest2);
 * 
 * CreateTableRequest createTableRequest = GeoTableUtil.getCreateTableRequest(config);
 * CreateTableResult createTableResult = ddb.createTable(createTableRequest);
 * </pre>
 * 
 * @return Generated create table request.
 */
public static CreateTableRequest getCreateTableRequest(GeoDataManagerConfiguration config) {
	CreateTableRequest createTableRequest = new CreateTableRequest()
			.withTableName(config.getTableName())
			.withProvisionedThroughput(
					new ProvisionedThroughput().withReadCapacityUnits(10L).withWriteCapacityUnits(5L))
			.withKeySchema(
					new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName(
							config.getHashKeyAttributeName()),
					new KeySchemaElement().withKeyType(KeyType.RANGE).withAttributeName(
							config.getRangeKeyAttributeName()))
			.withAttributeDefinitions(
					new AttributeDefinition().withAttributeType(ScalarAttributeType.N).withAttributeName(
							config.getHashKeyAttributeName()),
					new AttributeDefinition().withAttributeType(ScalarAttributeType.S).withAttributeName(
							config.getRangeKeyAttributeName()),
					new AttributeDefinition().withAttributeType(ScalarAttributeType.N).withAttributeName(
							config.getGeohashAttributeName()))
			.withLocalSecondaryIndexes(
					new LocalSecondaryIndex()
							.withIndexName(config.getGeohashIndexName())
							.withKeySchema(
									new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName(
											config.getHashKeyAttributeName()),
									new KeySchemaElement().withKeyType(KeyType.RANGE).withAttributeName(
											config.getGeohashAttributeName()))
							.withProjection(new Projection().withProjectionType(ProjectionType.ALL)));

	return createTableRequest;
}
 
Example #26
Source File: TransactionExamples.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
public void setup() throws Exception {
    print("\n*** setup() ***\n");
    TableHelper tableHelper = new TableHelper(dynamodb);
    
    // 1. Verify that the transaction table exists, or create it if it doesn't exist
    print("Verifying or creating table " + TX_TABLE_NAME);
    TransactionManager.verifyOrCreateTransactionTable(dynamodb, TX_TABLE_NAME, 1, 1, null);
    
    // 2. Verify that the transaction item images table exists, or create it otherwise
    print("Verifying or creating table " + TX_IMAGES_TABLE_NAME);
    TransactionManager.verifyOrCreateTransactionImagesTable(dynamodb, TX_IMAGES_TABLE_NAME, 1, 1, null);
    
    // 3. Create a table to do transactions on
    print("Verifying or creating table " + EXAMPLE_TABLE_NAME);
    List<AttributeDefinition> attributeDefinitions = Arrays.asList(
        new AttributeDefinition().withAttributeName(EXAMPLE_TABLE_HASH_KEY).withAttributeType(ScalarAttributeType.S));
    List<KeySchemaElement> keySchema = Arrays.asList(
        new KeySchemaElement().withAttributeName(EXAMPLE_TABLE_HASH_KEY).withKeyType(KeyType.HASH));
    ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput()
        .withReadCapacityUnits(1L)
        .withWriteCapacityUnits(1L);
    
    tableHelper.verifyOrCreateTable(EXAMPLE_TABLE_NAME, attributeDefinitions, keySchema, null, provisionedThroughput, null);
    
    // 4. Wait for tables to be created
    print("Waiting for table to become ACTIVE: " + EXAMPLE_TABLE_NAME);
    tableHelper.waitForTableActive(EXAMPLE_TABLE_NAME, 5 * 60L);
    print("Waiting for table to become ACTIVE: " + TX_TABLE_NAME);
    tableHelper.waitForTableActive(TX_TABLE_NAME, 5 * 60L);
    print("Waiting for table to become ACTIVE: " + TX_IMAGES_TABLE_NAME);
    tableHelper.waitForTableActive(TX_IMAGES_TABLE_NAME, 5 * 60L);
}
 
Example #27
Source File: DynamoDBUtils.java    From amazon-kinesis-connectors with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the Amazon DynamoDB table if it does not already exist and have the correct schema. Then it
 * waits for the table to become active.
 * 
 * @param client
 *        The {@link AmazonDynamoDBClient} with Amazon DynamoDB read and write privileges
 * @param tableName
 *        The Amazon DynamoDB table to create
 * @param key
 *        The Amazon DynamoDB table hashkey
 * @param readCapacityUnits
 *        Number of read capacity units for the Amazon DynamoDB table
 * @param writeCapacityUnits
 *        Number of write capacity units for the Amazon DynamoDB table
 * @throws IllegalStateException
 *         Table already exists and schema does not match
 * @throws IllegalStateException
 *         Table is already getting created
 */
public static void createTable(AmazonDynamoDBClient client,
        String tableName,
        String key,
        long readCapacityUnits,
        long writeCapacityUnits) {
    if (tableExists(client, tableName)) {
        if (tableHasCorrectSchema(client, tableName, key)) {
            waitForActive(client, tableName);
            return;
        } else {
            throw new IllegalStateException("Table already exists and schema does not match");
        }

    }
    CreateTableRequest createTableRequest = new CreateTableRequest();
    createTableRequest.setTableName(tableName);
    createTableRequest.setKeySchema(Arrays.asList(new KeySchemaElement(key, KeyType.HASH)));
    createTableRequest.setProvisionedThroughput(new ProvisionedThroughput(readCapacityUnits, writeCapacityUnits));
    createTableRequest.setAttributeDefinitions(Arrays.asList(new AttributeDefinition(key, ScalarAttributeType.S)));
    try {
        client.createTable(createTableRequest);
    } catch (ResourceInUseException e) {
        throw new IllegalStateException("The table may already be getting created.", e);
    }
    LOG.info("Table " + tableName + " created");
    waitForActive(client, tableName);
}
 
Example #28
Source File: HashKeyOnlyTableWithGSIITCase.java    From aws-dynamodb-encryption-java with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
    DynamoDBTestBase.setUpTestBase();
    List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
    keySchema.add(new KeySchemaElement("id", KeyType.HASH));

    CreateTableRequest req = new CreateTableRequest(
            HASH_KEY_ONLY_TABLE_NAME, keySchema)
            .withProvisionedThroughput(new ProvisionedThroughput(10L, 10L))
            .withAttributeDefinitions(
                    new AttributeDefinition("id", ScalarAttributeType.S),
                    new AttributeDefinition("status", ScalarAttributeType.S),
                    new AttributeDefinition("ts", ScalarAttributeType.S))
            .withGlobalSecondaryIndexes(
                    new GlobalSecondaryIndex()
                            .withProvisionedThroughput(
                                    new ProvisionedThroughput(10L, 10L))
                            .withIndexName("statusAndCreation")
                            .withKeySchema(
                                    new KeySchemaElement("status",
                                            KeyType.HASH),
                                    new KeySchemaElement("ts",
                                            KeyType.RANGE))
                            .withProjection(
                                    new Projection()
                                            .withProjectionType(ProjectionType.ALL)));

    TableUtils.createTableIfNotExists(dynamo, req);
    TableUtils.waitUntilActive(dynamo, HASH_KEY_ONLY_TABLE_NAME);
}
 
Example #29
Source File: DynamoDSETranslatorJSONBlob.java    From dynamo-cassandra-proxy with Apache License 2.0 5 votes vote down vote up
private Object getAttributeObject(ScalarAttributeType type, AttributeValue value) {
    //Note: only string, number, and binary are allowed for primary keys in dynamodb
    switch (type) {
        case N:
            return Double.parseDouble(value.getN());
        case S:
            return value.getS();
        case B:
            return value.getB();
        default:
            throw new IllegalStateException("Unknown dynamo scalar type: " + type);
    }
}
 
Example #30
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);

}