Example usage for com.amazonaws.services.dynamodbv2.model AttributeDefinition AttributeDefinition

List of usage examples for com.amazonaws.services.dynamodbv2.model AttributeDefinition AttributeDefinition

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.model AttributeDefinition AttributeDefinition.

Prototype

public AttributeDefinition(String attributeName, ScalarAttributeType attributeType) 

Source Link

Document

Constructs a new AttributeDefinition object.

Usage

From source file:io.ignitr.dispatchr.manager.core.data.ClientRepository.java

License:Apache License

/**
 * Creates the DynamoDB tables that are used by this repository.
 *
 * @throws Exception//from www .j  ava2s .  c om
 */
@PostConstruct
private void initialize() throws Exception {
    LOG.info("Initializing DynamoDB tables for TopicRepository...");

    CreateTableRequest clientRequest = new CreateTableRequest(
            Arrays.asList(new AttributeDefinition("clientId", ScalarAttributeType.S)), CLIENT_TABLE_NAME,
            Arrays.asList(new KeySchemaElement("clientId", KeyType.HASH)), new ProvisionedThroughput(4L, 1L));

    if (TableUtils.createTableIfNotExists(dynamoDBClient, clientRequest)) {
        LOG.info("Creating DynamoDB table '{}'...", CLIENT_TABLE_NAME);
    } else {
        LOG.debug("DynamoDB table '{}' already exists!  Skipping table creation.", CLIENT_TABLE_NAME);
    }

    LOG.info("Waiting for DynamoDB table '{}' to become active...", CLIENT_TABLE_NAME);
    TableUtils.waitUntilActive(dynamoDBClient, CLIENT_TABLE_NAME);
    LOG.info("DynamoDB table '{}' is active", CLIENT_TABLE_NAME);
}

From source file:io.ignitr.dispatchr.manager.core.data.SubscriptionRepository.java

License:Apache License

@PostConstruct
private void initialize() throws Exception {
    LOG.info("Initializing DynamoDB tables for SubscriptionRepository...");

    // Dispatchr_Subscription
    CreateTableRequest subscriptionRequest = new CreateTableRequest(
            Arrays.asList(new AttributeDefinition("id", ScalarAttributeType.S)), SUBSCRIPTION_TABLE_NAME,
            Arrays.asList(new KeySchemaElement("id", KeyType.HASH)), new ProvisionedThroughput(4L, 1L));

    // Dispatchr_TopicSubscriptions
    CreateTableRequest topicSubscriptionsRequest = new CreateTableRequest(
            Arrays.asList(new AttributeDefinition("topic", ScalarAttributeType.S),
                    new AttributeDefinition("subscriptionId", ScalarAttributeType.S)

            ), TOPIC_SUBSCRIPTIONS_TABLE_NAME, Arrays.asList(new KeySchemaElement("topic", KeyType.HASH),
                    new KeySchemaElement("subscriptionId", KeyType.RANGE)),
            new ProvisionedThroughput(4L, 1L));

    if (TableUtils.createTableIfNotExists(dynamoDBClient, subscriptionRequest)) {
        LOG.info("Creating DynamoDB table '{}'...", SUBSCRIPTION_TABLE_NAME);
    } else {//from  w  w  w.jav a 2s.  c o m
        LOG.debug("DynamoDB table '{}' already exists!  Skipping table creation.", SUBSCRIPTION_TABLE_NAME);
    }

    if (TableUtils.createTableIfNotExists(dynamoDBClient, topicSubscriptionsRequest)) {
        LOG.info("Creating DynamoDB table '{}'...", TOPIC_SUBSCRIPTIONS_TABLE_NAME);
    } else {
        LOG.debug("DynamoDB table '{}' already exists!  Skipping table creation.",
                TOPIC_SUBSCRIPTIONS_TABLE_NAME);
    }

    LOG.info("Waiting for DynamoDB table '{}' to become active...", SUBSCRIPTION_TABLE_NAME);
    TableUtils.waitUntilActive(dynamoDBClient, SUBSCRIPTION_TABLE_NAME);
    LOG.info("DynamoDB table '{}' is active", SUBSCRIPTION_TABLE_NAME);

    LOG.info("Waiting for DynamoDB table '{}' to become active...", TOPIC_SUBSCRIPTIONS_TABLE_NAME);
    TableUtils.waitUntilActive(dynamoDBClient, TOPIC_SUBSCRIPTIONS_TABLE_NAME);
    LOG.info("DynamoDB table '{}' is active", TOPIC_SUBSCRIPTIONS_TABLE_NAME);
}

From source file:io.ignitr.dispatchr.manager.core.data.TopicRepository.java

License:Apache License

