下面列出了怎么用com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex的API类实例代码及写法,或者点击链接到github查看源代码。
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 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);
}
/**
* Creates the table that stores images.
*
* @param dynamoDB
* {@link AmazonDynamoDB} used to create the image table
* @param tableName
* name of the table to create
* @param tableProvisionedThroughput
* initial provisioned throughput for the table
* @param timeGSIProvisionedThroughput
* initial provisioned throughput for the time-based global secondary index
* @param voteGSIProvisionedThroughput
* initial provisioned throughput for the vote-based global secondary index
* @see <a href="http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html">Global Secondary
* Indexes</a> ======= initial provisioned throughput for the time-based global secondary index
* @see <a
* href="http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ProvisionedThroughputIntro.html">Provisioned
* Throughput in Amazon DynamoDB</a>
*/
public static void createImageTable(final AmazonDynamoDB dynamoDB, final String tableName,
final ProvisionedThroughput tableProvisionedThroughput,
final ProvisionedThroughput timeGSIProvisionedThroughput,
final ProvisionedThroughput voteGSIProvisionedThroughput) {
// Set up time GSI
final GlobalSecondaryIndex timeGSI = new GlobalSecondaryIndex();
timeGSI.setIndexName(IMAGE_TABLE_TIME_GSI_NAME);
timeGSI.setKeySchema(Arrays.asList(IMAGE_TABLE_TIME_GSI_HASH_KSE, IMAGE_TABLE_TIME_GSI_RANGE_KSE));
timeGSI.setProjection(IMAGE_TABLE_TIME_GSI_PROJECTION);
timeGSI.setProvisionedThroughput(timeGSIProvisionedThroughput);
// Set up vote GSI
final GlobalSecondaryIndex voteGSI = new GlobalSecondaryIndex();
voteGSI.setIndexName(IMAGE_TABLE_VOTE_GSI_NAME);
voteGSI.setKeySchema(Arrays.asList(IMAGE_TABLE_VOTE_GSI_HASH_KSE, IMAGE_TABLE_VOTE_GSI_RANGE_KSE));
voteGSI.setProjection(IMAGE_TABLE_VOTE_GSI_PROJECTION);
voteGSI.setProvisionedThroughput(voteGSIProvisionedThroughput);
// Create table
final CreateTableRequest request = new CreateTableRequest();
request.setAttributeDefinitions(IMAGE_TABLE_ATTRIBUTE_DEFINITIONS);
request.setKeySchema(IMAGE_TABLE_KEY_SCHEMA);
request.setGlobalSecondaryIndexes(Arrays.asList(timeGSI, voteGSI));
request.setProvisionedThroughput(tableProvisionedThroughput);
request.setTableName(tableName);
LOGGER.info("Creating image table: " + request);
final TableDescription result = DynamoDBManager.createTable(dynamoDB, request);
LOGGER.info("Image table successfully created: " + result);
}
@Test
public void testCreateImageTable() {
final AmazonDynamoDB dynamoDB = PowerMock.createMock(AmazonDynamoDB.class);
PowerMock.mockStatic(DynamoDBManager.class);
final CreateTableRequest request = new CreateTableRequest();
request.setAttributeDefinitions(MarsDynamoDBManager.IMAGE_TABLE_ATTRIBUTE_DEFINITIONS);
request.setKeySchema(MarsDynamoDBManager.IMAGE_TABLE_KEY_SCHEMA);
final GlobalSecondaryIndex timeGSI = new GlobalSecondaryIndex();
timeGSI.setIndexName(MarsDynamoDBManager.IMAGE_TABLE_TIME_GSI_NAME);
timeGSI.setKeySchema(Arrays.asList(MarsDynamoDBManager.IMAGE_TABLE_TIME_GSI_HASH_KSE,
MarsDynamoDBManager.IMAGE_TABLE_TIME_GSI_RANGE_KSE));
timeGSI.setProjection(MarsDynamoDBManager.IMAGE_TABLE_TIME_GSI_PROJECTION);
timeGSI.setProvisionedThroughput(PROVISIONED_THROUGHPUT);
final GlobalSecondaryIndex voteGSI = new GlobalSecondaryIndex();
voteGSI.setIndexName(MarsDynamoDBManager.IMAGE_TABLE_VOTE_GSI_NAME);
voteGSI.setKeySchema(Arrays.asList(MarsDynamoDBManager.IMAGE_TABLE_VOTE_GSI_HASH_KSE,
MarsDynamoDBManager.IMAGE_TABLE_VOTE_GSI_RANGE_KSE));
voteGSI.setProjection(MarsDynamoDBManager.IMAGE_TABLE_VOTE_GSI_PROJECTION);
voteGSI.setProvisionedThroughput(PROVISIONED_THROUGHPUT);
request.setGlobalSecondaryIndexes(Arrays.asList(timeGSI, voteGSI));
request.setProvisionedThroughput(PROVISIONED_THROUGHPUT);
request.setTableName(TABLE_NAME);
DynamoDBManager.createTable(dynamoDB, request);
PowerMock.expectLastCall().andReturn(null);
PowerMock.replayAll();
MarsDynamoDBManager.createImageTable(dynamoDB, TABLE_NAME, PROVISIONED_THROUGHPUT, PROVISIONED_THROUGHPUT,
PROVISIONED_THROUGHPUT);
PowerMock.verifyAll();
}
public static void createTable() {
// Attribute definitions
ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition().withAttributeName("IssueId").withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition().withAttributeName("Title").withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition().withAttributeName("CreateDate").withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition().withAttributeName("DueDate").withAttributeType("S"));
// Key schema for table
ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
tableKeySchema.add(new KeySchemaElement().withAttributeName("IssueId").withKeyType(KeyType.HASH)); // Partition
// key
tableKeySchema.add(new KeySchemaElement().withAttributeName("Title").withKeyType(KeyType.RANGE)); // Sort
// key
// Initial provisioned throughput settings for the indexes
ProvisionedThroughput ptIndex = new ProvisionedThroughput().withReadCapacityUnits(1L)
.withWriteCapacityUnits(1L);
// CreateDateIndex
GlobalSecondaryIndex createDateIndex = new GlobalSecondaryIndex().withIndexName("CreateDateIndex")
.withProvisionedThroughput(ptIndex)
.withKeySchema(new KeySchemaElement().withAttributeName("CreateDate").withKeyType(KeyType.HASH), // Partition
// key
new KeySchemaElement().withAttributeName("IssueId").withKeyType(KeyType.RANGE)) // Sort
// key
.withProjection(
new Projection().withProjectionType("INCLUDE").withNonKeyAttributes("Description", "Status"));
// TitleIndex
GlobalSecondaryIndex titleIndex = new GlobalSecondaryIndex().withIndexName("TitleIndex")
.withProvisionedThroughput(ptIndex)
.withKeySchema(new KeySchemaElement().withAttributeName("Title").withKeyType(KeyType.HASH), // Partition
// key
new KeySchemaElement().withAttributeName("IssueId").withKeyType(KeyType.RANGE)) // Sort
// key
.withProjection(new Projection().withProjectionType("KEYS_ONLY"));
// DueDateIndex
GlobalSecondaryIndex dueDateIndex = new GlobalSecondaryIndex().withIndexName("DueDateIndex")
.withProvisionedThroughput(ptIndex)
.withKeySchema(new KeySchemaElement().withAttributeName("DueDate").withKeyType(KeyType.HASH)) // Partition
// key
.withProjection(new Projection().withProjectionType("ALL"));
CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
.withProvisionedThroughput(
new ProvisionedThroughput().withReadCapacityUnits((long) 1).withWriteCapacityUnits((long) 1))
.withAttributeDefinitions(attributeDefinitions).withKeySchema(tableKeySchema)
.withGlobalSecondaryIndexes(createDateIndex, titleIndex, dueDateIndex);
System.out.println("Creating table " + tableName + "...");
System.out.println(client.createTable(createTableRequest));
waitForTableToBecomeAvailable(tableName);
}
public static void createTable() {
// Attribute definitions
ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition().withAttributeName("IssueId").withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition().withAttributeName("Title").withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition().withAttributeName("CreateDate").withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition().withAttributeName("DueDate").withAttributeType("S"));
// Key schema for table
ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
tableKeySchema.add(new KeySchemaElement().withAttributeName("IssueId").withKeyType(KeyType.HASH)); // Partition
// key
tableKeySchema.add(new KeySchemaElement().withAttributeName("Title").withKeyType(KeyType.RANGE)); // Sort
// key
// Initial provisioned throughput settings for the indexes
ProvisionedThroughput ptIndex = new ProvisionedThroughput().withReadCapacityUnits(1L)
.withWriteCapacityUnits(1L);
// CreateDateIndex
GlobalSecondaryIndex createDateIndex = new GlobalSecondaryIndex().withIndexName("CreateDateIndex")
.withProvisionedThroughput(ptIndex)
.withKeySchema(new KeySchemaElement().withAttributeName("CreateDate").withKeyType(KeyType.HASH), // Partition
// key
new KeySchemaElement().withAttributeName("IssueId").withKeyType(KeyType.RANGE)) // Sort
// key
.withProjection(
new Projection().withProjectionType("INCLUDE").withNonKeyAttributes("Description", "Status"));
// TitleIndex
GlobalSecondaryIndex titleIndex = new GlobalSecondaryIndex().withIndexName("TitleIndex")
.withProvisionedThroughput(ptIndex)
.withKeySchema(new KeySchemaElement().withAttributeName("Title").withKeyType(KeyType.HASH), // Partition
// key
new KeySchemaElement().withAttributeName("IssueId").withKeyType(KeyType.RANGE)) // Sort
// key
.withProjection(new Projection().withProjectionType("KEYS_ONLY"));
// DueDateIndex
GlobalSecondaryIndex dueDateIndex = new GlobalSecondaryIndex().withIndexName("DueDateIndex")
.withProvisionedThroughput(ptIndex)
.withKeySchema(new KeySchemaElement().withAttributeName("DueDate").withKeyType(KeyType.HASH)) // Partition
// key
.withProjection(new Projection().withProjectionType("ALL"));
CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
.withProvisionedThroughput(
new ProvisionedThroughput().withReadCapacityUnits((long) 1).withWriteCapacityUnits((long) 1))
.withAttributeDefinitions(attributeDefinitions).withKeySchema(tableKeySchema)
.withGlobalSecondaryIndexes(createDateIndex, titleIndex, dueDateIndex);
System.out.println("Creating table " + tableName + "...");
dynamoDB.createTable(createTableRequest);
// Wait for table to become active
System.out.println("Waiting for " + tableName + " to become ACTIVE...");
try {
Table table = dynamoDB.getTable(tableName);
table.waitForActive();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void createTable() {
// Attribute definitions
ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("IssueId").withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("Title").withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("CreateDate").withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("DueDate").withAttributeType("S"));
// Key schema for table
ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
tableKeySchema.add(new KeySchemaElement()
.withAttributeName("IssueId").withKeyType(KeyType.HASH));
tableKeySchema.add(new KeySchemaElement()
.withAttributeName("Title").withKeyType(KeyType.RANGE));
// Initial provisioned throughput settings for the indexes
ProvisionedThroughput ptIndex = new ProvisionedThroughput()
.withReadCapacityUnits(1L).withWriteCapacityUnits(1L);
// CreateDateIndex
GlobalSecondaryIndex createDateIndex = new GlobalSecondaryIndex()
.withIndexName("CreateDateIndex")
.withProvisionedThroughput(ptIndex)
.withKeySchema(
new KeySchemaElement()
.withAttributeName("CreateDate").withKeyType(KeyType.HASH),
new KeySchemaElement()
.withAttributeName("IssueId")
.withKeyType(KeyType.RANGE))
.withProjection(new Projection()
.withProjectionType("INCLUDE")
.withNonKeyAttributes("Description", "Status"));
// TitleIndex
GlobalSecondaryIndex titleIndex = new GlobalSecondaryIndex()
.withIndexName("TitleIndex")
.withProvisionedThroughput(ptIndex)
.withKeySchema(
new KeySchemaElement()
.withAttributeName("Title")
.withKeyType(KeyType.HASH),
new KeySchemaElement()
.withAttributeName("IssueId")
.withKeyType(KeyType.RANGE))
.withProjection(new Projection()
.withProjectionType("KEYS_ONLY"));
// DueDateIndex
GlobalSecondaryIndex dueDateIndex = new GlobalSecondaryIndex()
.withIndexName("DueDateIndex")
.withProvisionedThroughput(ptIndex)
.withKeySchema(
new KeySchemaElement()
.withAttributeName("DueDate")
.withKeyType(KeyType.HASH))
.withProjection(new Projection()
.withProjectionType("ALL"));
CreateTableRequest createTableRequest = new CreateTableRequest()
.withTableName(tableName)
.withProvisionedThroughput( new ProvisionedThroughput()
.withReadCapacityUnits( (long) 1)
.withWriteCapacityUnits( (long) 1))
.withAttributeDefinitions(attributeDefinitions)
.withKeySchema(tableKeySchema)
.withGlobalSecondaryIndexes(createDateIndex, titleIndex, dueDateIndex);
System.out.println("Creating table " + tableName + "...");
System.out.println(client.createTable(createTableRequest));
waitForTableToBecomeAvailable(tableName);
}
public static void createTable() {
// Attribute definitions
ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("IssueId")
.withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("Title")
.withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("CreateDate")
.withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition()
.withAttributeName("DueDate")
.withAttributeType("S"));
// Key schema for table
ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
tableKeySchema.add(new KeySchemaElement()
.withAttributeName("IssueId")
.withKeyType(KeyType.HASH));
tableKeySchema.add(new KeySchemaElement()
.withAttributeName("Title")
.withKeyType(KeyType.RANGE));
// Initial provisioned throughput settings for the indexes
ProvisionedThroughput ptIndex = new ProvisionedThroughput()
.withReadCapacityUnits(1L)
.withWriteCapacityUnits(1L);
// CreateDateIndex
GlobalSecondaryIndex createDateIndex = new GlobalSecondaryIndex()
.withIndexName("CreateDateIndex")
.withProvisionedThroughput(ptIndex)
.withKeySchema( new KeySchemaElement()
.withAttributeName("CreateDate")
.withKeyType(
KeyType.HASH),
new KeySchemaElement()
.withAttributeName("IssueId")
.withKeyType(KeyType.RANGE))
.withProjection(new Projection()
.withProjectionType("INCLUDE")
.withNonKeyAttributes("Description", "Status"));
// TitleIndex
GlobalSecondaryIndex titleIndex = new GlobalSecondaryIndex()
.withIndexName("TitleIndex")
.withProvisionedThroughput(ptIndex)
.withKeySchema(new KeySchemaElement()
.withAttributeName("Title")
.withKeyType(KeyType.HASH),
new KeySchemaElement()
.withAttributeName("IssueId")
.withKeyType(KeyType.RANGE))
.withProjection(new Projection()
.withProjectionType("KEYS_ONLY"));
// DueDateIndex
GlobalSecondaryIndex dueDateIndex = new GlobalSecondaryIndex()
.withIndexName("DueDateIndex")
.withProvisionedThroughput(ptIndex)
.withKeySchema( new KeySchemaElement()
.withAttributeName("DueDate")
.withKeyType(KeyType.HASH))
.withProjection(new Projection()
.withProjectionType("ALL"));
CreateTableRequest createTableRequest = new CreateTableRequest()
.withTableName(tableName)
.withProvisionedThroughput( new ProvisionedThroughput()
.withReadCapacityUnits( (long) 1)
.withWriteCapacityUnits( (long) 1))
.withAttributeDefinitions(attributeDefinitions)
.withKeySchema(tableKeySchema)
.withGlobalSecondaryIndexes(createDateIndex, titleIndex, dueDateIndex);
System.out.println("Creating table " + tableName + "...");
dynamoDB.createTable(createTableRequest);
// Wait for table to become active
System.out.println("Waiting for " + tableName + " to become ACTIVE...");
try {
Table table = dynamoDB.getTable(tableName);
table.waitForActive();
} catch (InterruptedException e) {
e.printStackTrace();
}
}