use of com.amazonaws.services.dynamodbv2.document.Index in project aws-doc-sdk-examples by awsdocs.
the class LowLevelLocalSecondaryIndexExample method createTable.
public static void createTable() {
CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName).withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits((long) 1).withWriteCapacityUnits((long) 1));
// Attribute definitions for table partition key and sort key
ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition().withAttributeName("CustomerId").withAttributeType("S"));
attributeDefinitions.add(new AttributeDefinition().withAttributeName("OrderId").withAttributeType("N"));
// Attribute definition for index sort key attributes
attributeDefinitions.add(new AttributeDefinition().withAttributeName("OrderCreationDate").withAttributeType("N"));
attributeDefinitions.add(new AttributeDefinition().withAttributeName("IsOpen").withAttributeType("N"));
createTableRequest.setAttributeDefinitions(attributeDefinitions);
// Key schema for table
ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
// Partition
tableKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH));
// key
// Sort
tableKeySchema.add(new KeySchemaElement().withAttributeName("OrderId").withKeyType(KeyType.RANGE));
// key
createTableRequest.setKeySchema(tableKeySchema);
ArrayList<LocalSecondaryIndex> localSecondaryIndexes = new ArrayList<LocalSecondaryIndex>();
// OrderCreationDateIndex
LocalSecondaryIndex orderCreationDateIndex = new LocalSecondaryIndex().withIndexName("OrderCreationDateIndex");
// Key schema for OrderCreationDateIndex
ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<KeySchemaElement>();
// Partition
indexKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH));
// key
// Sort
indexKeySchema.add(new KeySchemaElement().withAttributeName("OrderCreationDate").withKeyType(KeyType.RANGE));
// key
orderCreationDateIndex.setKeySchema(indexKeySchema);
// Projection (with list of projected attributes) for
// OrderCreationDateIndex
Projection projection = new Projection().withProjectionType(ProjectionType.INCLUDE);
ArrayList<String> nonKeyAttributes = new ArrayList<String>();
nonKeyAttributes.add("ProductCategory");
nonKeyAttributes.add("ProductName");
projection.setNonKeyAttributes(nonKeyAttributes);
orderCreationDateIndex.setProjection(projection);
localSecondaryIndexes.add(orderCreationDateIndex);
// IsOpenIndex
LocalSecondaryIndex isOpenIndex = new LocalSecondaryIndex().withIndexName("IsOpenIndex");
// Key schema for IsOpenIndex
indexKeySchema = new ArrayList<KeySchemaElement>();
// Partition
indexKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH));
// key
// Sort
indexKeySchema.add(new KeySchemaElement().withAttributeName("IsOpen").withKeyType(KeyType.RANGE));
// key
// Projection (all attributes) for IsOpenIndex
projection = new Projection().withProjectionType(ProjectionType.ALL);
isOpenIndex.setKeySchema(indexKeySchema);
isOpenIndex.setProjection(projection);
localSecondaryIndexes.add(isOpenIndex);
// Add index definitions to CreateTable request
createTableRequest.setLocalSecondaryIndexes(localSecondaryIndexes);
System.out.println("Creating table " + tableName + "...");
System.out.println(client.createTable(createTableRequest));
waitForTableToBecomeAvailable(tableName);
}
use of com.amazonaws.services.dynamodbv2.document.Index in project aws-doc-sdk-examples by awsdocs.
the class DocumentAPIGlobalSecondaryIndexExample method queryIndex.
public static void queryIndex(String indexName) {
Table table = dynamoDB.getTable(tableName);
System.out.println("\n***********************************************************\n");
System.out.print("Querying index " + indexName + "...");
Index index = table.getIndex(indexName);
ItemCollection<QueryOutcome> items = null;
QuerySpec querySpec = new QuerySpec();
if (indexName == "CreateDateIndex") {
System.out.println("Issues filed on 2013-11-01");
querySpec.withKeyConditionExpression("CreateDate = :v_date and begins_with(IssueId, :v_issue)").withValueMap(new ValueMap().withString(":v_date", "2013-11-01").withString(":v_issue", "A-"));
items = index.query(querySpec);
} else if (indexName == "TitleIndex") {
System.out.println("Compilation errors");
querySpec.withKeyConditionExpression("Title = :v_title and begins_with(IssueId, :v_issue)").withValueMap(new ValueMap().withString(":v_title", "Compilation error").withString(":v_issue", "A-"));
items = index.query(querySpec);
} else if (indexName == "DueDateIndex") {
System.out.println("Items that are due on 2013-11-30");
querySpec.withKeyConditionExpression("DueDate = :v_date").withValueMap(new ValueMap().withString(":v_date", "2013-11-30"));
items = index.query(querySpec);
} else {
System.out.println("\nNo valid index name provided");
return;
}
Iterator<Item> iterator = items.iterator();
System.out.println("Query: printing results...");
while (iterator.hasNext()) {
System.out.println(iterator.next().toJSONPretty());
}
}
use of com.amazonaws.services.dynamodbv2.document.Index in project aws-doc-sdk-examples by awsdocs.
the class DocumentAPILocalSecondaryIndexExample method query.
public static void query(String indexName) {
Table table = dynamoDB.getTable(tableName);
System.out.println("\n***********************************************************\n");
System.out.println("Querying table " + tableName + "...");
QuerySpec querySpec = new QuerySpec().withConsistentRead(true).withScanIndexForward(true).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
if (indexName == "IsOpenIndex") {
System.out.println("\nUsing index: '" + indexName + "': Bob's orders that are open.");
System.out.println("Only a user-specified list of attributes are returned\n");
Index index = table.getIndex(indexName);
querySpec.withKeyConditionExpression("CustomerId = :v_custid and IsOpen = :v_isopen").withValueMap(new ValueMap().withString(":v_custid", "bob@example.com").withNumber(":v_isopen", 1));
querySpec.withProjectionExpression("OrderCreationDate, ProductCategory, ProductName, OrderStatus");
ItemCollection<QueryOutcome> items = index.query(querySpec);
Iterator<Item> iterator = items.iterator();
System.out.println("Query: printing results...");
while (iterator.hasNext()) {
System.out.println(iterator.next().toJSONPretty());
}
} else if (indexName == "OrderCreationDateIndex") {
System.out.println("\nUsing index: '" + indexName + "': Bob's orders that were placed after 01/31/2015.");
System.out.println("Only the projected attributes are returned\n");
Index index = table.getIndex(indexName);
querySpec.withKeyConditionExpression("CustomerId = :v_custid and OrderCreationDate >= :v_orddate").withValueMap(new ValueMap().withString(":v_custid", "bob@example.com").withNumber(":v_orddate", 20150131));
querySpec.withSelect(Select.ALL_PROJECTED_ATTRIBUTES);
ItemCollection<QueryOutcome> items = index.query(querySpec);
Iterator<Item> iterator = items.iterator();
System.out.println("Query: printing results...");
while (iterator.hasNext()) {
System.out.println(iterator.next().toJSONPretty());
}
} else {
System.out.println("\nNo index: All of Bob's orders, by OrderId:\n");
querySpec.withKeyConditionExpression("CustomerId = :v_custid").withValueMap(new ValueMap().withString(":v_custid", "bob@example.com"));
ItemCollection<QueryOutcome> items = table.query(querySpec);
Iterator<Item> iterator = items.iterator();
System.out.println("Query: printing results...");
while (iterator.hasNext()) {
System.out.println(iterator.next().toJSONPretty());
}
}
}
use of com.amazonaws.services.dynamodbv2.document.Index in project athenz by yahoo.
the class DynamoDBCertRecordStoreFactory method create.
@Override
public CertRecordStore create(PrivateKeyStore keyStore) {
final String tableName = System.getProperty(ZTSConsts.ZTS_PROP_CERT_DYNAMODB_TABLE_NAME);
if (tableName == null || tableName.isEmpty()) {
LOGGER.error("Cert Store DynamoDB table name not specified");
throw new ResourceException(ResourceException.SERVICE_UNAVAILABLE, "DynamoDB table name not specified");
}
final String currentTimeIndexName = System.getProperty(ZTSConsts.ZTS_PROP_CERT_DYNAMODB_INDEX_CURRENT_TIME_NAME);
if (currentTimeIndexName == null || currentTimeIndexName.isEmpty()) {
LOGGER.error("Cert Store DynamoDB index current-time not specified");
throw new ResourceException(ResourceException.SERVICE_UNAVAILABLE, "DynamoDB index current-time not specified");
}
final String hostNameIndex = System.getProperty(ZTSConsts.ZTS_PROP_CERT_DYNAMODB_INDEX_HOST_NAME);
if (hostNameIndex == null || hostNameIndex.isEmpty()) {
LOGGER.error("Cert Store DynamoDB index host-name not specified");
throw new ResourceException(ResourceException.SERVICE_UNAVAILABLE, "DynamoDB index host-name not specified");
}
ZTSClientNotificationSenderImpl ztsClientNotificationSender = new ZTSClientNotificationSenderImpl();
AmazonDynamoDB client = getDynamoDBClient(ztsClientNotificationSender, keyStore);
return new DynamoDBCertRecordStore(client, tableName, currentTimeIndexName, hostNameIndex, ztsClientNotificationSender);
}
use of com.amazonaws.services.dynamodbv2.document.Index in project athenz by yahoo.
the class DynamoDBWorkloadRecordStoreFactory method create.
@Override
public WorkloadRecordStore create(PrivateKeyStore keyStore) {
final String tableName = System.getProperty(ZTSConsts.ZTS_PROP_WORKLOAD_DYNAMODB_TABLE_NAME);
if (StringUtil.isEmpty(tableName)) {
LOGGER.error("Workload Store DynamoDB table name not specified");
throw new ResourceException(ResourceException.SERVICE_UNAVAILABLE, "DynamoDB table name not specified");
}
final String serviceIndexName = System.getProperty(ZTSConsts.ZTS_PROP_WORKLOAD_DYNAMODB_INDEX_SERVICE_NAME);
if (StringUtil.isEmpty(serviceIndexName)) {
LOGGER.error("Workload Store DynamoDB index by name service not specified");
throw new ResourceException(ResourceException.SERVICE_UNAVAILABLE, "DynamoDB index workload-service-index not specified");
}
final String ipIndexName = System.getProperty(ZTSConsts.ZTS_PROP_WORKLOAD_DYNAMODB_INDEX_IP_NAME);
if (StringUtil.isEmpty(ipIndexName)) {
LOGGER.error("Workload Store DynamoDB index by name ip not specified");
throw new ResourceException(ResourceException.SERVICE_UNAVAILABLE, "DynamoDB index workload-ip-index not specified");
}
AmazonDynamoDB client = getDynamoDBClient(null, keyStore);
return new DynamoDBWorkloadRecordStore(client, tableName, serviceIndexName, ipIndexName);
}
Aggregations