@PostConstruct
private void initialize() throws Exception {
    LOG.info("Initializing DynamoDB tables for TopicRepository...");

    CreateTableRequest request = new CreateTableRequest(
            Arrays.asList(new AttributeDefinition("name", ScalarAttributeType.S)), TOPIC_TABLE_NAME,
            Arrays.asList(new KeySchemaElement("name", KeyType.HASH)), new ProvisionedThroughput(4L, 1L));

    if (TableUtils.createTableIfNotExists(dynamoDBClient, request)) {
        LOG.info("Creating DynamoDB table '{}'...", TOPIC_TABLE_NAME);
    } else {/*from   ww w . j  ava2  s  .c  o m*/
        LOG.debug("DynamoDB table '{}' already exists!  Skipping table creation.", TOPIC_TABLE_NAME);
    }

    LOG.info("Waiting for DynamoDB table '{}' to become active...", TOPIC_TABLE_NAME);
    TableUtils.waitUntilActive(dynamoDBClient, TOPIC_TABLE_NAME);
    LOG.info("DynamoDB table '{}' is active", TOPIC_TABLE_NAME);
}

From source file:jp.classmethod.aws.dynamodb.DynamoDbRepository.java

License:Open Source License

private CreateTableRequest createTableRequest() {
    return new CreateTableRequest().withTableName(table.getTableName())
            .withProvisionedThroughput(ptMap.get(tableNameSuffix)).withKeySchema(schemata)
            .withAttributeDefinitions(definitions.keySet().stream()
                    .map(name -> new AttributeDefinition(name, definitions.get(name)))
                    .collect(Collectors.toList()))
            .withGlobalSecondaryIndexes(gsis == null ? null : gsis.values());
}

From source file:org.apache.beam.sdk.io.aws.dynamodb.DynamoDBIOTestHelper.java

License:Apache License

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);
}

From source file:org.apache.metamodel.dynamodb.DynamoDbTableCreationBuilder.java

License:Apache License

@Override
public Table execute() throws MetaModelException {
    final MutableTable table = getTable();
    final String tableName = table.getName();

    final Collection<AttributeDefinition> attributes = new ArrayList<>();
    final Collection<KeySchemaElement> keySchema = new ArrayList<>();
    final Collection<GlobalSecondaryIndex> globalSecondaryIndices = new ArrayList<>();

    final long readCapacity = Long
            .parseLong(System.getProperty(DynamoDbDataContext.SYSTEM_PROPERTY_THROUGHPUT_READ_CAPACITY, "5"));
    final long writeCapacity = Long
            .parseLong(System.getProperty(DynamoDbDataContext.SYSTEM_PROPERTY_THROUGHPUT_WRITE_CAPACITY, "5"));
    final ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput(readCapacity, writeCapacity);

    for (Column column : table.getColumns()) {
        if (column.isPrimaryKey()) {
            final KeyType keyType = getKeyType(column.getRemarks());
            keySchema.add(new KeySchemaElement(column.getName(), keyType));
            attributes.add(/*from ww  w  . ja v a  2  s  .c om*/
                    new AttributeDefinition(column.getName(), DynamoDbUtils.toAttributeType(column.getType())));
        }
    }

    final CreateTableRequest createTableRequest = new CreateTableRequest();
    createTableRequest.setTableName(tableName);
    createTableRequest.setAttributeDefinitions(attributes);
    createTableRequest.setGlobalSecondaryIndexes(globalSecondaryIndices);
    createTableRequest.setKeySchema(keySchema);
    createTableRequest.setProvisionedThroughput(provisionedThroughput);

    final AmazonDynamoDB client = getUpdateCallback().getDataContext().getDynamoDb();

    final CreateTableResult createTableResult = client.createTable(createTableRequest);

    // await the table creation to be "done".
    {
        String tableStatus = createTableResult.getTableDescription().getTableStatus();
        while (TableStatus.CREATING.name().equals(tableStatus)) {
            logger.debug("Waiting for table status to be ACTIVE. Currently: {}", tableStatus);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                getUpdateCallback().setInterrupted(true);
            }
            tableStatus = client.describeTable(tableName).getTable().getTableStatus();
        }
    }

    return table;
}

From source file:org.iternine.jeppetto.dao.dynamodb.extra.TableBuilder.java

License:Apache License

public TableBuilder withKey(String hashKeyName, ScalarAttributeType hashKeyType, String rangeKeyName,
        ScalarAttributeType rangeKeyType) {
    keySchema.add(new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName(hashKeyName));
    attributeDefinitions.add(new AttributeDefinition(hashKeyName, hashKeyType));

    if (rangeKeyName != null) {
        keySchema.add(new KeySchemaElement().withKeyType(KeyType.RANGE).withAttributeName(rangeKeyName));
        attributeDefinitions.add(new AttributeDefinition(rangeKeyName, rangeKeyType));
    }//w  ww .ja  v  a 2 s.com

    return this;
}

