Java Code Examples for com.amazonaws.services.dynamodbv2.util.TableUtils

The following examples show how to use com.amazonaws.services.dynamodbv2.util.TableUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public final Response createTable(final Request request) {

    final CreateTableRequest createTableRequest = new CreateTableRequest(
            Arrays.asList(
                    new AttributeDefinition(request.getPartitionKey(), ScalarAttributeType.S),
                    new AttributeDefinition(request.getSortKey(), ScalarAttributeType.N)),
            request.getTableName(),
            Arrays.asList(
                    new KeySchemaElement(request.getPartitionKey(), KeyType.HASH),
                    new KeySchemaElement(request.getSortKey(), KeyType.RANGE)),
            new ProvisionedThroughput(request.getReadCapacityUnits(), request.getWriteCapacityUnits()));

    TableUtils.createTableIfNotExists(this.dynamoDBClient, createTableRequest);

    try {
        TableUtils.waitUntilActive(this.dynamoDBClient, request.getTableName());
    } catch (final AmazonClientException | InterruptedException e) {
        return new Response(null, "Failed in table active check in API version V2: " + e.getMessage());
    }

    return new Response(request.getTableName() + " created with API version V2.", null);
}
 
Example 2
Source Project: spring-data-dynamodb-examples   Source File: Application.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public CommandLineRunner multirepo(ConfigurableApplicationContext ctx, CustomerRepository jpaRepository,
		DeviceRepository dynamoDBRepository, AmazonDynamoDB amazonDynamoDB, DynamoDBMapper dynamoDBMapper,
		DynamoDBMapperConfig config) {
	return (args) -> {
		demoJPA(jpaRepository);

		CreateTableRequest ctr = dynamoDBMapper.generateCreateTableRequest(Device.class)
				.withProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
		TableUtils.createTableIfNotExists(amazonDynamoDB, ctr);
		TableUtils.waitUntilActive(amazonDynamoDB, ctr.getTableName());


		demoDynamoDB(dynamoDBRepository);

		ctx.close();
	};
}
 
Example 3
Source Project: spring-data-dynamodb-examples   Source File: Application.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public CommandLineRunner rest(ConfigurableApplicationContext ctx, UserRepository dynamoDBRepository,
		AmazonDynamoDB amazonDynamoDB, DynamoDBMapper dynamoDBMapper, DynamoDBMapperConfig config) {
	return (args) -> {

		CreateTableRequest ctr = dynamoDBMapper.generateCreateTableRequest(User.class)
				.withProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
		TableUtils.createTableIfNotExists(amazonDynamoDB, ctr);
		TableUtils.waitUntilActive(amazonDynamoDB, ctr.getTableName());

		createEntities(dynamoDBRepository);

		log.info("");
		log.info("Run curl -v http://localhost:8080/users and follow the HATEOS links");
		log.info("");
		log.info("Press <enter> to shutdown");
		System.in.read();
		ctx.close();
	};
}
 
Example 4
Source Project: aws-dynamodb-encryption-java   Source File: ScanITCase.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUpTestData() throws Exception {
    String keyName = "id";
    CreateTableRequest createTableRequest = new CreateTableRequest()
            .withTableName(TABLE_NAME)
            .withKeySchema(new KeySchemaElement().withAttributeName(keyName).withKeyType(KeyType.HASH))
            .withAttributeDefinitions(
                    new AttributeDefinition().withAttributeName(keyName).withAttributeType(
                            ScalarAttributeType.S));
    createTableRequest.setProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(10L)
            .withWriteCapacityUnits(5L));

    TableUtils.createTableIfNotExists(dynamo, createTableRequest);
    TableUtils.waitUntilActive(dynamo, TABLE_NAME);

    createTestData();
}
 
