Java 类com.amazonaws.services.dynamodbv2.model.Projection 实例源码

项目:lambdora    文件:IntegrationTestBase.java   
private CreateTableResult createTable() {
    final List<AttributeDefinition> attributeDefinitions = new ArrayList<>();
    attributeDefinitions.add(new AttributeDefinition(RESOURCE_NAME_ATT, ScalarAttributeType.S));
    attributeDefinitions.add(new AttributeDefinition(RDF_TRIPLE_ATT, ScalarAttributeType.S));
    attributeDefinitions.add(new AttributeDefinition(RDF_PREDICATE_ATT, ScalarAttributeType.S));
    attributeDefinitions.add(new AttributeDefinition(RDF_OBJECT_ATT, ScalarAttributeType.S));

    final List<KeySchemaElement> keySchema = new ArrayList<>();
    keySchema.add(new KeySchemaElement(RESOURCE_NAME_ATT, KeyType.HASH));
    keySchema.add(new KeySchemaElement(RDF_TRIPLE_ATT, KeyType.RANGE));

    final ProvisionedThroughput provisionedthroughput =
        new ProvisionedThroughput(10L, 10L);

    final LocalSecondaryIndex predicateIndex = new LocalSecondaryIndex()
        .withIndexName(PREDICATE_INDEX_NAME)
        .withKeySchema(new KeySchemaElement(RESOURCE_NAME_ATT, KeyType.HASH))
        .withKeySchema(new KeySchemaElement(RDF_PREDICATE_ATT, KeyType.RANGE))
        .withProjection(new Projection().withNonKeyAttributes(RDF_SUBJECT_ATT, RDF_OBJECT_ATT)
                                        .withProjectionType(ProjectionType.INCLUDE));

    final GlobalSecondaryIndex objectIndex = new GlobalSecondaryIndex()
        .withIndexName(OBJECT_INDEX_NAME)
        .withKeySchema(new KeySchemaElement(RDF_OBJECT_ATT, KeyType.HASH))
        .withKeySchema(new KeySchemaElement(RDF_PREDICATE_ATT, KeyType.RANGE))
        .withProjection(new Projection().withNonKeyAttributes(RDF_SUBJECT_ATT)
                                        .withProjectionType(ProjectionType.INCLUDE))
        .withProvisionedThroughput(new ProvisionedThroughput(10L, 10L));

    final CreateTableRequest request =
        new CreateTableRequest()
            .withTableName(TABLE_NAME)
            .withAttributeDefinitions(attributeDefinitions)
            .withKeySchema(keySchema)
            .withProvisionedThroughput(provisionedthroughput)
            .withLocalSecondaryIndexes(predicateIndex)
            .withGlobalSecondaryIndexes(objectIndex);

    return dynamodbClient.createTable(request);
}
项目:java-persistence    文件:DdbSchema.java   
@Override
protected void createIndex(String tableName, IndexDescription index) {
    // We ALWAYS create these as GSI's, since you can't add a LSI:
    CreateGlobalSecondaryIndexAction createAction = new CreateGlobalSecondaryIndexAction()
        .withIndexName(index.getIndexName())
        .withKeySchema(toKeySchema(tableName, index))
        .withProvisionedThroughput(
            new ProvisionedThroughput()
            .withReadCapacityUnits(index.getReadCapacity())
            .withWriteCapacityUnits(index.getWriteCapacity()))
        .withProjection(new Projection().withProjectionType(ProjectionType.ALL));

    AttributeDefinition hashKey = new AttributeDefinition()
        .withAttributeName(index.getHashKey().getAttrName())
        .withAttributeType(toScalarAttributeType(index.getHashKey().getAttrType()));

    if ( null == index.getRangeKey() ) {
        _dynamodb.getTable(getTableName(tableName))
            .createGSI(createAction, hashKey);
    } else {
        AttributeDefinition rangeKey = new AttributeDefinition()
            .withAttributeName(index.getRangeKey().getAttrName())
            .withAttributeType(toScalarAttributeType(index.getRangeKey().getAttrType()));

        _dynamodb.getTable(getTableName(tableName))
            .createGSI(createAction, hashKey, rangeKey);
    }
}
项目:para    文件:AWSDynamoUtils.java   
/**
 * Creates a table in AWS DynamoDB which will be shared between apps.
 * @param readCapacity read capacity
 * @param writeCapacity write capacity
 * @return true if created
 */