From source file:org.iternine.jeppetto.dao.dynamodb.extra.TableBuilder.java

License:Apache License

public TableBuilder withLsi(String indexKey, ScalarAttributeType indexKeyType) {
    if (localSecondaryIndexes == null) {
        localSecondaryIndexes = new ArrayList<LocalSecondaryIndex>();
    }/*  w  w  w  .java2 s  . c o  m*/

    attributeDefinitions.add(new AttributeDefinition(indexKey, indexKeyType));

    localSecondaryIndexes.add(new LocalSecondaryIndex().withIndexName(indexKey + "-index")
            .withKeySchema(new KeySchemaElement(keySchema.get(0).getAttributeName(), KeyType.HASH),
                    new KeySchemaElement(indexKey, KeyType.RANGE))
            .withProjection(new Projection().withProjectionType(ProjectionType.ALL)));

    return this;
}

From source file:org.iternine.jeppetto.dao.dynamodb.extra.TableBuilder.java

License:Apache License

public TableBuilder withGsi(String gsiHashKeyName, ScalarAttributeType gsiHashKeyType, String gsiRangeKeyName,
        ScalarAttributeType gsiRangeKeyType) {
    if (globalSecondaryIndexes == null) {
        globalSecondaryIndexes = new ArrayList<GlobalSecondaryIndex>();
    }/*  ww w. j a  v a  2s  .  com*/

    ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
    String indexName;

    attributeDefinitions.add(new AttributeDefinition(gsiHashKeyName, gsiHashKeyType));
    keySchema.add(new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName(gsiHashKeyName));

    if (gsiRangeKeyName != null) {
        attributeDefinitions.add(new AttributeDefinition(gsiRangeKeyName, gsiRangeKeyType));
        keySchema.add(new KeySchemaElement().withKeyType(KeyType.RANGE).withAttributeName(gsiRangeKeyName));

        indexName = gsiHashKeyName + "-" + gsiRangeKeyName;
    } else {
        indexName = gsiHashKeyName;
    }

    globalSecondaryIndexes.add(new GlobalSecondaryIndex().withIndexName(indexName)
            .withProvisionedThroughput(new ProvisionedThroughput(64L, 64L)).withKeySchema(keySchema)
            .withProjection(new Projection().withProjectionType(ProjectionType.ALL)));

    return this;
}

From source file:org.springframework.integration.aws.metadata.DynamoDbMetaDataStore.java

License:Apache License

@Override
public void afterPropertiesSet() throws Exception {
    try {/*from ww  w.  j av  a  2 s  .  c  om*/
        this.table.describe();
        createTableLatch.countDown();
        return;
    } catch (ResourceNotFoundException e) {
        if (logger.isInfoEnabled()) {
            logger.info("No table '" + this.table.getTableName() + "'. Creating one...");
        }
    }

    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(this.table.getTableName())
            .withKeySchema(new KeySchemaElement(KEY, KeyType.HASH))
            .withAttributeDefinitions(new AttributeDefinition(KEY, ScalarAttributeType.S))
            .withProvisionedThroughput(new ProvisionedThroughput(this.readCapacity, this.writeCapacity));

    this.dynamoDB.createTableAsync(createTableRequest,
            new AsyncHandler<CreateTableRequest, CreateTableResult>() {

                @Override
                public void onError(Exception e) {
                    logger.error(
                            "Cannot create DynamoDb table: " + DynamoDbMetaDataStore.this.table.getTableName(),
                            e);
                    DynamoDbMetaDataStore.this.createTableLatch.countDown();
                }

                @Override
                public void onSuccess(CreateTableRequest request, CreateTableResult createTableResult) {
                    Waiter<DescribeTableRequest> waiter = DynamoDbMetaDataStore.this.dynamoDB.waiters()
                            .tableExists();

                    WaiterParameters<DescribeTableRequest> waiterParameters = new WaiterParameters<>(
                            new DescribeTableRequest(DynamoDbMetaDataStore.this.table.getTableName()))
                                    .withPollingStrategy(new PollingStrategy(new MaxAttemptsRetryStrategy(25),
                                            new FixedDelayStrategy(1)));

                    waiter.runAsync(waiterParameters, new WaiterHandler<DescribeTableRequest>() {

                        @Override
                        public void onWaitSuccess(DescribeTableRequest request) {
                            DynamoDbMetaDataStore.this.createTableLatch.countDown();
                            DynamoDbMetaDataStore.this.table.describe();
                        }

                        @Override
                        public void onWaitFailure(Exception e) {
                            logger.error("Cannot describe DynamoDb table: "
                                    + DynamoDbMetaDataStore.this.table.getTableName(), e);
                            DynamoDbMetaDataStore.this.createTableLatch.countDown();
                        }

                    });
                }

            });
}