Example usage for com.amazonaws.services.dynamodbv2.document Table Table

List of usage examples for com.amazonaws.services.dynamodbv2.document Table Table

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.document Table Table.

Prototype

public Table(AmazonDynamoDB client, String tableName) 

Source Link

Usage

From source file:com.github.fge.jsonpatch.JsonPatchToXSpecRemove.java

License:LGPL

@BeforeTest
public void setUp() throws Exception {
    AmazonDynamoDB amazonDynamoDB = DynamoDBEmbedded.create().amazonDynamoDB();
    try {//from  w w w  .j  a  va2 s  .  co  m
        amazonDynamoDB.deleteTable(TABLE_NAME);
    } catch (ResourceNotFoundException e) {
        //do nothing because the first run will not have the table.
    }
    amazonDynamoDB.createTable(new CreateTableRequest().withTableName(TABLE_NAME)
            .withProvisionedThroughput(new ProvisionedThroughput(1L, 1L))
            .withAttributeDefinitions(new AttributeDefinition().withAttributeName(KEY_ATTRIBUTE_NAME)
                    .withAttributeType(ScalarAttributeType.S))
            .withKeySchema(
                    new KeySchemaElement().withAttributeName(KEY_ATTRIBUTE_NAME).withKeyType(KeyType.HASH)));
    table = new Table(amazonDynamoDB, TABLE_NAME);
}

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

License:Open Source License

/**
 * Create instance.//  w  ww.j a v  a2  s. co m
 *
 * @param prefix the table prefix
 * @param tableNameSuffix the suffix for the table name
 * @param amazonDynamoDB dynamodb client
 * @param provisionedThroughputMap map of provisioned througput
 * @param objectMapper mapper to use for back/forth to json
 * @param clazz class reference of E
 * @param attributeDefinitions types of keys on base table and GSI
 * @param baseTableKeyNames names of base table keys
 * @param gsiList list of GSI definitions
 * @param versionString
 * @since #version#
 */
protected DynamoDbRepository(String prefix, String tableNameSuffix, AmazonDynamoDB amazonDynamoDB,
        Map<String, ProvisionedThroughput> provisionedThroughputMap, ObjectMapper objectMapper, Class<E> clazz,
        Map<String, ScalarAttributeType> attributeDefinitions, List<String> baseTableKeyNames,
        Map<String, GlobalSecondaryIndex> gsiList, String versionString) {
    Preconditions.checkNotNull(amazonDynamoDB);
    Preconditions.checkArgument(false == Strings.isNullOrEmpty(tableNameSuffix));
    Preconditions.checkNotNull(provisionedThroughputMap);
    Preconditions.checkNotNull(attributeDefinitions);
    this.dynamoDB = amazonDynamoDB;
    this.tableNameSuffix = tableNameSuffix;
    final String tableName = Strings.isNullOrEmpty(prefix) ? tableNameSuffix
            : String.format(Locale.ENGLISH, "%s_%s", prefix, tableNameSuffix);
    this.table = new Table(this.dynamoDB, tableName);
    this.ptMap = provisionedThroughputMap;
    this.gsis = gsiList != null ? new HashMap<>() : null;
    this.definitions = new HashMap<>(attributeDefinitions);
    this.lookupKeyConditions = new HashMap<>();
    this.objectMapper = objectMapper;
    this.clazz = clazz;
    this.gsiHashKeys = new HashMap<>();
    this.gsiRangeKeys = new HashMap<>();
    this.versionProperty = Strings.isNullOrEmpty(versionString) ? null : versionString;
    Optional.ofNullable(gsiList).orElse(new HashMap<>()).values().forEach(gsi -> {
        final String indexName = gsi.getIndexName();
        //make a copy
        final GlobalSecondaryIndex copy = new GlobalSecondaryIndex().withIndexName(gsi.getIndexName())
                .withKeySchema(gsi.getKeySchema()).withProjection(gsi.getProjection())
                .withProvisionedThroughput(ptMap.get(indexName));
        final String hk = copy.getKeySchema().get(0).getAttributeName();
        final String rk = copy.getKeySchema().size() == 2 ? copy.getKeySchema().get(1).getAttributeName()
                : null;
        this.gsis.put(indexName, copy);
        this.gsiHashKeys.put(indexName, hk);
        if (rk != null) {
            lookupKeyConditions.put(indexName,
                    String.format(Locale.ENGLISH, "%s = :%s and %s = :%s", hk, hk, rk, rk));
            this.gsiRangeKeys.put(indexName, rk);
        } else {
            lookupKeyConditions.put(indexName, String.format(Locale.ENGLISH, "%s = :%s", hk, hk));
        }
    });

    Preconditions.checkNotNull(baseTableKeyNames);
    Preconditions.checkArgument(false == baseTableKeyNames.isEmpty(), "need at least one key");
    Preconditions.checkArgument(baseTableKeyNames.size() <= 2,
            "cant have more than two keys (one partition and one sort key)");

    //add the attribute definitions for the base table key schema
    baseTableKeyNames.stream().forEach(name -> Preconditions.checkArgument(this.definitions.containsKey(name)));
    this.schemata = Lists.newArrayList(new KeySchemaElement(baseTableKeyNames.get(0), KeyType.HASH));
    if (baseTableKeyNames.size() == 2) {
        schemata.add(new KeySchemaElement(baseTableKeyNames.get(1), KeyType.RANGE));
    }
    hashKeyName = schemata.get(0).getAttributeName();
    rangeKeyName = schemata.size() == 2 ? schemata.get(1).getAttributeName() : null;
    conditionalCreateCondition = rangeKeyName != null
            ? String.format(Locale.ENGLISH, "attribute_not_exists(%s) and attribute_not_exists(%s)",
                    hashKeyName, rangeKeyName)
            : String.format(Locale.ENGLISH, "attribute_not_exists(%s)", hashKeyName);
    conditionalDeleteCondition = rangeKeyName != null
            ? String.format(Locale.ENGLISH, "attribute_exists(%s) and attribute_exists(%s)", hashKeyName,
                    rangeKeyName)
            : String.format(Locale.ENGLISH, "attribute_exists(%s)", hashKeyName);
}