Example 5
@BeforeClass
public static void setUp() throws Exception {
    // Create a table
    DynamoDBTestBase.setUpTestBase();
    String keyName = KEY_NAME;
    CreateTableRequest createTableRequest = new CreateTableRequest()
            .withTableName(TABLE_NAME)
            .withKeySchema(new KeySchemaElement().withAttributeName(keyName).withKeyType(KeyType.HASH))
            .withAttributeDefinitions(
                    new AttributeDefinition().withAttributeName(keyName).withAttributeType(
                            ScalarAttributeType.S));
    createTableRequest.setProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(10L)
            .withWriteCapacityUnits(5L));

    if (TableUtils.createTableIfNotExists(dynamo, createTableRequest)) {
        TableUtils.waitUntilActive(dynamo, TABLE_NAME);
    }
}
 
Example 6
protected static void setUpTableWithRangeAttribute() throws Exception {
    setUp();

    String keyName = DynamoDBCryptoIntegrationTestBase.KEY_NAME;
    String rangeKeyAttributeName = "rangeKey";

    CreateTableRequest createTableRequest = new CreateTableRequest()
            .withTableName(TABLE_WITH_RANGE_ATTRIBUTE)
            .withKeySchema(new KeySchemaElement().withAttributeName(keyName).withKeyType(KeyType.HASH),
                    new KeySchemaElement().withAttributeName(rangeKeyAttributeName).withKeyType(KeyType.RANGE))
            .withAttributeDefinitions(
                    new AttributeDefinition().withAttributeName(keyName).withAttributeType(
                            ScalarAttributeType.N),
                    new AttributeDefinition().withAttributeName(rangeKeyAttributeName).withAttributeType(
                            ScalarAttributeType.N));
    createTableRequest.setProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(10L)
            .withWriteCapacityUnits(5L));

    if (TableUtils.createTableIfNotExists(dynamo, createTableRequest)) {
        TableUtils.waitUntilActive(dynamo, TABLE_WITH_RANGE_ATTRIBUTE);
    }
}
 
Example 7
@BeforeClass
public static void setUp() throws Exception {
    DynamoDBCryptoIntegrationTestBase.setUp();
    dynamoMapper = TestDynamoDBMapperFactory.createDynamoDBMapper(dynamo);

    CreateTableRequest createTableRequest = new CreateTableRequest()
            .withTableName(tableName)
            .withKeySchema(new KeySchemaElement().withAttributeName(hashKeyName).withKeyType(KeyType.HASH))
            .withKeySchema(new KeySchemaElement().withAttributeName(rangeKeyName).withKeyType(KeyType.RANGE))
            .withAttributeDefinitions(new AttributeDefinition().withAttributeName(hashKeyName)
                    .withAttributeType(ScalarAttributeType.S))
            .withAttributeDefinitions(new AttributeDefinition().withAttributeName(rangeKeyName)
                    .withAttributeType(ScalarAttributeType.N));
    createTableRequest.setProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT);

    if (TableUtils.createTableIfNotExists(dynamo, createTableRequest)) {
        TableUtils.waitUntilActive(dynamo, tableName);
    }
}
 
Example 8
@BeforeClass
public static void setUp() throws Exception {
    DynamoDBMapperCryptoIntegrationTestBase.setUp();

    // Create a table
    String keyName = DynamoDBMapperCryptoIntegrationTestBase.KEY_NAME;
    String rangeKeyAttributeName = "rangeKey";

    CreateTableRequest createTableRequest = new CreateTableRequest()
            .withTableName(TABLE_NAME)
            .withKeySchema(new KeySchemaElement().withAttributeName(keyName).withKeyType(KeyType.HASH),
                    new KeySchemaElement().withAttributeName(rangeKeyAttributeName).withKeyType(KeyType.RANGE))
            .withAttributeDefinitions(
                    new AttributeDefinition().withAttributeName(keyName).withAttributeType(
                            ScalarAttributeType.S),
                    new AttributeDefinition().withAttributeName(rangeKeyAttributeName).withAttributeType(
                            ScalarAttributeType.S));
    createTableRequest.setProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(10L)
            .withWriteCapacityUnits(5L));

    if (TableUtils.createTableIfNotExists(dynamo, createTableRequest)) {
        TableUtils.waitUntilActive(dynamo, TABLE_NAME);
    }
}
 
