private static AmazonDynamoDB getClient() { if (null != dynamodbClient) { return dynamodbClient; } String region = System.getProperty("DYNAMODB_REGION"); if (null == region) { System.err.println("Region not set, default \"" + Regions.US_EAST_1.name() + "\" is used"); region = Regions.US_EAST_1.name(); } System.out.println("DynamoDB region: " + region); dynamodbClient = AmazonDynamoDBClientBuilder.standard() .withRegion(region) .build(); return dynamodbClient; }
public static final AmazonDynamoDB getClient() { if (null != dynamodbClient) { return dynamodbClient; } String region = System.getenv("DYNAMODB_REGION"); if (null == region) { System.err.println("Region is null, using default \"" + Regions.US_WEST_1 + "\""); region = Regions.US_WEST_1.name(); } System.out.println("DynamoDB region: " + region); dynamodbClient = AmazonDynamoDBClientBuilder.standard() .withRegion(region) .build(); System.out.println("Got DynamoDB client..."); return dynamodbClient; }
private static AmazonDynamoDB getClient() { if (null != dynamodbClient) return dynamodbClient; String region = System.getProperty("DYNAMODB_REGION"); if (null == region) { System.err.println("Region not set, default \"" + Regions.US_WEST_1.name() + "\" is used"); region = Regions.US_WEST_1.name(); } System.out.println("DynamoDB region: " + region); dynamodbClient = AmazonDynamoDBClientBuilder.standard() .withRegion(region) .build(); return dynamodbClient; }
/** * Returns a client instance for AWS DynamoDB. * @return a client that talks to DynamoDB */ public static AmazonDynamoDB getClient() { if (ddbClient != null) { return ddbClient; } if (Config.IN_PRODUCTION) { ddbClient = AmazonDynamoDBClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider( new BasicAWSCredentials(Config.AWS_ACCESSKEY, Config.AWS_SECRETKEY))). withRegion(Config.AWS_REGION).build(); } else { ddbClient = AmazonDynamoDBClientBuilder.standard(). withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("local", "null"))). withEndpointConfiguration(new EndpointConfiguration(LOCAL_ENDPOINT, "")).build(); } if (!existsTable(Config.getRootAppIdentifier())) { createTable(Config.getRootAppIdentifier()); } ddb = new DynamoDB(ddbClient); Para.addDestroyListener(new DestroyListener() { public void onDestroy() { shutdownClient(); } }); return ddbClient; }
@Before public void before() throws Exception { final String userHome = System.getProperty("user.home"); final String propertiesFilename = userHome + "/metamodel-integrationtest-configuration.properties"; final File file = new File(propertiesFilename); Assume.assumeTrue(file.exists()); final Properties props = new Properties(); props.load(new FileReader(file)); final String accessKey = props.getProperty("dynamodb.accessKey"); final String secretKey = props.getProperty("dynamodb.secretKey"); Assume.assumeNotNull(accessKey, secretKey); final Regions region = Regions.fromName(props.getProperty("dynamodb.region", Regions.DEFAULT_REGION.getName())); final AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); final AWSCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(credentials); client = AmazonDynamoDBClientBuilder.standard().withRegion(region).withCredentials(credentialsProvider).build(); }
public MAVLinkMessagesTable() { if (System.getenv(SPL_DYNAMODB_TABLE) != null) { tableName = System.getenv(SPL_DYNAMODB_TABLE); } AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder.defaultClient(); DynamoDB dynamoDB = new DynamoDB(dynamoDBClient); table = dynamoDB.getTable(tableName); }
/** * Creates the DynamoDB client {@link Bean}. * * Uses the default client, but if a region is unspecified, uses {@code us-east-1}. * * @return The DynamoDB client. */ @Bean public AmazonDynamoDB dynamoDbClient() { AmazonDynamoDB client; try { client = AmazonDynamoDBClientBuilder.defaultClient(); } catch (SdkClientException exception) { API_LOG.info("Default DynamoDB client failed to build, trying again with region us-east-1", exception); client = planB(); } return client; }
@Test public void testDefaultClient() { mockStatic(AmazonDynamoDBClientBuilder.class); when(AmazonDynamoDBClientBuilder.defaultClient()).thenReturn(Mockito.mock(AmazonDynamoDB.class)); assertNotNull(underTest.dynamoDbClient()); verify(underTest, times(0)).planB(); }
@Test public void testRegionClient() { mockStatic(AmazonDynamoDBClientBuilder.class); when(AmazonDynamoDBClientBuilder.defaultClient()).thenThrow(new SdkClientException("meep")); doAnswer(invocationOnMock -> null).when(underTest).planB(); underTest.dynamoDbClient(); verify(underTest, times(1)).planB(); }
public static DynamoDB fromCredentials(AWSCredentialsProvider awsCredentials, ClientConfiguration clientConfiguration, SecretsGroupIdentifier groupIdentifier, ReadWriteLock readWriteLock) { AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard() .withCredentials(awsCredentials) .withClientConfiguration(verifyOrThrow(transformAndVerifyOrThrow(clientConfiguration))) .withRegion(groupIdentifier.region.getName()) .build(); return new DynamoDB(client, awsCredentials, clientConfiguration, groupIdentifier, readWriteLock); }
private static void cleanUpDynamoDBTables(Regions testRegion, String testResourcePrefix, Date createdBeforeThreshold, AWSCredentialsProvider awsCredentials) { LOG.info("Cleaning DynamoDB..."); AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder.standard() .withCredentials(awsCredentials) .withRegion(testRegion) .build(); List<String> tableNames = dynamoDBClient.listTables().getTableNames(); for (String tableName: tableNames) { if (!tableName.startsWith(testResourcePrefix)) { continue; } LOG.info(String.format("Checking if table %s needs cleaning...", tableName)); try { TableDescription desc = dynamoDBClient.describeTable(tableName).getTable(); if (!desc.getTableName().equals(TableStatus.DELETING.toString()) && desc.getCreationDateTime() != null && desc.getCreationDateTime().before(createdBeforeThreshold)) { LOG.info("Cleaning up table: " + tableName); dynamoDBClient.deleteTable(tableName); } } catch (ResourceNotFoundException e) { LOG.info("Looks like table was already cleaned up: " + tableName); } } }
/** * Create {@link AmazonDynamoDB} with customized configuration. * @return {@link AmazonDynamoDB} */ @Bean(destroyMethod = "shutdown") public AmazonDynamoDB createAmazonDynamoDB() { return AmazonDynamoDBClientBuilder.standard() .withRegion(Regions.DEFAULT_REGION) .build(); }
public static void main(String[] args) { final String USAGE = "\n" + "Usage:\n" + " UpdateTable <table> <read> <write>\n\n" + "Where:\n" + " table - the table to put the item in.\n" + " read - the new read capacity of the table.\n" + " write - the new write capacity of the table.\n\n" + "Example:\n" + " UpdateTable HelloTable 16 10\n"; if (args.length < 3) { System.out.println(USAGE); System.exit(1); } String table_name = args[0]; Long read_capacity = Long.parseLong(args[1]); Long write_capacity = Long.parseLong(args[2]); System.out.format( "Updating %s with new provisioned throughput values\n", table_name); System.out.format("Read capacity : %d\n", read_capacity); System.out.format("Write capacity : %d\n", write_capacity); ProvisionedThroughput table_throughput = new ProvisionedThroughput( read_capacity, write_capacity); final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient(); try { ddb.updateTable(table_name, table_throughput); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } System.out.println("Done!"); }
public static void main(String[] args) { final String USAGE = "\n" + "Usage:\n" + " DeleteTable <table>\n\n" + "Where:\n" + " table - the table to delete.\n\n" + "Example:\n" + " DeleteTable Greetings\n\n" + "**Warning** This program will actually delete the table\n" + " that you specify!\n"; if (args.length < 1) { System.out.println(USAGE); System.exit(1); } String table_name = args[0]; System.out.format("Deleting table %s...\n", table_name); final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient(); try { ddb.deleteTable(table_name); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } System.out.println("Done!"); }
public static void main(String[] args) { final String USAGE = "\n" + "Usage:\n" + " DeleteItem <table> <name>\n\n" + "Where:\n" + " table - the table to delete the item from.\n" + " name - the item to delete from the table,\n" + " using the primary key \"Name\"\n\n" + "Example:\n" + " DeleteItem HelloTable World\n\n" + "**Warning** This program will actually delete the item\n" + " that you specify!\n"; if (args.length < 2) { System.out.println(USAGE); System.exit(1); } String table_name = args[0]; String name = args[1]; System.out.format("Deleting item \"%s\" from %s\n", name, table_name); HashMap<String,AttributeValue> key_to_get = new HashMap<String,AttributeValue>(); key_to_get.put("Name", new AttributeValue(name)); final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient(); try { ddb.deleteItem(table_name, key_to_get); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } System.out.println("Done!"); }
public DynamoDbDelegate(final String endpoint, final String region, final AWSCredentialsProvider provider, final ClientConfiguration clientConfig, final Configuration titanConfig, final Map<String, RateLimiter> readRateLimit, final Map<String, RateLimiter> writeRateLimit, final long maxRetries, final long retryMillis, final String prefix, final String metricsPrefix, final RateLimiter controlPlaneRateLimiter) { if (prefix == null) { throw new IllegalArgumentException("prefix must be set"); } if (metricsPrefix == null || metricsPrefix.isEmpty()) { throw new IllegalArgumentException("metrics-prefix may not be null or empty"); } this.metricsPrefix = metricsPrefix; executorGaugeName = String.format("%s.%s_executor-queue-size", this.metricsPrefix, prefix); clientThreadPool = getPoolFromNs(titanConfig); if (!MetricManager.INSTANCE.getRegistry().getNames().contains(executorGaugeName)) { MetricManager.INSTANCE.getRegistry().register(executorGaugeName, (Gauge<Integer>) () -> clientThreadPool.getQueue().size()); } client = AmazonDynamoDBClientBuilder.standard() .withCredentials(provider) .withClientConfiguration(clientConfig) .withEndpointConfiguration(getEndpointConfiguration(Optional.ofNullable(endpoint), region)) .build(); this.readRateLimit = readRateLimit; this.writeRateLimit = writeRateLimit; this.controlPlaneRateLimiter = controlPlaneRateLimiter; this.maxConcurrentUsers = titanConfig.get(Constants.DYNAMODB_CLIENT_EXECUTOR_MAX_CONCURRENT_OPERATIONS); this.maxRetries = maxRetries; this.retryMillis = retryMillis; if (maxConcurrentUsers < 1) { throw new IllegalArgumentException("need at least one user otherwise wont make progress on scan"); } this.listTablesApiName = String.format("%s_ListTables", prefix); }
@Test public void getterSetterTest() { String randomValue = UUID.randomUUID().toString(); DynamoExample exampleClient = new DynamoExample(AmazonDynamoDBClientBuilder .standard() .withEndpointConfiguration(new EndpointConfiguration(subject.getEndpoint(), "eu-west-1")) .build()); exampleClient.createTable(); exampleClient.setValue(1L, randomValue); assertThat(exampleClient.getValue(1L), is(randomValue)); }
@PostConstruct public void init() { log.info("GeoAPI integration is active, setting up URLs etc"); useDynamoDirect = environment.getProperty("grassroot.geo.dynamodb.direct", Boolean.class, false); geoApiHost = environment.getProperty("grassroot.geo.apis.host", "localhost"); geoApiPort = environment.getProperty("grassroot.geo.apis.port", Integer.class, 80); if (useDynamoDirect) { log.info("okay we are using dynamo db directly for geo APIs ..."); dynamoDBClient = AmazonDynamoDBClientBuilder.standard() .withRegion(Regions.EU_WEST_1) .withCredentials(new ProfileCredentialsProvider("geoApisDynamoDb")).build(); } }
public static AmazonDynamoDBClient createDynamoDBClient() { BasicCredentialsProvider credentials = BasicCredentialsProvider.standard(); AmazonDynamoDBClient client = !credentials.isValid() ? null : (AmazonDynamoDBClient) AmazonDynamoDBClientBuilder.standard() .withCredentials(credentials) .withRegion("eu-west-1") .build(); return client; }
private DynamoDBManager() { final AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient(); mapper = new DynamoDBMapper(client); }
public RepositoryFactoryImpl() { dynamoDbClient = AmazonDynamoDBClientBuilder.standard() .withRegion(Regions.US_EAST_1).build(); dbMapper = new DynamoDBMapper(dynamoDbClient); }
public CloudNoticeDAO(boolean local) { ddb = local ? DynamoDBEmbedded.create().amazonDynamoDB() : AmazonDynamoDBClientBuilder.defaultClient(); verifyTables(); mapper = new DynamoDBMapper(ddb); }
public static void main(String[] args) { final String USAGE = "\n" + "Usage:\n" + " Query <table> <partitionkey> <partitionkeyvalue>\n\n" + "Where:\n" + " table - the table to put the item in.\n" + " partitionkey - partition key name of the table.\n" + " partitionkeyvalue - value of the partition key that should match.\n\n" + "Example:\n" + " Query GreetingsTable Language eng \n"; if (args.length < 3) { System.out.println(USAGE); System.exit(1); } String table_name = args[0]; String partition_key_name = args[1]; String partition_key_val = args[2]; String partition_alias = "#a"; System.out.format("Querying %s", table_name); System.out.println(""); final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient(); //set up an alias for the partition key name in case it's a reserved word HashMap<String,String> attrNameAlias = new HashMap<String,String>(); attrNameAlias.put(partition_alias, partition_key_name); //set up mapping of the partition name with the value HashMap<String, AttributeValue> attrValues = new HashMap<String,AttributeValue>(); attrValues.put(":"+partition_key_name, new AttributeValue().withS(partition_key_val)); QueryRequest queryReq = new QueryRequest() .withTableName(table_name) .withKeyConditionExpression(partition_alias + " = :" + partition_key_name) .withExpressionAttributeNames(attrNameAlias) .withExpressionAttributeValues(attrValues); try { QueryResult response = ddb.query(queryReq); System.out.println(response.getCount()); } catch (AmazonDynamoDBException e) { System.err.println(e.getErrorMessage()); System.exit(1); } System.out.println("Done!"); }
public static void main(String[] args) { final String USAGE = "\n" + "Usage:\n" + " CreateTable <table>\n\n" + "Where:\n" + " table - the table to create.\n\n" + "Example:\n" + " CreateTable HelloTable\n"; if (args.length < 1) { System.out.println(USAGE); System.exit(1); } /* Read the name from command args */ String table_name = args[0]; System.out.format( "Creating table \"%s\" with a simple primary key: \"Name\".\n", table_name); CreateTableRequest request = new CreateTableRequest() .withAttributeDefinitions(new AttributeDefinition( "Name", ScalarAttributeType.S)) .withKeySchema(new KeySchemaElement("Name", KeyType.HASH)) .withProvisionedThroughput(new ProvisionedThroughput( new Long(10), new Long(10))) .withTableName(table_name); final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient(); try { CreateTableResult result = ddb.createTable(request); System.out.println(result.getTableDescription().getTableName()); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } System.out.println("Done!"); }
public static void main(String[] args) { final String USAGE = "\n" + "Usage:\n" + " GetItem <table> <name> [projection_expression]\n\n" + "Where:\n" + " table - the table to get an item from.\n" + " name - the item to get.\n\n" + "You can add an optional projection expression (a quote-delimited,\n" + "comma-separated list of attributes to retrieve) to limit the\n" + "fields returned from the table.\n\n" + "Example:\n" + " GetItem HelloTable World\n" + " GetItem SiteColors text \"default, bold\"\n"; if (args.length < 2) { System.out.println(USAGE); System.exit(1); } String table_name = args[0]; String name = args[1]; String projection_expression = null; // if a projection expression was included, set it. if (args.length == 3) { projection_expression = args[2]; } System.out.format("Retrieving item \"%s\" from \"%s\"\n", name, table_name); HashMap<String,AttributeValue> key_to_get = new HashMap<String,AttributeValue>(); key_to_get.put("Name", new AttributeValue(name)); GetItemRequest request = null; if (projection_expression != null) { request = new GetItemRequest() .withKey(key_to_get) .withTableName(table_name) .withProjectionExpression(projection_expression); } else { request = new GetItemRequest() .withKey(key_to_get) .withTableName(table_name); } final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient(); try { Map<String,AttributeValue> returned_item = ddb.getItem(request).getItem(); if (returned_item != null) { Set<String> keys = returned_item.keySet(); for (String key : keys) { System.out.format("%s: %s\n", key, returned_item.get(key).toString()); } } else { System.out.format("No item found with the key %s!\n", name); } } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } }
public static void main(String[] args) { System.out.println("Your DynamoDB tables:\n"); final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient(); ListTablesRequest request; boolean more_tables = true; String last_name = null; while(more_tables) { try { if (last_name == null) { request = new ListTablesRequest().withLimit(10); } else { request = new ListTablesRequest() .withLimit(10) .withExclusiveStartTableName(last_name); } ListTablesResult table_list = ddb.listTables(request); List<String> table_names = table_list.getTableNames(); if (table_names.size() > 0) { for (String cur_name : table_names) { System.out.format("* %s\n", cur_name); } } else { System.out.println("No tables found!"); System.exit(0); } last_name = table_list.getLastEvaluatedTableName(); if (last_name == null) { more_tables = false; } } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } } System.out.println("\nDone!"); }
public static void main(String[] args) { final String USAGE = "\n" + "Usage:\n" + " DescribeTable <table>\n\n" + "Where:\n" + " table - the table to get information about.\n\n" + "Example:\n" + " DescribeTable HelloTable\n"; if (args.length < 1) { System.out.println(USAGE); System.exit(1); } String table_name = args[0]; System.out.format("Getting description for %s\n\n", table_name); final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient(); try { TableDescription table_info = ddb.describeTable(table_name).getTable(); if (table_info != null) { System.out.format("Table name : %s\n", table_info.getTableName()); System.out.format("Table ARN : %s\n", table_info.getTableArn()); System.out.format("Status : %s\n", table_info.getTableStatus()); System.out.format("Item count : %d\n", table_info.getItemCount().longValue()); System.out.format("Size (bytes): %d\n", table_info.getTableSizeBytes().longValue()); ProvisionedThroughputDescription throughput_info = table_info.getProvisionedThroughput(); System.out.println("Throughput"); System.out.format(" Read Capacity : %d\n", throughput_info.getReadCapacityUnits().longValue()); System.out.format(" Write Capacity: %d\n", throughput_info.getWriteCapacityUnits().longValue()); List<AttributeDefinition> attributes = table_info.getAttributeDefinitions(); System.out.println("Attributes"); for (AttributeDefinition a : attributes) { System.out.format(" %s (%s)\n", a.getAttributeName(), a.getAttributeType()); } } } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } System.out.println("\nDone!"); }
public static void main(String[] args) { final String USAGE = "\n" + "Usage:\n" + " CreateTable <table>\n\n" + "Where:\n" + " table - the table to create.\n\n" + "Example:\n" + " CreateTable GreetingsTable\n"; if (args.length < 1) { System.out.println(USAGE); System.exit(1); } /* Read the name from command args */ String table_name = args[0]; System.out.format("Creating table %s\n with a composite primary key:\n"); System.out.format("* Language - partition key\n"); System.out.format("* Greeting - sort key\n"); CreateTableRequest request = new CreateTableRequest() .withAttributeDefinitions( new AttributeDefinition("Language", ScalarAttributeType.S), new AttributeDefinition("Greeting", ScalarAttributeType.S)) .withKeySchema( new KeySchemaElement("Language", KeyType.HASH), new KeySchemaElement("Greeting", KeyType.RANGE)) .withProvisionedThroughput( new ProvisionedThroughput(new Long(10), new Long(10))) .withTableName(table_name); final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient(); try { CreateTableResult result = ddb.createTable(request); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } System.out.println("Done!"); }
private AmazonDynamoDB getDynamoDBClient() { return AmazonDynamoDBClientBuilder .standard() .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(getDynamoEndpoint(), null)) .build(); }
public AmazonDynamoDB getClient() { return AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration( new AwsClientBuilder.EndpointConfiguration(getEndpoint(), "us-west-2")) .build(); }
public DynamoDBClient(AWSCredentials credentials, Regions region) { client = AmazonDynamoDBClientBuilder.standard().withRegion(region) .withCredentials(new AWSStaticCredentialsProvider(credentials)).build(); dynamo = new DynamoDB(client); }
public DynamoDbDataContext() { this(AmazonDynamoDBClientBuilder.defaultClient(), null, true); }
public DynamoDbDataContext(SimpleTableDef[] tableDefs) { this(AmazonDynamoDBClientBuilder.defaultClient(), tableDefs, true); }
/** * Returns the default client that uses {@code us-east-1}. * * @return The DynamoDB client. */ AmazonDynamoDB planB() { return AmazonDynamoDBClientBuilder.standard().withRegion(Regions.US_EAST_1).build(); }
/** * The most convenient constructor just takes the Alexa session. An AWS client for accessing DynamoDB * will make use of all defaults in regards to credentials and region. The credentials used in this client need permission for reading, writing and * removing items from a DynamoDB table and also the right to create a table. On the very first read or * write operation of this handler it creates a table named like your Alexa App Id. * The table created consist of a hash-key and a sort-key. * If you don't want this handler to auto-create a table provide the name of an existing table in DynamoDB * in another constructor. * * @param session The Alexa session of your current skill invocation. */ public AWSDynamoStateHandler(final Session session) { this(session, AmazonDynamoDBClientBuilder.defaultClient(), null, 10L, 5L); }
/** * Takes the Alexa session and a table. An AWS client for accessing DynamoDB * will make use of all defaults in regards to credentials and region. The credentials used in this client need permission for reading, writing and * removing items from the given DynamoDB table. The table needs a string hash-key with name model-class and a string sort-key * of name amzn-user-id. The option of providing an existing table to this handler prevents it from checking its existence * which might end up with a better performance. You also don't need to provide permission of creating a DynamoDB table to * the credentials of the given AWS client. * * @param session The Alexa session of your current skill invocation. * @param tableName An existing table accessible by the client and with string hash-key named model-class and a string sort-key named amzn-user-id. */ public AWSDynamoStateHandler(final Session session, final String tableName) { this(session, AmazonDynamoDBClientBuilder.defaultClient(), tableName, 10L, 5L); }