public static boolean createSharedTable(long readCapacity, long writeCapacity) {
    if (StringUtils.isBlank(SHARED_TABLE) || StringUtils.containsWhitespace(SHARED_TABLE) ||
            existsTable(SHARED_TABLE)) {
        return false;
    }
    try {
        GlobalSecondaryIndex secIndex = new GlobalSecondaryIndex().
                withIndexName(getSharedIndexName()).
                withProvisionedThroughput(new ProvisionedThroughput().
                        withReadCapacityUnits(1L).
                        withWriteCapacityUnits(1L)).
                withProjection(new Projection().withProjectionType(ProjectionType.ALL)).
                withKeySchema(new KeySchemaElement().withAttributeName(Config._APPID).withKeyType(KeyType.HASH),
                        new KeySchemaElement().withAttributeName(Config._ID).withKeyType(KeyType.RANGE));

        getClient().createTable(new CreateTableRequest().withTableName(getTableNameForAppid(SHARED_TABLE)).
                withKeySchema(new KeySchemaElement(Config._KEY, KeyType.HASH)).
                withAttributeDefinitions(new AttributeDefinition(Config._KEY, ScalarAttributeType.S),
                        new AttributeDefinition(Config._APPID, ScalarAttributeType.S),
                        new AttributeDefinition(Config._ID, ScalarAttributeType.S)).
                withGlobalSecondaryIndexes(secIndex).
                withProvisionedThroughput(new ProvisionedThroughput(readCapacity, writeCapacity)));
    } catch (Exception e) {
        logger.error(null, e);
        return false;
    }
    return true;
}
项目:micro-genie    文件:DynamoAdmin.java   
/***
 * Create the dynamoDb Projection
 * @param projection
 * @return projection
 */