Example 9
Source Project: geowave   Source File: DynamoDBOperations.java    License: Apache License 2.0 6 votes vote down vote up
private boolean createTable(final String qName, final Supplier<CreateTableRequest> tableRequest) {
  synchronized (tableExistsCache) {
    final Boolean tableExists = tableExistsCache.get(qName);
    if ((tableExists == null) || !tableExists) {
      final boolean tableCreated =
          TableUtils.createTableIfNotExists(
              client,
              tableRequest.get().withProvisionedThroughput(
                  new ProvisionedThroughput(
                      Long.valueOf(options.getReadCapacity()),
                      Long.valueOf(options.getWriteCapacity()))));
      if (tableCreated) {
        try {
          TableUtils.waitUntilActive(client, qName);
        } catch (TableNeverTransitionedToStateException | InterruptedException e) {
          LOGGER.error("Unable to wait for active table '" + qName + "'", e);
        }
      }
      tableExistsCache.put(qName, true);
      return true;
    }
  }
  return false;
}
 
Example 10
Source Project: aws-dynamodb-examples   Source File: MoviesCreateTable.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        
        client.setEndpoint("http://localhost:8000");
        DynamoDB dynamoDB = new DynamoDB(client);
        
        String tableName = "Movies";
        Table table = dynamoDB.createTable(tableName,
                Arrays.asList(
                        new KeySchemaElement("year", KeyType.HASH),
                        new KeySchemaElement("title", KeyType.RANGE)), 
                Arrays.asList(
                        new AttributeDefinition("year", ScalarAttributeType.N),
                        new AttributeDefinition("title", ScalarAttributeType.S)), 
                new ProvisionedThroughput(10L, 10L));

        try {
            TableUtils.waitUntilActive(client, tableName);
            System.out.println("Table status: " + table.getDescription().getTableStatus());
        } catch (AmazonClientException e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
 
Example 11
@Override
public final Response putItem(final Request request) {

    if (request.isWaitForActive()) {
        try {
            TableUtils.waitUntilActive(this.dynamoDBClient, request.getTableName());
        } catch (InterruptedException e) {
            return new Response(null,
                    "Error while waiting for table to become active with API version V2: " + e.getMessage());
        }
    }

    Map<String, AttributeValue> attributeValueMap = new HashMap<>();

    attributeValueMap.put(request.getPartitionKey(), new AttributeValue(request.getPartitionKeyValue()));
    attributeValueMap.put(request.getSortKey(), new AttributeValue().withN(request.getSortKeyValue().toString()));

    if (request.getStringData() != null) {
        request.getStringData().forEach((k, v)
                -> attributeValueMap.put(k, new AttributeValue(v)));
    }

    if (request.getIntegerData() != null) {
        request.getIntegerData().forEach((k, v)
                -> attributeValueMap.put(k, new AttributeValue().withN(v.toString())));
    }

    final PutItemRequest putItemRequest = new PutItemRequest()
            .withTableName(request.getTableName())
            .withItem(attributeValueMap)
            .withReturnValues(ReturnValue.NONE);

    this.dynamoDBClient.putItem(putItemRequest);

    return new Response("Item added into " + request.getTableName() + " with API version V2.", null);
}
 
Example 12
Source Project: spring-data-dynamodb-examples   Source File: Application.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public CommandLineRunner custom(ConfigurableApplicationContext ctx, UserRepository userRepository,
		AmazonDynamoDB amazonDynamoDB, DynamoDBMapper dynamoDBMapper, DynamoDBMapperConfig config) {
	return (args) -> {
		CreateTableRequest ctr = dynamoDBMapper.generateCreateTableRequest(User.class)
				.withProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
		TableUtils.createTableIfNotExists(amazonDynamoDB, ctr);
		TableUtils.waitUntilActive(amazonDynamoDB, ctr.getTableName());

		demoCustomInterface(userRepository);

		ctx.close();
	};
}
 
Example 13
@Before
public void init() throws Exception {
	CreateTableRequest ctr = mapper.generateCreateTableRequest(User.class)
			.withProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
	tableWasCreatedForTest = TableUtils.createTableIfNotExists(amazonDynamoDB, ctr);
	if (tableWasCreatedForTest) {
		log.info("Created table {}", ctr.getTableName());
	}
	TableUtils.waitUntilActive(amazonDynamoDB, ctr.getTableName());
	log.info("Table {} is active", ctr.getTableName());
}
 
Example 14
@After
public void destroy() throws Exception {
	if (tableWasCreatedForTest) {
		DeleteTableRequest dtr = mapper.generateDeleteTableRequest(User.class);
		TableUtils.deleteTableIfExists(amazonDynamoDB, dtr);
		log.info("Deleted table {}", dtr.getTableName());
	}
}
 
Example 15
@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);
}
 
