下面列出了com.amazonaws.auth.PropertiesCredentials#com.amazonaws.services.dynamodbv2.model.AttributeDefinition 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public DynamoDBTable(
String name,
String hashKey,
Optional<String> rangeKey,
List<AttributeDefinition> knownAttributeDefinitions,
List<DynamoDBTable> indexes,
long approxTableSizeInBytes,
long approxItemCount,
long provisionedReadCapacity)
{
checkArgument(!isNullOrEmpty(name), "name is null or is empty");
this.hashKey = requireNonNull(hashKey, "hashKey is null");
this.rangeKey = requireNonNull(rangeKey, "rangeKey is null");
this.knownAttributeDefinitions = requireNonNull(knownAttributeDefinitions, "knownAttributeDefinitions is null");
this.name = requireNonNull(name, "name is null");
this.indexes = ImmutableList.copyOf(requireNonNull(indexes, "indexes is null"));
this.approxTableSizeInBytes = approxTableSizeInBytes;
this.approxItemCount = approxItemCount;
this.provisionedReadCapacity = provisionedReadCapacity;
}
private String attributeToPairs(AttributeDefinition attributeDefinition) {
String name = "\"" + attributeDefinition.getAttributeName() + "\"";
DynamoDBAttributeType type = valueOf(attributeDefinition.getAttributeType());
switch(type) {
case N:
return name + " double";
case S:
return name + " text";
case BOOL:
return name + " boolean";
case B:
return name + " blob";
default:
throw new RuntimeException("Type not supported");
}
}
@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);
}
public void waitForTableActive(String tableName,
List<AttributeDefinition> definitions,
List<KeySchemaElement> keySchema,
List<LocalSecondaryIndex> localIndexes,
long waitTimeSeconds) throws InterruptedException {
if(waitTimeSeconds < 0) {
throw new IllegalArgumentException("Invalid waitTimeSeconds " + waitTimeSeconds);
}
long startTimeMs = System.currentTimeMillis();
long elapsedMs = 0;
do {
String status = verifyTableExists(tableName, definitions, keySchema, localIndexes);
if(TableStatus.ACTIVE.toString().equals(status)) {
return;
}
if(TableStatus.DELETING.toString().equals(status)) {
throw new ResourceInUseException("Table " + tableName + " is " + status + ", and waiting for it to become ACTIVE is not useful.");
}
Thread.sleep(10 * 1000);
elapsedMs = System.currentTimeMillis() - startTimeMs;
} while(elapsedMs / 1000.0 < waitTimeSeconds);
throw new ResourceInUseException("Table " + tableName + " did not become ACTIVE after " + waitTimeSeconds + " seconds.");
}
@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);
}
@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);
}
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) {
}
});
}
private TableDescription getTableDescription(String hashType, String rangeType) {
List<KeySchemaElement> keySchema = new ArrayList<>();
List<AttributeDefinition> definitions = new ArrayList<>();
keySchema.add(new KeySchemaElement().withAttributeName("hashKey").withKeyType(KeyType.HASH));
definitions.add(new AttributeDefinition().withAttributeName("hashKey").withAttributeType
(hashType));
if (rangeType != null) {
keySchema.add(new KeySchemaElement().withAttributeName("rangeKey").withKeyType(KeyType
.RANGE));
definitions.add(new AttributeDefinition().withAttributeName("rangeKey").withAttributeType
(rangeType));
}
TableDescription description = new TableDescription().withKeySchema(keySchema)
.withAttributeDefinitions(definitions).withProvisionedThroughput(new
ProvisionedThroughputDescription().withReadCapacityUnits(1000L)
.withWriteCapacityUnits(1000L));
return description;
}
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();
}
}
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 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);
}
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);
}
}
@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));
}
@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);
}
}
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);
}
}
@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);
}
}
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;
}
@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);
}
}
}
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)));
}
/**
* 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);
}
}
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();
}
/**
* Derives an Arrow {@link Schema} for the given table by performing a small table scan and mapping the returned
* attribute values' types to Arrow types. If the table is empty, only attributes found in the table's metadata
* are added to the return schema.
*
* @param tableName the table to derive a schema for
* @param invoker the ThrottlingInvoker to call DDB with
* @param ddbClient the DDB client to use
* @return the table's derived schema
*/
public static Schema peekTableForSchema(String tableName, ThrottlingInvoker invoker, AmazonDynamoDB ddbClient)
throws TimeoutException
{
ScanRequest scanRequest = new ScanRequest().withTableName(tableName).withLimit(SCHEMA_INFERENCE_NUM_RECORDS);
ScanResult scanResult = invoker.invoke(() -> ddbClient.scan(scanRequest));
List<Map<String, AttributeValue>> items = scanResult.getItems();
Set<String> discoveredColumns = new HashSet<>();
SchemaBuilder schemaBuilder = new SchemaBuilder();
if (!items.isEmpty()) {
for (Map<String, AttributeValue> item : items) {
for (Map.Entry<String, AttributeValue> column : item.entrySet()) {
if (!discoveredColumns.contains(column.getKey())) {
Field field = DDBTypeUtils.inferArrowField(column.getKey(), ItemUtils.toSimpleValue(column.getValue()));
if (field != null) {
schemaBuilder.addField(field);
discoveredColumns.add(column.getKey());
}
}
}
}
}
else {
// there's no items, so use any attributes defined in the table metadata
DynamoDBTable table = getTable(tableName, invoker, ddbClient);
for (AttributeDefinition attributeDefinition : table.getKnownAttributeDefinitions()) {
schemaBuilder.addField(DDBTypeUtils.getArrowFieldFromDDBType(attributeDefinition.getAttributeName(), attributeDefinition.getAttributeType()));
}
}
return schemaBuilder.build();
}
private TableDescription getTableDescription(String tableName, Collection<AttributeDefinition> attributeDefinitions, Collection<KeySchemaElement> keySchema) {
TableDescription tableDescription = (new TableDescription())
.withTableName(tableName)
.withAttributeDefinitions(attributeDefinitions)
.withKeySchema(keySchema)
.withTableStatus(TableStatus.ACTIVE)
.withCreationDateTime(new Date())
.withTableArn(tableName);
return tableDescription;
}
private static void createTable(String tableName, long readCapacityUnits, long writeCapacityUnits,
String hashKeyName, String hashKeyType, String rangeKeyName, String rangeKeyType) {
try {
System.out.println("Creating table " + tableName);
ArrayList<KeySchemaElement> ks = new ArrayList<KeySchemaElement>();
ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
ks.add(new KeySchemaElement().withAttributeName(
hashKeyName).withKeyType(KeyType.HASH));
attributeDefinitions.add(new AttributeDefinition().withAttributeName(
hashKeyName).withAttributeType(hashKeyType));
if (rangeKeyName != null){
ks.add(new KeySchemaElement().withAttributeName(
rangeKeyName).withKeyType(KeyType.RANGE));
attributeDefinitions.add(new AttributeDefinition().withAttributeName(
rangeKeyName).withAttributeType(rangeKeyType));
}
// Provide initial provisioned throughput values as Java long data types
ProvisionedThroughput provisionedthroughput = new ProvisionedThroughput()
.withReadCapacityUnits(readCapacityUnits)
.withWriteCapacityUnits(writeCapacityUnits);
CreateTableRequest request = new CreateTableRequest()
.withTableName(tableName)
.withKeySchema(ks)
.withProvisionedThroughput(provisionedthroughput)
.withAttributeDefinitions(attributeDefinitions);
client.createTable(request);
} catch (AmazonServiceException ase) {
System.err.println("Failed to create table " + tableName + " " + ase);
}
}
/**
* Verifies that the table exists with the specified schema, and creates it if it does not exist.
*
* @param tableName
* @param definitions
* @param keySchema
* @param localIndexes
* @param provisionedThroughput
* @param waitTimeSeconds
* @throws InterruptedException
*/
public void verifyOrCreateTable(
String tableName,
List<AttributeDefinition> definitions,
List<KeySchemaElement> keySchema,
List<LocalSecondaryIndex> localIndexes,
ProvisionedThroughput provisionedThroughput,
Long waitTimeSeconds) throws InterruptedException {
if(waitTimeSeconds != null && waitTimeSeconds < 0) {
throw new IllegalArgumentException("Invalid waitTimeSeconds " + waitTimeSeconds);
}
String status = null;
try {
status = verifyTableExists(tableName, definitions, keySchema, localIndexes);
} catch(ResourceNotFoundException e) {
status = client.createTable(new CreateTableRequest()
.withTableName(tableName)
.withAttributeDefinitions(definitions)
.withKeySchema(keySchema)
.withLocalSecondaryIndexes(localIndexes)
.withProvisionedThroughput(provisionedThroughput)).getTableDescription().getTableStatus();
}
if(waitTimeSeconds != null && ! TableStatus.ACTIVE.toString().equals(status)) {
waitForTableActive(tableName, definitions, keySchema, localIndexes, waitTimeSeconds);
}
}