/** * Creates the table and ignores any errors if it already exists. * * @param dynamo The Dynamo client to use. * @param createTableRequest The create table request. * @return True if created, false otherwise. */ public static final boolean createTableIfNotExists( final AmazonDynamoDB dynamo, final CreateTableRequest createTableRequest) { try { dynamo.createTable(createTableRequest); return true; } catch (final ResourceInUseException e) { if (LOG.isTraceEnabled()) { LOG.trace("Table " + createTableRequest.getTableName() + " already exists", e); } } return false; }
@Bean public AmazonDynamoDB amazonDynamoDB() { System.setProperty("java.library.path", "./DynamoDBLocal_lib"); try { Field fieldSysPath = ClassLoader.class.getDeclaredField("sys_paths"); fieldSysPath.setAccessible(true); fieldSysPath.set(null, null); } catch (Exception e) { e.printStackTrace(); } AmazonDynamoDB amazonDynamoDB = DynamoDBEmbedded.create(); String tableName = "person"; List<AttributeDefinition> attributeDefinitions = new ArrayList<>(); attributeDefinitions.add( new AttributeDefinition().withAttributeName("id").withAttributeType("S")); List<KeySchemaElement> keySchema = new ArrayList<>(); keySchema.add(new KeySchemaElement().withAttributeName("id").withKeyType(KeyType.HASH)); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withKeySchema(keySchema) .withAttributeDefinitions(attributeDefinitions) .withProvisionedThroughput( new ProvisionedThroughput().withReadCapacityUnits(5L).withWriteCapacityUnits(5L)); amazonDynamoDB.createTable(request); log.debug("created dynamodb table[{}]", tableName); return amazonDynamoDB; }