Example 16
Source Project: geowave   Source File: DynamoDBOperations.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public MetadataWriter createMetadataWriter(final MetadataType metadataType) {
  final String tableName = getMetadataTableName(metadataType);

  synchronized (DynamoDBOperations.tableExistsCache) {
    final Boolean tableExists = DynamoDBOperations.tableExistsCache.get(tableName);
    if ((tableExists == null) || !tableExists) {
      final boolean tableCreated =
          TableUtils.createTableIfNotExists(
              client,
              new CreateTableRequest().withTableName(tableName).withAttributeDefinitions(
                  new AttributeDefinition(
                      METADATA_PRIMARY_ID_KEY,
                      ScalarAttributeType.B)).withKeySchema(
                          new KeySchemaElement(
                              METADATA_PRIMARY_ID_KEY,
                              KeyType.HASH)).withAttributeDefinitions(
                                  new AttributeDefinition(
                                      METADATA_TIMESTAMP_KEY,
                                      ScalarAttributeType.N)).withKeySchema(
                                          new KeySchemaElement(
                                              METADATA_TIMESTAMP_KEY,
                                              KeyType.RANGE)).withProvisionedThroughput(
                                                  new ProvisionedThroughput(
                                                      Long.valueOf(5),
                                                      Long.valueOf(5))));
      if (tableCreated) {
        try {
          TableUtils.waitUntilActive(client, tableName);
        } catch (TableNeverTransitionedToStateException | InterruptedException e) {
          LOGGER.error("Unable to wait for active table '" + tableName + "'", e);
        }
      }
      DynamoDBOperations.tableExistsCache.put(tableName, true);
    }
  }

  return new DynamoDBMetadataWriter(this, tableName);
}
 
Example 17
Source Project: tutorials   Source File: ApplicationPropertiesConfigurations.java    License: MIT License 5 votes vote down vote up
private void initDatabase() {
    // Create the table
    DynamoDBMapper mapper = new DynamoDBMapper(amazonDynamoDb);
    CreateTableRequest tableRequest = mapper.generateCreateTableRequest(ArchaiusProperties.class);
    tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
    TableUtils.createTableIfNotExists(amazonDynamoDb, tableRequest);

    // Populate the table
    ArchaiusProperties property = new ArchaiusProperties("baeldung.archaius.properties.one", "one FROM:dynamoDB");
    ArchaiusProperties property3 = new ArchaiusProperties("baeldung.archaius.properties.three", "three FROM:dynamoDB");
    repository.saveAll(Arrays.asList(property, property3));
}
 