From source file:tr.com.serkanozal.samba.cache.impl.SambaGlobalCache.java

License:Open Source License

private Table ensureTableAvailable() {
    boolean tableExist = false;
    try {//from   w  w  w  .j a va  2s . com
        DYNAMO_DB.describeTable(DYNAMO_DB_TABLE_NAME);
        tableExist = true;
    } catch (ResourceNotFoundException e) {
    }

    if (!tableExist) {
        ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
        attributeDefinitions.add(new AttributeDefinition().withAttributeName("id").withAttributeType("S"));

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

        StreamSpecification streamSpecification = new StreamSpecification();
        streamSpecification.setStreamEnabled(true);
        streamSpecification.setStreamViewType(StreamViewType.NEW_AND_OLD_IMAGES);

        CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(DYNAMO_DB_TABLE_NAME)
                .withKeySchema(keySchema).withAttributeDefinitions(attributeDefinitions)
                .withStreamSpecification(streamSpecification)
                .withProvisionedThroughput(new ProvisionedThroughput()
                        .withReadCapacityUnits((long) DYNAMO_DB_TABLE_READ_CAPACITY_PER_SECOND)
                        .withWriteCapacityUnits((long) DYNAMO_DB_TABLE_WRITE_CAPACITY_PER_SECOND));

        try {
            LOGGER.info(String.format("Creating DynamoDB table (%s) creation, because it is not exist",
                    DYNAMO_DB_TABLE_NAME));

            DYNAMO_DB.createTable(createTableRequest);
        } catch (ResourceInUseException e) {
            LOGGER.info(String.format("Ignoring DynamoDB table (%s) creation, because it is already exist",
                    DYNAMO_DB_TABLE_NAME));
        }
    } else {
        LOGGER.info(String.format("Ignoring DynamoDB table (%s) creation, because it is already exist",
                DYNAMO_DB_TABLE_NAME));
    }

    while (true) {
        DescribeTableResult describeTableResult = DYNAMO_DB.describeTable(DYNAMO_DB_TABLE_NAME);
        TableDescription tableDescription = describeTableResult.getTable();
        if ("ACTIVE".equals(tableDescription.getTableStatus())) {
            break;
        }
        LOGGER.info(String.format("DynamoDB table (%s) is not active yet, waiting until it is active ...",
                DYNAMO_DB_TABLE_NAME));
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
    }

    SCHEDULED_EXECUTOR_SERVICE.scheduleAtFixedRate(new StreamListener(), 0, 1000, TimeUnit.MILLISECONDS);

    return new Table(DYNAMO_DB, DYNAMO_DB_TABLE_NAME);
}