下面列出了com.amazonaws.auth.PropertiesCredentials#com.amazonaws.services.dynamodbv2.model.CreateTableRequest 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Before
public void setup() {
try {
repository = new ProductInfoRepository();
repository.setMapper(dynamoDBMapper);
CreateTableRequest tableRequest = dynamoDBMapper.generateCreateTableRequest(ProductInfo.class);
tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
amazonDynamoDB.createTable(tableRequest);
} catch (ResourceInUseException e) {
// Do nothing, table already created
}
// TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfo on table
dynamoDBMapper.batchDelete((List<ProductInfo>) repository.findAll());
}
@Override
public DynamoDBResponse createTable(CreateTableRequest createTableRequest) throws IOException {
logger.info("creating JSON table");
String columnPairs = createTableRequest.getAttributeDefinitions().stream().map(this::attributeToPairs).collect(Collectors.joining(", "));
columnPairs += ",json_blob text";
String keyspace = keyspaceName;
String table = createTableRequest.getTableName();
String primaryKey = getPrimaryKey(createTableRequest.getKeySchema());
String statement = String.format("CREATE TABLE IF NOT EXISTS %s.\"%s\" ( %s, PRIMARY KEY %s);\n", keyspace, table, columnPairs, primaryKey);
ResultSet result = session().execute(statement);
if (result.wasApplied()) {
logger.info("created {} as {}", table, statement);
cassandraManager.refreshSchema();
TableDescription newTableDesc = this.getTableDescription(table, createTableRequest.getAttributeDefinitions(), createTableRequest.getKeySchema());
CreateTableResult createResult = (new CreateTableResult()).withTableDescription(newTableDesc);
return new DynamoDBResponse(createResult, 200);
}
return null;
}
@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);
}
@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);
}
private static Future<CreateTableResult> createTableAsync(final AmazonDynamoDBAsync 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));
return dbClient.createTableAsync(createTableRequest, new AsyncHandler<CreateTableRequest,CreateTableResult>() {
@Override
public void onError(final Exception exception) {
}
@Override
public void onSuccess(final CreateTableRequest request, final CreateTableResult createTableResult) {
}
});
}
@Bean
public CommandLineRunner multirepo(ConfigurableApplicationContext ctx, CustomerRepository jpaRepository,
DeviceRepository dynamoDBRepository, AmazonDynamoDB amazonDynamoDB, DynamoDBMapper dynamoDBMapper,
DynamoDBMapperConfig config) {
return (args) -> {
demoJPA(jpaRepository);
CreateTableRequest ctr = dynamoDBMapper.generateCreateTableRequest(Device.class)
.withProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
TableUtils.createTableIfNotExists(amazonDynamoDB, ctr);
TableUtils.waitUntilActive(amazonDynamoDB, ctr.getTableName());
demoDynamoDB(dynamoDBRepository);
ctx.close();
};
}
@Bean
public CommandLineRunner rest(ConfigurableApplicationContext ctx, UserRepository dynamoDBRepository,
AmazonDynamoDB amazonDynamoDB, DynamoDBMapper dynamoDBMapper, DynamoDBMapperConfig config) {
return (args) -> {
CreateTableRequest ctr = dynamoDBMapper.generateCreateTableRequest(User.class)
.withProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
TableUtils.createTableIfNotExists(amazonDynamoDB, ctr);
TableUtils.waitUntilActive(amazonDynamoDB, ctr.getTableName());
createEntities(dynamoDBRepository);
log.info("");
log.info("Run curl -v http://localhost:8080/users and follow the HATEOS links");
log.info("");
log.info("Press <enter> to shutdown");
System.in.read();
ctx.close();
};
}
public CreateTableRequest constructCreateTableRequest() {
ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<>();
attributeDefinitions.add(new AttributeDefinition().withAttributeName(partitionKeyName.toString()).withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition().withAttributeName(sortKeyName.toString()).withAttributeType("N"));
ArrayList<KeySchemaElement> keySchema = new ArrayList<>();
keySchema.add(new KeySchemaElement().withAttributeName(partitionKeyName.toString()).withKeyType(KeyType.HASH));
keySchema.add(new KeySchemaElement().withAttributeName(sortKeyName.toString()).withKeyType(KeyType.RANGE));
ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput()
.withReadCapacityUnits(1L)
.withWriteCapacityUnits(1L);
CreateTableRequest request = new CreateTableRequest()
.withTableName(tableName)
.withKeySchema(keySchema)
.withAttributeDefinitions(attributeDefinitions)
.withProvisionedThroughput(provisionedThroughput);
return request;
}
@Test
public void testCreateTableWithWait() throws Exception {
// Create fake responses from AWS. First response is still creating the table, second response the table
// has become active.
TableDescription creatingDescription = constructTableDescription(TableStatus.CREATING);
TableDescription createdDescription = constructTableDescription(TableStatus.ACTIVE);
CreateTableResult mockCreateResult = new CreateTableResult().withTableDescription(creatingDescription);
DescribeTableResult mockDescribeResultCreating = new DescribeTableResult().withTable(creatingDescription);
DescribeTableResult mockDescribeResultCreated = new DescribeTableResult().withTable(createdDescription);
// Create the table.
CreateTableRequest expectedRequest = dynamoDB.constructCreateTableRequest();
when(mockDynamoDBClient.createTable(expectedRequest)).thenReturn(mockCreateResult);
when(mockDynamoDBClient.describeTable(tableName)).thenReturn(mockDescribeResultCreating, mockDescribeResultCreated);
assertEquals(dynamoDB.create(), TEST_ARN);
verify(mockDynamoDBClient, times(1)).createTable(expectedRequest);
verify(mockDynamoDBClient, times(2)).describeTable(tableName);
}
static void createExampleTable() {
// Provide the initial provisioned throughput values as Java long data
// types
ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(5L)
.withWriteCapacityUnits(6L);
CreateTableRequest request = new CreateTableRequest().withTableName(tableName)
.withProvisionedThroughput(provisionedThroughput);
ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N"));
request.setAttributeDefinitions(attributeDefinitions);
ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
tableKeySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH)); // Partition
// key
request.setKeySchema(tableKeySchema);
client.createTable(request);
waitForTableToBecomeAvailable(tableName);
getTableInformation();
}
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);
}
/**
* Used to create the Identity Table. This function only needs to be called
* once.
*/
protected void createIdentityTable() throws DataAccessException {
ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput()
.withReadCapacityUnits(10L)
.withWriteCapacityUnits(5L);
ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
attributeDefinitions
.add(new AttributeDefinition().withAttributeName(ATTRIBUTE_USERNAME).withAttributeType("S"));
ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
tableKeySchema.add(new KeySchemaElement().withAttributeName(ATTRIBUTE_USERNAME).withKeyType(KeyType.HASH));
CreateTableRequest createTableRequest = new CreateTableRequest()
.withTableName(USER_TABLE)
.withProvisionedThroughput(provisionedThroughput)
.withAttributeDefinitions(attributeDefinitions)
.withKeySchema(tableKeySchema);
try {
ddb.createTable(createTableRequest);
} catch (AmazonClientException e) {
throw new DataAccessException("Failed to create table: " + USER_TABLE, e);
}
}
/**
* Used to create the device table. This function only needs to be called
* once.
*/
protected void createDeviceTable() throws DataAccessException {
ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput()
.withReadCapacityUnits(10L)
.withWriteCapacityUnits(5L);
ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition().withAttributeName(
ATTRIBUTE_UID).withAttributeType("S"));
ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
tableKeySchema.add(new KeySchemaElement().withAttributeName(ATTRIBUTE_UID)
.withKeyType(KeyType.HASH));
CreateTableRequest createTableRequest = new CreateTableRequest()
.withTableName(DEVICE_TABLE)
.withProvisionedThroughput(provisionedThroughput)
.withAttributeDefinitions(attributeDefinitions)
.withKeySchema(tableKeySchema);
try {
ddb.createTable(createTableRequest);
} catch (AmazonClientException e) {
throw new DataAccessException("Failed to create table: " + DEVICE_TABLE, e);
}
}
void createTableAndWaitForActive(final CreateTableRequest request) throws BackendException {
final String tableName = request.getTableName();
Preconditions.checkArgument(!Strings.isNullOrEmpty(tableName), "Table name was null or empty");
final TableDescription desc;
try {
desc = this.describeTable(tableName);
if (null != desc && isTableAcceptingWrites(desc.getTableStatus())) {
return; //store existed
}
} catch (BackendNotFoundException e) {
log.debug(tableName + " did not exist yet, creating it", e);
}
createTable(request);
waitForTableCreation(tableName, false /*verifyIndexesList*/, null /*expectedLsiList*/, null /*expectedGsiList*/);
}
@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));
}
public void createTable(Class<? extends IDomain> domain){
CreateTableRequest tableRequest = dynamoDBMapper.generateCreateTableRequest(domain);
tableRequest = tableRequest.withProvisionedThroughput(new ProvisionedThroughput(5L,5L));
//check whether or not we need to add a provisioning throughput value for GSI
for (Method method : domain.getMethods()) {
if(method.isAnnotationPresent(DynamoDBIndexHashKey.class)){
String tempGSI = method.getAnnotation(DynamoDBIndexHashKey.class).globalSecondaryIndexName();
for (GlobalSecondaryIndex globalSecondaryIndex : tableRequest.getGlobalSecondaryIndexes()) {
if(globalSecondaryIndex.getIndexName().equals(tempGSI)){
globalSecondaryIndex.setProvisionedThroughput(new ProvisionedThroughput(5L,5L));
}
}
}
}
amazonDynamoDBClient.createTable(tableRequest);
}
@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();
}
@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);
}
}
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);
}
}
@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);
}
}
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);
}
@Test
public void testCreateResourceTable() {
final AmazonDynamoDB dynamoDB = PowerMock.createMock(AmazonDynamoDB.class);
PowerMock.mockStatic(DynamoDBManager.class);
final CreateTableRequest request = new CreateTableRequest();
request.setAttributeDefinitions(MarsDynamoDBManager.RESOURCE_TABLE_ATTRIBUTE_DEFINITIONS);
request.setKeySchema(MarsDynamoDBManager.RESOURCE_TABLE_KEY_SCHEMA);
request.setProvisionedThroughput(PROVISIONED_THROUGHPUT);
request.setTableName(TABLE_NAME);
DynamoDBManager.createTable(dynamoDB, request);
PowerMock.expectLastCall().andReturn(null);
PowerMock.replayAll();
MarsDynamoDBManager.createResourceTable(dynamoDB, TABLE_NAME, PROVISIONED_THROUGHPUT);
PowerMock.verifyAll();
}
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)));
}
private boolean createTable(final String qName, final Supplier<CreateTableRequest> tableRequest) {
synchronized (tableExistsCache) {
final Boolean tableExists = tableExistsCache.get(qName);
if ((tableExists == null) || !tableExists) {
final boolean tableCreated =
TableUtils.createTableIfNotExists(
client,
tableRequest.get().withProvisionedThroughput(
new ProvisionedThroughput(
Long.valueOf(options.getReadCapacity()),
Long.valueOf(options.getWriteCapacity()))));
if (tableCreated) {
try {
TableUtils.waitUntilActive(client, qName);
} catch (TableNeverTransitionedToStateException | InterruptedException e) {
LOGGER.error("Unable to wait for active table '" + qName + "'", e);
}
}
tableExistsCache.put(qName, true);
return true;
}
}
return false;
}
/**
* Used to create the device table. This function only needs to be called
* once.
*/
protected void createDeviceTable() throws DataAccessException {
ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput()
.withReadCapacityUnits(10L)
.withWriteCapacityUnits(5L);
ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition().withAttributeName(
ATTRIBUTE_UID).withAttributeType("S"));
ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
tableKeySchema.add(new KeySchemaElement().withAttributeName(ATTRIBUTE_UID)
.withKeyType(KeyType.HASH));
CreateTableRequest createTableRequest = new CreateTableRequest()
.withTableName(DEVICE_TABLE)
.withProvisionedThroughput(provisionedThroughput)
.withAttributeDefinitions(attributeDefinitions)
.withKeySchema(tableKeySchema);
try {
ddb.createTable(createTableRequest);
} catch (AmazonClientException e) {
throw new DataAccessException("Failed to create table: " + DEVICE_TABLE, e);
}
}
/**
* Used to create the Identity Table. This function only needs to be called
* once.
*/
protected void createIdentityTable() throws DataAccessException {
ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput()
.withReadCapacityUnits(10L)
.withWriteCapacityUnits(5L);
ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
attributeDefinitions
.add(new AttributeDefinition().withAttributeName(ATTRIBUTE_USERNAME).withAttributeType("S"));
ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
tableKeySchema.add(new KeySchemaElement().withAttributeName(ATTRIBUTE_USERNAME).withKeyType(KeyType.HASH));
CreateTableRequest createTableRequest = new CreateTableRequest()
.withTableName(USER_TABLE)
.withProvisionedThroughput(provisionedThroughput)
.withAttributeDefinitions(attributeDefinitions)
.withKeySchema(tableKeySchema);
try {
ddb.createTable(createTableRequest);
} catch (AmazonClientException e) {
throw new DataAccessException("Failed to create table: " + USER_TABLE, e);
}
}
/**
* Used to create the device table. This function only needs to be called
* once.
*/
protected void createDeviceTable() throws DataAccessException {
ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput()
.withReadCapacityUnits(10L)
.withWriteCapacityUnits(5L);
ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition().withAttributeName(
ATTRIBUTE_UID).withAttributeType("S"));
ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
tableKeySchema.add(new KeySchemaElement().withAttributeName(ATTRIBUTE_UID)
.withKeyType(KeyType.HASH));
CreateTableRequest createTableRequest = new CreateTableRequest()
.withTableName(DEVICE_TABLE)
.withProvisionedThroughput(provisionedThroughput)
.withAttributeDefinitions(attributeDefinitions)
.withKeySchema(tableKeySchema);
try {
ddb.createTable(createTableRequest);
} catch (AmazonClientException e) {
throw new DataAccessException("Failed to create table: " + DEVICE_TABLE, e);
}
}
@Before
public void setup() throws Exception {
try {
dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB);
CreateTableRequest tableRequest = dynamoDBMapper.generateCreateTableRequest(ProductInfo.class);
tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
amazonDynamoDB.createTable(tableRequest);
} catch (ResourceInUseException e) {
// Do nothing, table already created
}
// TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfo on table
dynamoDBMapper.batchDelete((List<ProductInfo>) repository.findAll());
}
static void createExampleTable() {
// Provide the initial provisioned throughput values as Java long data types
ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput()
.withReadCapacityUnits(5L)
.withWriteCapacityUnits(6L);
CreateTableRequest request = new CreateTableRequest()
.withTableName(tableName)
.withProvisionedThroughput(provisionedThroughput);
ArrayList<AttributeDefinition> attributeDefinitions= new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N"));
request.setAttributeDefinitions(attributeDefinitions);
ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
tableKeySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH));
request.setKeySchema(tableKeySchema);
client.createTable(request);
waitForTableToBecomeAvailable(tableName);
getTableInformation();
}
@Before
public void setup() {
tableName = "etl001";
ddb = DynamoDBEmbedded.create().amazonDynamoDB();
CreateTableRequest createTableRequest = new CreateTableRequest()
.withTableName(tableName)
.withProvisionedThroughput(new ProvisionedThroughput(10L, 10L))
.withAttributeDefinitions(new AttributeDefinition("pk", "S"))
.withKeySchema(new KeySchemaElement("pk", "HASH"));
ddb.createTable(createTableRequest);
}