Example 18
protected static void setUpTableWithIndexRangeAttribute(boolean recreateTable) throws Exception {
    setUp();
    if (recreateTable) {
        dynamo.deleteTable(new DeleteTableRequest().withTableName(TABLE_WITH_INDEX_RANGE_ATTRIBUTE));
        waitForTableToBecomeDeleted(TABLE_WITH_INDEX_RANGE_ATTRIBUTE);
    }

    String keyName = DynamoDBCryptoIntegrationTestBase.KEY_NAME;
    String rangeKeyAttributeName = "rangeKey";
    String indexFooRangeKeyAttributeName = "indexFooRangeKey";
    String indexBarRangeKeyAttributeName = "indexBarRangeKey";
    String multipleIndexRangeKeyAttributeName = "multipleIndexRangeKey";
    String indexFooName = "index_foo";
    String indexBarName = "index_bar";
    String indexFooCopyName = "index_foo_copy";
    String indexBarCopyName = "index_bar_copy";

    CreateTableRequest createTableRequest = new CreateTableRequest()
            .withTableName(TABLE_WITH_INDEX_RANGE_ATTRIBUTE)
            .withKeySchema(
                    new KeySchemaElement().withAttributeName(keyName).withKeyType(KeyType.HASH),
                    new KeySchemaElement().withAttributeName(rangeKeyAttributeName).withKeyType(KeyType.RANGE))
            .withLocalSecondaryIndexes(
                    new LocalSecondaryIndex()
                            .withIndexName(indexFooName)
                            .withKeySchema(
                                    new KeySchemaElement().withAttributeName(keyName).withKeyType(KeyType.HASH),
                                    new KeySchemaElement().withAttributeName(indexFooRangeKeyAttributeName).withKeyType(KeyType.RANGE))
                            .withProjection(new Projection().withProjectionType(ProjectionType.ALL)),
                    new LocalSecondaryIndex()
                            .withIndexName(indexBarName)
                            .withKeySchema(
                                    new KeySchemaElement().withAttributeName(keyName).withKeyType(KeyType.HASH),
                                    new KeySchemaElement().withAttributeName(indexBarRangeKeyAttributeName).withKeyType(KeyType.RANGE))
                            .withProjection(new Projection()
                                                .withProjectionType(ProjectionType.ALL)),
                    new LocalSecondaryIndex()
                            .withIndexName(indexFooCopyName)
                            .withKeySchema(
                                    new KeySchemaElement().withAttributeName(keyName).withKeyType(KeyType.HASH),
                                    new KeySchemaElement().withAttributeName(multipleIndexRangeKeyAttributeName).withKeyType(KeyType.RANGE))
                            .withProjection(new Projection()
                                                .withProjectionType(ProjectionType.ALL)),
                    new LocalSecondaryIndex()
                            .withIndexName(indexBarCopyName)
                            .withKeySchema(
                                    new KeySchemaElement().withAttributeName(keyName).withKeyType(KeyType.HASH),
                                    new KeySchemaElement().withAttributeName(multipleIndexRangeKeyAttributeName).withKeyType(KeyType.RANGE))
                            .withProjection(new Projection()
                                                .withProjectionType(ProjectionType.ALL)))
            .withAttributeDefinitions(
                    new AttributeDefinition().withAttributeName(keyName).withAttributeType(ScalarAttributeType.N),
                    new AttributeDefinition().withAttributeName(rangeKeyAttributeName).withAttributeType(ScalarAttributeType.N),
                    new AttributeDefinition().withAttributeName(indexFooRangeKeyAttributeName).withAttributeType(ScalarAttributeType.N),
                    new AttributeDefinition().withAttributeName(indexBarRangeKeyAttributeName).withAttributeType(ScalarAttributeType.N),
                    new AttributeDefinition().withAttributeName(multipleIndexRangeKeyAttributeName).withAttributeType(ScalarAttributeType.N));
    createTableRequest.setProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(10L)
            .withWriteCapacityUnits(5L));

    if (TableUtils.createTableIfNotExists(dynamo, createTableRequest)) {
        TableUtils.waitUntilActive(dynamo, TABLE_WITH_INDEX_RANGE_ATTRIBUTE);
    }
}