private Projection createProjection(final io.microgenie.aws.config.DynamoDbConfig.Projection projection) {

    final Projection p = new Projection();
    p.withNonKeyAttributes(projection.getNonKeyAttributes());
    p.withProjectionType(projection.getType());
    return p;
}
项目:dynamodb-geo    文件:GeoTableUtil.java   
/**
 * <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;
}
项目:outland    文件:DynamoCreateFeatureTableTask.java   
public void createTable() {

    final AttributeDefinition nsKey =
        new AttributeDefinition().withAttributeName(HASH_KEY).withAttributeType(
            ScalarAttributeType.S);

    final AttributeDefinition featureKey =
        new AttributeDefinition().withAttributeName(RANGE_KEY)
            .withAttributeType(ScalarAttributeType.S);

    final AttributeDefinition id =
        new AttributeDefinition().withAttributeName(ATTR_ID)
            .withAttributeType(ScalarAttributeType.S);

    final ArrayList<AttributeDefinition>
        tableAttributeDefinitions = Lists.newArrayList(nsKey, featureKey, id);
    final ArrayList<KeySchemaElement> tableKeySchema = Lists.newArrayList();

    tableKeySchema.add(
        new KeySchemaElement().withAttributeName(HASH_KEY).withKeyType(KeyType.HASH));
    tableKeySchema.add(
        new KeySchemaElement().withAttributeName(RANGE_KEY).withKeyType(KeyType.RANGE));

    final ProvisionedThroughput tableProvisionedThroughput =
        new ProvisionedThroughput()
            .withReadCapacityUnits(10L)
            .withWriteCapacityUnits(10L);

    final ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<>();
    indexKeySchema.add(
        new KeySchemaElement().withAttributeName(HASH_KEY).withKeyType(KeyType.HASH));
    indexKeySchema.add(
        new KeySchemaElement().withAttributeName(ATTR_ID).withKeyType(KeyType.RANGE));

    final Projection projection = new Projection().withProjectionType(ProjectionType.INCLUDE);
    final ArrayList<String> indexColumns = new ArrayList<>();
    indexColumns.add("json");
    indexColumns.add("v");
    projection.setNonKeyAttributes(indexColumns);

    final LocalSecondaryIndex localSecondaryIndex = new LocalSecondaryIndex()
        .withIndexName("feature_by_ns_key_and_id_lsi_idx")
        .withKeySchema(indexKeySchema)
        .withProjection(projection);

    final ArrayList<LocalSecondaryIndex> secondaryIndices = new ArrayList<>();
    secondaryIndices.add(localSecondaryIndex);

    final CreateTableRequest createTableRequest =
        new CreateTableRequest()
            .withTableName(tableName)
            .withKeySchema(tableKeySchema)
            .withAttributeDefinitions(tableAttributeDefinitions)
            .withProvisionedThroughput(tableProvisionedThroughput)
            .withLocalSecondaryIndexes(secondaryIndices);

    final TableDescription tableDescription =
        amazonDynamoDB.createTable(createTableRequest).getTableDescription();

    logger.info("created_table {}", tableDescription);

    final DescribeTableRequest describeTableRequest =
        new DescribeTableRequest().withTableName(tableName);

    final TableDescription description =
        amazonDynamoDB.describeTable(describeTableRequest).getTable();

    logger.info("table_description: " + description);
  }
项目:AwsCommons    文件:AbstractDynamoTable.java   
private void createTable() {
    try {
        final ArrayList<KeySchemaElement> keySchema = new ArrayList<>();
        keySchema.add(new KeySchemaElement()
                .withAttributeName("id")
                .withKeyType(KeyType.HASH));

        final ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<>();
        attributeDefinitions.add(new AttributeDefinition()
                .withAttributeName("id")
                .withAttributeType("S"));

        final ArrayList<GlobalSecondaryIndex> globalSecondaryIndices = new ArrayList<>();

        if (secondaryIndexNames != null) {
            for (final String indexName : secondaryIndexNames) {
                attributeDefinitions.add(new AttributeDefinition()
                        .withAttributeName(indexName)
                        .withAttributeType("S"));

                globalSecondaryIndices.add(new GlobalSecondaryIndex()
                        .withIndexName(indexName + "-index")
                        .withKeySchema(new KeySchemaElement().withAttributeName(indexName).withKeyType(KeyType.HASH))
                        .withProjection(new Projection().withProjectionType(ProjectionType.ALL))
                        .withProvisionedThroughput(new ProvisionedThroughput()
                                .withReadCapacityUnits(readCapacityUnits)
                                .withWriteCapacityUnits(writeCapacityUnits)));
            }
        }

        final CreateTableRequest request = new CreateTableRequest()
                .withTableName(tableName)
                .withKeySchema(keySchema)
                .withProvisionedThroughput(new ProvisionedThroughput()
                        .withReadCapacityUnits(readCapacityUnits)
                        .withWriteCapacityUnits(writeCapacityUnits));

        if (!globalSecondaryIndices.isEmpty()) {
            request.withGlobalSecondaryIndexes(globalSecondaryIndices);
        }

        request.setAttributeDefinitions(attributeDefinitions);

        logger.debug("Issuing CreateTable request for " + tableName);
        final Table table = DynamoCommons.getInstance().getDb().createTable(request);
        logger.debug("Waiting for table to be created...");
        table.waitForActive();

    } catch (final Exception e) {
        logger.error("CreateTable request failed: " + e.getMessage());
    }
}
项目:java-persistence    文件:DdbSchema.java   
@Override
protected void createTable(TableDescription table) {
    List<GlobalSecondaryIndex> gsis = new ArrayList<>();
    List<LocalSecondaryIndex> lsis = new ArrayList<>();
    ProvisionedThroughput mainThroughtput = null;
    List<KeySchemaElement> mainKeySchema = null;

    Map<String, AttrType> attrTypes = new HashMap<>();

    for ( IndexDescription index : table.getIndexes() ) {
        addAttrType(table.getTableName(), index.getIndexName(), attrTypes, index.getHashKey());
        addAttrType(table.getTableName(), index.getIndexName(), attrTypes, index.getRangeKey());
        ProvisionedThroughput throughput = new ProvisionedThroughput()
            .withReadCapacityUnits(index.getReadCapacity())
            .withWriteCapacityUnits(index.getWriteCapacity());
        switch ( index.getIndexType() ) {
        case MAIN_INDEX:
            mainThroughtput = throughput;
            mainKeySchema = toKeySchema(table.getTableName(), index);
            break;
        case LOCAL_SECONDARY_INDEX:
            lsis.add(
                new LocalSecondaryIndex()
                .withProjection(
                    new Projection().withProjectionType(ProjectionType.ALL))
                .withIndexName(index.getIndexName())
                .withKeySchema(toKeySchema(table.getTableName(), index)));
            break;
        case GLOBAL_SECONDARY_INDEX:
            gsis.add(
                new GlobalSecondaryIndex()
                .withIndexName(index.getIndexName())
                .withKeySchema(toKeySchema(table.getTableName(), index))
                .withProjection(
                    new Projection().withProjectionType(ProjectionType.ALL))
                .withProvisionedThroughput(throughput));
            break;
        default:
            throw new UnsupportedOperationException(
                "Unsupported indexType="+index.getIndexType()+" for table name "+
                table.getTableName()+" index="+index.getIndexName());
        }
    }

    String tableName = getTableName(table.getTableName());
    for ( int retry=0;; retry++ ) {
        try {
            _dynamodb.createTable(
                new CreateTableRequest()
                .withKeySchema(mainKeySchema)
                .withProvisionedThroughput(mainThroughtput)
                .withAttributeDefinitions(toAttributeDefinitions(attrTypes))
                .withLocalSecondaryIndexes(lsis.size() == 0 ? null : lsis)
                .withGlobalSecondaryIndexes(gsis.size() == 0 ? null : gsis)
                .withTableName(tableName));
            break;
        } catch ( LimitExceededException ex ) {
            long secs = (retry < 6) ? (long)Math.pow(2, retry) : 60L;
            LOG.info("Waiting {} seconds to create {} due to: {}", secs, tableName, ex.getMessage());
            try {
                Thread.sleep(1000*secs);
            } catch ( InterruptedException interruptedEx ) {
                Thread.currentThread().interrupt();
                return;
            }
        }
    }
}
项目:amazon-kinesis-aggregators    文件:DynamoDataStore.java   
public void initAggTable(final String keyColumn, final String dateColumnName,
        final long readCapacity, final long writeCapacity) throws Exception {
    final String setDateColumn = dateColumnName == null ? StreamAggregator.DEFAULT_DATE_VALUE
            : dateColumnName;

    long setReadCapacity = readCapacity == -1 ? DEFAULT_READ_CAPACITY : readCapacity;
    long setWriteCapacity = writeCapacity == -1 ? DEFAULT_WRITE_CAPACITY : writeCapacity;

    // we have to add this attribute list so that we can project the key
    // into the GSI
    List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>() {
        {
            add(new AttributeDefinition().withAttributeName(keyColumn).withAttributeType("S"));
            add(new AttributeDefinition().withAttributeName(setDateColumn).withAttributeType(
                    "S"));
        }
    };

    Collection<GlobalSecondaryIndex> gsi = new ArrayList<>();

    // Global Secondary Index for accessing the table by date item
    gsi.add(new GlobalSecondaryIndex().withIndexName(
            StreamAggregatorUtils.getDateDimensionIndexName(tableName, setDateColumn)).withKeySchema(
            new KeySchemaElement().withAttributeName(SCATTER_PREFIX_ATTRIBUTE).withKeyType(
                    KeyType.HASH),
            new KeySchemaElement().withAttributeName(setDateColumn).withKeyType(KeyType.RANGE)).withProjection(
            new Projection().withProjectionType(ProjectionType.KEYS_ONLY)).withProvisionedThroughput(
            new ProvisionedThroughput().withReadCapacityUnits(setReadCapacity).withWriteCapacityUnits(
                    setWriteCapacity)));

    attributes.add(new AttributeDefinition().withAttributeName(SCATTER_PREFIX_ATTRIBUTE).withAttributeType(
            "N"));

    // table is hash/range on value and date
    List<KeySchemaElement> key = new ArrayList<KeySchemaElement>() {
        {
            add(new KeySchemaElement().withAttributeName(keyColumn).withKeyType(KeyType.HASH));
            add(new KeySchemaElement().withAttributeName(setDateColumn).withKeyType(
                    KeyType.RANGE));
        }
    };

    // initialise the table
    DynamoUtils.initTable(this.dynamoClient, this.tableName, setReadCapacity, setWriteCapacity,
            attributes, key, gsi);
}
项目:spring-security-oauth2-dynamodb    文件:DynamoDBInitializationHelper.java   
public static void createTokenTables(AmazonDynamoDBClient client, DynamoDBTokenSchema schema) {
    GlobalSecondaryIndex gsiAuthenticationIdToken = new GlobalSecondaryIndex() //
            .withIndexName(schema.getAccessIndexAuthenticationId()) //
            .withKeySchema(new KeySchemaElement(schema.getAccessColumnAuthenticationId(), KeyType.HASH)) //
            .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT) //
            .withProjection(new Projection().withProjectionType(ProjectionType.KEYS_ONLY));

    GlobalSecondaryIndex gsiRefreshToken = new GlobalSecondaryIndex() //
            .withIndexName(schema.getAccessIndexRefreshToken()) //
            .withKeySchema(new KeySchemaElement(schema.getAccessColumnRefreshToken(), KeyType.HASH)) //
            .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT) //
            .withProjection(new Projection().withProjectionType(ProjectionType.KEYS_ONLY));

    GlobalSecondaryIndex gsiClientIdAndUserName = new GlobalSecondaryIndex() //
            .withIndexName(schema.getAccessIndexClientIdAndUserName()) //
            .withKeySchema( //
                    new KeySchemaElement(schema.getAccessColumnClientId(), KeyType.HASH), //
                    new KeySchemaElement(schema.getAccessColumnUserName(), KeyType.RANGE) //
            ) //
            .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT) //
            .withProjection(new Projection().withProjectionType(ProjectionType.KEYS_ONLY));

    CreateTableRequest accessTableRequest = new CreateTableRequest() //
            .withTableName(schema.getAccessTableName()) //
            .withKeySchema(new KeySchemaElement(schema.getAccessColumnTokenId(), KeyType.HASH)) //
            .withGlobalSecondaryIndexes(gsiAuthenticationIdToken, gsiRefreshToken, gsiClientIdAndUserName) //
            .withAttributeDefinitions(new AttributeDefinition(schema.getAccessColumnTokenId(), ScalarAttributeType.S), //
                    new AttributeDefinition(schema.getAccessColumnAuthenticationId(), ScalarAttributeType.S), //
                    new AttributeDefinition(schema.getAccessColumnRefreshToken(), ScalarAttributeType.S), //
                    new AttributeDefinition(schema.getAccessColumnClientId(), ScalarAttributeType.S), //
                    new AttributeDefinition(schema.getAccessColumnUserName(), ScalarAttributeType.S) //
            ) //
            .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT) //
    ;

    CreateTableResult accessTableresponse = client.createTable(accessTableRequest);

    CreateTableRequest refreshTableRequest = new CreateTableRequest() //
            .withTableName(schema.getRefreshTableName()) //
            .withKeySchema(new KeySchemaElement(schema.getRefreshColumnTokenId(), KeyType.HASH)) //
            .withAttributeDefinitions(new AttributeDefinition(schema.getRefreshColumnTokenId(), ScalarAttributeType.S) //
            ) //
            .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT) //
    ;

    CreateTableResult refreshTableresponse = client.createTable(refreshTableRequest);
}
项目:aws-dynamodb-examples    文件:LowLevelGlobalSecondaryIndexExample.java   
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);
}
项目:aws-dynamodb-examples    文件:CreateTablesLoadData.java   
private static void createTable(
    String tableName, long readCapacityUnits, long writeCapacityUnits, 
    String hashKeyName, String hashKeyType, 
    String rangeKeyName, String rangeKeyType) {

    try {

        ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
        keySchema.add(new KeySchemaElement()
            .withAttributeName(hashKeyName)
            .withKeyType(KeyType.HASH));

        ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
        attributeDefinitions.add(new AttributeDefinition()
            .withAttributeName(hashKeyName)
            .withAttributeType(hashKeyType));

        if (rangeKeyName != null) {
            keySchema.add(new KeySchemaElement()
                .withAttributeName(rangeKeyName)
                .withKeyType(KeyType.RANGE));
            attributeDefinitions.add(new AttributeDefinition()
                .withAttributeName(rangeKeyName)
                .withAttributeType(rangeKeyType));
        }

        CreateTableRequest request = new CreateTableRequest()
                .withTableName(tableName)
                .withKeySchema(keySchema)
                .withProvisionedThroughput( new ProvisionedThroughput()
                    .withReadCapacityUnits(readCapacityUnits)
                    .withWriteCapacityUnits(writeCapacityUnits));

        // If this is the Reply table, define a local secondary index
        if (replyTableName.equals(tableName)) {

            attributeDefinitions.add(new AttributeDefinition()
                .withAttributeName("PostedBy")
                .withAttributeType("S"));

            ArrayList<LocalSecondaryIndex> localSecondaryIndexes = new ArrayList<LocalSecondaryIndex>();
            localSecondaryIndexes.add(new LocalSecondaryIndex()
                .withIndexName("PostedBy-Index")
                .withKeySchema(
                    new KeySchemaElement().withAttributeName(hashKeyName).withKeyType(KeyType.HASH), 
                    new KeySchemaElement() .withAttributeName("PostedBy") .withKeyType(KeyType.RANGE))
                .withProjection(new Projection() .withProjectionType(ProjectionType.KEYS_ONLY)));

            request.setLocalSecondaryIndexes(localSecondaryIndexes);
        }

        request.setAttributeDefinitions(attributeDefinitions);

        System.out.println("Issuing CreateTable request for " + tableName);
        Table table = dynamoDB.createTable(request);
        System.out.println("Waiting for " + tableName
            + " to be created...this may take a while...");
        table.waitForActive();

    } catch (Exception e) {
        System.err.println("CreateTable request failed for " + tableName);
        System.err.println(e.getMessage());
    }
}
项目:tweetamo    文件:PersistentStore.java   
private void createTables(long readCapacity, long writeCapacity)
        throws Exception {
    // ID | createdAt | lat | long | screen name | text |
    // Primary index is by ID
    // Global Secondary index is by screen name + createdAt

    try {
        CreateTableRequest createTableRequest = new CreateTableRequest()
                .withTableName(TABLE_NAME)
                .withKeySchema(
                        new KeySchemaElement().withAttributeName(COL_ID)
                                .withKeyType(KeyType.HASH))
                .withAttributeDefinitions(
                        new AttributeDefinition().withAttributeName(COL_ID)
                                .withAttributeType(ScalarAttributeType.N),
                        new AttributeDefinition().withAttributeName(
                                COL_CREATEDAT).withAttributeType(
                                ScalarAttributeType.N),
                        new AttributeDefinition().withAttributeName(
                                COL_SCREENNAME).withAttributeType(
                                ScalarAttributeType.S))
                .withProvisionedThroughput(
                        new ProvisionedThroughput().withReadCapacityUnits(
                                readCapacity).withWriteCapacityUnits(
                                writeCapacity))
                .withGlobalSecondaryIndexes(
                        new GlobalSecondaryIndex()
                                .withIndexName(INDEX_SCREENNAME)
                                .withProvisionedThroughput(
                                        new ProvisionedThroughput()
                                                .withReadCapacityUnits(
                                                        (long) 10)
                                                .withWriteCapacityUnits(
                                                        (long) 1))
                                .withProjection(
                                        new Projection()
                                                .withProjectionType("ALL"))
                                .withKeySchema(
                                        new KeySchemaElement()
                                                .withAttributeName(
                                                        COL_SCREENNAME)
                                                .withKeyType(KeyType.HASH),
                                        new KeySchemaElement()
                                                .withAttributeName(
                                                        COL_CREATEDAT)
                                                .withKeyType(KeyType.RANGE)));

        TableDescription createdTableDescription = dynamoDB.createTable(
                createTableRequest).getTableDescription();
        LOG.info("Created Table: " + createdTableDescription);
    } catch (Exception e) {
        handleException(e);
    }
}