@Inject public DefaultGroupStorage( AmazonDynamoDB amazonDynamoDB, TableConfiguration tableConfiguration, @Named("dynamodbGroupWriteHystrix") HystrixConfiguration dynamodbGroupWriteHystrix, @Named("dynamodbGraphWriteHystrix") HystrixConfiguration dynamodbGraphWriteHystrix, @Named("dynamodbNamespaceGraphQueryHystrix") HystrixConfiguration dynamodbNamespaceGraphQueryHystrix, MetricRegistry metrics ) { this.amazonDynamoDB = amazonDynamoDB; this.dynamoDB = new DynamoDB(this.amazonDynamoDB); this.groupTableName = tableConfiguration.outlandGroupsTable; this.groupGraphTableName = tableConfiguration.outlandAppGraphTable; this.dynamodbGroupWriteHystrix = dynamodbGroupWriteHystrix; this.dynamodbGraphWriteHystrix = dynamodbGraphWriteHystrix; this.dynamodbNamespaceGraphQueryHystrix = dynamodbNamespaceGraphQueryHystrix; this.metrics = metrics; }
private boolean tryAddMissingPartition(String dyanmoDBTaableName,DynamoDB dynamoDBClient, Partition partition){ Table ddbTable= dynamoDBClient.getTable(dyanmoDBTaableName); Item item=new Item() .withPrimaryKey("PartitionSpec",partition.spec()) .withString("PartitionPath",partition.path()) .withString("PartitionName", partition.name()); PutItemSpec itemSpec=new PutItemSpec() .withItem(item) .withConditionExpression("attribute_not_exists(#ps)") .withNameMap(new NameMap() .with("#ps","PartitionSpec")); try{ ddbTable.putItem(itemSpec); System.out.println("Item was added to the table.PartitionSpec="+partition.spec()+"; Path="+partition.path()); return true; } catch(ConditionalCheckFailedException e){ System.out.println(e.toString()); System.out.println("Item already exists. PartitionSpec="+partition.spec()+"; Path="+partition.path()); return false; } }
protected Table createTable(CreateTableRequest request) throws InterruptedException { DynamoDB dynamoDB = new DynamoDB(tables.getAsyncClient()); request.withProvisionedThroughput(new ProvisionedThroughput() .withReadCapacityUnits(5L) .withWriteCapacityUnits(6L)); if (request.getTableName() == null) { String tableName = tables.getTestTableName(); tableName = tableName.replace('-', '_'); request.setTableName(tableName); } Table table = dynamoDB.createTable(request); table.waitForActive(); return table; }
@Inject protected DdbSchema(@Assisted SchemaBuilder builder, AmazonWebServiceClients amazonWebServiceClients, ClientConfigurations clientConfigurations, AWSCredentialsProviderFactory awsCredentialsProviderFactory) { _tableNameFormat = builder.getTableNameFormat(); AmazonDynamoDBClient client = amazonWebServiceClients.withEndpoint( new AmazonDynamoDBClient( awsCredentialsProviderFactory.create(builder.getCredProvider()), clientConfigurations.withProxy( new ClientConfiguration(), builder.getProxyEndpoint())), builder.getEndpoint()); _dynamodb = new DynamoDB(client); }
@Before public void setUp() { // Unique table for each run tableName = "table" + String.valueOf(tableCount++); dynamoDB.getDynamoDbClient().createTable( new CreateTableRequest() .withTableName(tableName) .withKeySchema(new KeySchemaElement("id", "HASH")) .withAttributeDefinitions( new AttributeDefinition("id", "S") ) .withProvisionedThroughput( new ProvisionedThroughput(1L, 1L) ) ); locker = new DynamoDbLocker( new DynamoDB(dynamoDB.getDynamoDbClient()), tableName, Clock.systemUTC() ); }
private void createTable(Class tableClass) { CreateTableRequest createTableRequest = mapper.generateCreateTableRequest(tableClass); createTableRequest.setProvisionedThroughput(new ProvisionedThroughput(dbReadCapacity, dbWriteCapacity)); if (tableExists(createTableRequest.getTableName())) { LOG.info("Table {} already exists", createTableRequest.getTableName()); return; } try { DynamoDB dynamoDB = new DynamoDB(amazonDynamoDB); Table table = dynamoDB.createTable(createTableRequest); LOG.info("Creating table {} ... ", createTableRequest.getTableName()); table.waitForActive(); LOG.info("Table {} was created successfully.", createTableRequest.getTableName()); } catch (Exception e) { LOG.error("Failed to create table {}. ", createTableRequest.getTableName()); LOG.error(e); throw new ConfigurationException("Failed to create table" + createTableRequest.getTableName(), e); } }
@PostConstruct private void init() { configurationId = SystemUtils.getSystemId(); dynamoDB = new DynamoDB(db); switch (SystemUtils.getSystemMode()) { case CLUSTER: dropConfiguration(removeS3Bucket); clusterConfigurationService.removeClusterInfrastructure(); break; case STANDALONE: dropConfiguration(removeS3Bucket); LOG.info("Terminating instance"); terminateInstance(); break; } }
@Override public void transform(Item scoreItem, DynamoDB dynamodb) { String playerName = scoreItem.getString(PLAYER_NAME); int score = scoreItem.getInt(SCORE); int gameLength = scoreItem.getInt(GAME_LENGTH); /* * The XSpec API allows you to use DynamoDB's expression language * to execute expressions on the service-side. * * https://java.awsblog.com/post/TxBG87QOQZRZJF/-DynamoDB-XSpec-API */ Table viewTable = dynamodb.getTable(PLAYER_STATS_TABLE_NAME); UpdateItemExpressionSpec incrementTotalOrder = new ExpressionSpecBuilder() .addUpdate(N(TOTAL_SCORE).add(score)) .addUpdate(N(TOTAL_GAMEPLAY).add(gameLength)) .addUpdate(N(TOTAL_GAMES).add(1)) .buildForUpdate(); viewTable.updateItem(PLAYER_NAME, playerName, incrementTotalOrder); }
@Override public void transform(Item scoreItem, DynamoDB dynamodb) { String playerName = scoreItem.getString(PLAYER_NAME); int score = scoreItem.getInt(SCORE); String date = scoreItem.getString(DATE); Table table = dynamodb.getTable(HIGH_SCORES_BY_DATE_TABLE_NAME); // Use conditional write to update max score UpdateItemExpressionSpec updateMax = new ExpressionSpecBuilder() .withCondition(N(MAX_SCORE).lt(score) .or(attribute_not_exists(MAX_SCORE))) .addUpdate(N(MAX_SCORE).set(score)) .buildForUpdate(); try { table.updateItem(PLAYER_NAME, playerName, DATE, date, updateMax); } catch (ConditionalCheckFailedException ccfe) {} }
@Override public Object handleRequest(DynamodbEvent input, Context context) { context.getLogger().log("Input: " + input); DynamoDB dynamodb = new DynamoDB(Regions.US_WEST_2); for (DynamodbStreamRecord record : input.getRecords()) { Map<String, AttributeValue> newData = record.getDynamodb().getNewImage(); if (newData == null) continue; // ignore deletes Item item = Item.fromMap(InternalCalls.toSimpleMapValue(newData)); DataTransformer.PLAYER_STATS_TRANSFORMER.transform(item, dynamodb); } return true; }
/** * 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; }
public static void main(String[] args) { AmazonDynamoDBClient client = new AmazonDynamoDBClient(); client.setEndpoint("http://localhost:8000"); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Movies"); // Conditional delete (will fail) DeleteItemSpec deleteItemSpec = new DeleteItemSpec() .withPrimaryKey(new PrimaryKey("year", 2015, "title", "The Big New Movie")) .withConditionExpression("info.rating <= :val") .withValueMap(new ValueMap() .withNumber(":val", 5.0)); System.out.println("Attempting a conditional delete..."); try { table.deleteItem(deleteItemSpec); System.out.println("DeleteItem succeeded"); } catch (Exception e) { e.printStackTrace(); System.out.println("DeleteItem failed"); } }
public static void main(String[] args) { AmazonDynamoDBClient client = new AmazonDynamoDBClient(); client.setEndpoint("http://localhost:8000"); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Movies"); ScanSpec scanSpec = new ScanSpec() .withProjectionExpression("#yr, title, info.rating") .withFilterExpression("#yr between :start_yr and :end_yr") .withNameMap(new NameMap().with("#yr", "year")) .withValueMap(new ValueMap().withNumber(":start_yr", 1950).withNumber(":end_yr", 1959)); ItemCollection<ScanOutcome> items = table.scan(scanSpec); Iterator<Item> iter = items.iterator(); while (iter.hasNext()) { Item item = iter.next(); System.out.println(item.toString()); } }
public static void main(String[] args) { AmazonDynamoDBClient client = new AmazonDynamoDBClient(); client.setEndpoint("http://localhost:8000"); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("Movies"); int year = 2015; String title = "The Big New Movie"; try { table.putItem(new Item() .withPrimaryKey("year", year, "title", title) .withJSON("info", "{\"plot\" : \"Something happens.\"}")); System.out.println("PutItem succeeded: " + table.getItem("year", year, "title", title).toJSONPretty()); } catch (Exception e) { System.out.println("PutItem failed"); e.printStackTrace(); } }
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); } }
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); }
public static final Table getTable() { if (null != table) { return table; } table = new DynamoDB(getClient()).getTable(getTableName()); System.out.println("Got DynamoDB table..."); return table; }
@Inject public DefaultFeatureStorage( AmazonDynamoDB amazonDynamoDB, TableConfiguration tableConfiguration, @Named("dynamodbFeatureWriteHystrix") HystrixConfiguration hystrixWriteConfiguration, @Named("dynamodbFeatureReadHystrix") HystrixConfiguration hystrixReadConfiguration, MetricRegistry metrics ) { this.dynamoDB = new DynamoDB(amazonDynamoDB); this.featureTableName = tableConfiguration.outlandFeaturesTable; this.hystrixWriteConfiguration = hystrixWriteConfiguration; this.hystrixReadConfiguration = hystrixReadConfiguration; this.metrics = metrics; }
@Override public Void handleRequest(S3Event s3Event, Context context){ Collection<Partition>requiredPartitions = new HashSet<>(); TableService tableService = new TableService(); DynamoDB dynamoDBClient=new DynamoDB(new AmazonDynamoDBClient(new EnvironmentVariableCredentialsProvider())); for(S3EventNotification.S3EventNotificationRecord record:s3Event.getRecords()){ String bucket=record.getS3().getBucket().getName(); String key=record.getS3().getObject().getKey(); System.out.printf("S3event[Event:%s,Bucket:%s,Key:%s]%n",record.getEventName(),bucket,key); S3Object s3Object=new S3Object(bucket,key); if(s3Object.hasDateTimeKey()){ Partition partition = partitionConfig.createPartitionFor(s3Object); //Check if the partition exists in DynamoDBtable, if not add the partition details to the table, skip otherwise if (tryAddMissingPartition(partitionConfig.dynamoDBTableName(), dynamoDBClient, partition)) { requiredPartitions.add(partition); } } } if(!requiredPartitions.isEmpty()){ tableService.addPartitions(partitionConfig.tableName(),requiredPartitions, true); } return null; }
public DynamoDBAnnouncementWatcher(AWSCredentials credentials, String tableName, String blobNamespace) { this.dynamoDB = new DynamoDB(new AmazonDynamoDBClient(credentials)); this.tableName = tableName; this.blobNamespace = blobNamespace; this.subscribedConsumers = Collections.synchronizedList(new ArrayList<HollowConsumer>()); this.latestVersion = readLatestVersion(); setupPollingThread(); }
/** * generate dynamo db mapper etc. to connect to database */ private DBConnector() { AWSPropertiesProvider awsPropertiesProvider = new AWSPropertiesProvider("dynamoDb.properties"); AmazonDynamoDBClient dbClient = new AmazonDynamoDBClient(awsPropertiesProvider.getCredentials()); dbClient.setRegion(awsPropertiesProvider.getRegion()); dynamoDB = new DynamoDB(dbClient); dynamoDBMapper = new DynamoDBMapper(dbClient); }
boolean updateItem(String key, long currentTimeMillis, int expiredIntervalMillis, Context context) { AmazonDynamoDB client = createDynamoDBClient(cc); String functionName = context.getFunctionName(); try { long sec = currentTimeMillis - expiredIntervalMillis; DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable(TABLE_NAME); Map<String, String> expressionAttributeNames = new HashMap<>(); expressionAttributeNames.put("#created_time", COL_CREATED_TIME); Map<String, Object> expressionAttributeValues = new HashMap<>(); expressionAttributeValues.put(":now", currentTimeMillis); expressionAttributeValues.put(":expired", sec); table.updateItem(new PrimaryKey(COL_FUNCTION_NAME, functionName, COL_KEY, key), "set #created_time = :now", // UpdateExpression "#created_time < :expired", // ConditionExpression expressionAttributeNames, expressionAttributeValues); return true; } catch (ConditionalCheckFailedException e) { return false; } finally { client.shutdown(); } }
@Override public DynamoDB getDB() { try { return new DynamoDB(getClient()); } catch (SdkClientException e) { throw new IllegalStateException(e); } }
@Test(expected = IllegalStateException.class) public void getEnvironmentDynamoDBProviderGetDBThrowsIllegalStateExWhenDefaultRegionEnvNotProvidedTest() { environmentVariables.set("AWS_ACCESS_KEY", "asd"); environmentVariables.set("AWS_SECRET_ACCESS_KEY", "asd"); DynamoDB actual = DynamoDBProvider.getEnvironmentDynamoDBProvider().getDB(); assertEquals("ThisIsTable", actual); }
@Test(expected = IllegalArgumentException.class) public void getEnvironmentDynamoDBProviderGetDBThrowsIllegalArgumentExWhenRegionEnvProvidedButNotRealRegionTest() { environmentVariables.set("AWS_ACCESS_KEY", "asd"); environmentVariables.set("AWS_SECRET_ACCESS_KEY", "asd"); environmentVariables.set("AWS_REGION", "blah"); DynamoDB actual = DynamoDBProvider.getEnvironmentDynamoDBProvider().getDB(); assertEquals("ThisIsTable", actual); }
@Test public void getEnvironmentDynamoDBProviderGetDBRegionDoesNotThrowExceptionTest() { environmentVariables.set("AWS_ACCESS_KEY", "asd"); environmentVariables.set("AWS_SECRET_ACCESS_KEY", "asd"); environmentVariables.set("AWS_REGION", Regions.EU_WEST_1.toString()); DynamoDB db = DynamoDBProvider.getEnvironmentDynamoDBProvider().getDB(); }
public Item getConfigItem() { Regions currentRegion = getDeploymentRegion(); AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient().withRegion(currentRegion); String configTableName = getConfigTableName(context.getFunctionName()); Table configTable = new DynamoDB(dynamoDBClient).getTable(configTableName); String environment = getFunctionNameEnvironmentSuffix().orElse(DEFAULT_ENVIRONMENT); System.out.println("Looking up config table : " + configTableName + " for config by key: "+environment); return configTable.getItem(CONFIG_TABLE_KEY_NAME, environment); }
public static void cleanupTables(AmazonDynamoDBClient client) { StreamSupport.stream(new DynamoDB(client).listTables() .pages().spliterator(), false) .flatMap(page -> StreamSupport.stream(page.spliterator(), false)) .map(table -> table.getTableName()) .parallel().peek(name -> LOG.info("Deleting table: " + name)) .forEach(name -> client.deleteTable(name)); }
private void ensureModel() { if (this.model == null) { this.client = new AmazonDynamoDBAsyncClient(config.inflateCredentials()); config.getEndpoint().configure(client); this.model = new DynamoDB(client); } }
public static final Table getTable() { if (null != table) { return table; } table = new DynamoDB(getClient()).getTable("Books"); System.out.println("Got DynamoDB table..."); return table; }
@PostConstruct public void connect(){ BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey); AmazonDynamoDB client = AmazonDynamoDBAsyncClientBuilder .standard() .withRegion(region) .withCredentials(new AWSStaticCredentialsProvider(basicAWSCredentials)) .build(); dynamoDB = new DynamoDB(client); mapper = new DynamoDBMapper(client); }
@Override protected void configure() { bindFactory(AmazonCloudSearchFactory.class).to(AmazonCloudSearch.class).in(Singleton.class); bindFactory(AmazonDynamoDBFactory.class).to(AmazonDynamoDB.class).in(Singleton.class); bindFactory(AmazonS3ClientFactory.class).to(AmazonS3Client.class).in(Singleton.class); bindFactory(AWSCredentialsProviderFactory.class).to(AWSCredentialsProvider.class).in(Singleton.class); bindFactory(CurrentUserFactory.class).to(User.class).in(RequestScoped.class); bindFactory(DynamoDBConfigurationFactory.class).to(ClientConfiguration.class).in(Singleton.class); bindFactory(DynamoDBFactory.class).to(DynamoDB.class).in(Singleton.class); bindFactory(DynamoDBMapperFactory.class).to(DynamoDBMapper.class).in(Singleton.class); bindFactory(EnvFactory.class).to(Env.class).in(Singleton.class); bindFactory(ExecutorServiceFactory.class).to(ExecutorService.class).in(Singleton.class); bindFactory(ObjectMapperFactory.class).to(ObjectMapper.class).in(Singleton.class); bindFactory(WebRequestorFactory.class).to(WebRequestor.class).in(Singleton.class); }
@Test public void testProvide() { AmazonDynamoDB amazonDynamoDB = mock(AmazonDynamoDB.class); DynamoDBFactory factory = new DynamoDBFactory(amazonDynamoDB); DynamoDB dynamoDB = factory.provide(); assertNotNull(dynamoDB); factory.dispose(dynamoDB); }
public DynamoDbLocker( DynamoDB configuration, String tableName, Clock clock ) { this.dynamoDb = configuration; this.tableName = tableName; this.clock = clock; }
@Test public void waiterMethodsShouldWorkWithWrapper() throws InterruptedException { DynamoDBClientWithStubbedWaiter subject = new DynamoDBClientWithStubbedWaiter( dynamoDbRule.getClient()); DynamoDB db = new DynamoDB(subject); AttributeDefinition id = new AttributeDefinition("id", ScalarAttributeType.S); List<KeySchemaElement> keySchema = new ArrayList<>(); keySchema.add( new KeySchemaElement() .withAttributeName(id.getAttributeName()) .withKeyType(KeyType.HASH) ); ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput(); provisionedThroughput.setReadCapacityUnits(10L); provisionedThroughput.setWriteCapacityUnits(10L); CreateTableRequest request = new CreateTableRequest() .withTableName("test_table") .withKeySchema(keySchema) .withAttributeDefinitions(singleton(id)) .withProvisionedThroughput(provisionedThroughput); Table result = db.createTable(request); TableDescription description = result.waitForActive(); assertThat(description.getTableName(), is("test_table")); }
public static void dynamoInsertHl7Json(String hl7Json, String mirthTable, String mirthId, String mirthDate) { String firstName = "NONE"; String lastName = "NONE"; String dob = "NONE"; String docType = "hl7"; String messageType = "NONE"; AmazonDynamoDBClient client = new AmazonDynamoDBClient(); client.withRegion(Regions.US_WEST_2); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable(mirthTable); try { JSONObject obj = new JSONObject(hl7Json); firstName = obj.getJSONObject("HL7Message").getJSONObject("PID").getJSONObject("PID.5").getString("PID.5.2"); lastName = obj.getJSONObject("HL7Message").getJSONObject("PID").getJSONObject("PID.5").getString("PID.5.1"); dob = obj.getJSONObject("HL7Message").getJSONObject("PID").getJSONObject("PID.7").getString("PID.7.1"); messageType = obj.getJSONObject("HL7Message").getJSONObject("MSH").getJSONObject("MSH.9").getString("MSH.9.1"); } catch (org.json.JSONException e) { System.out.println("HL7 JSON ERROR"); } //replace empyty string with value representing blank hl7Json = hl7Json.replaceAll("\"\"","\"NONE\""); Item item = new Item() .withPrimaryKey("mirthid", mirthId) .withString("mirthdate", mirthDate) .withString("type", docType) .withString("FirstName", firstName) .withString("LastName", lastName) .withString("DOB", dob) .withString("HL7Type", messageType) .withString("Processed", "N") .withJSON("document", hl7Json); table.putItem(item); }
public static void dynamoInsertJson(String ccdJson, String mirthTable, String mirthId, String mirthDate) { System.out.println( "Performing insert into DynamoDB" ); String firstName = "NONE"; String lastName = "NONE"; String dob = "NONE"; String docType = "ccda"; AmazonDynamoDBClient client = new AmazonDynamoDBClient(); client.withRegion(Regions.US_WEST_2); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable(mirthTable); //System.out.println(ccdJson); try { JSONObject obj = new JSONObject(ccdJson); firstName = obj.getJSONObject("data").getJSONObject("demographics").getJSONObject("name").getString("first"); lastName = obj.getJSONObject("data").getJSONObject("demographics").getJSONObject("name").getString("last"); dob = obj.getJSONObject("data").getJSONObject("demographics").getJSONObject("dob").getJSONObject("point").getString("date"); //System.out.println(firstName); } catch (org.json.JSONException e) { System.out.println("JSON ERROR"); } ccdJson = ccdJson.replaceAll("\"\"","\"NONE\""); Item item = new Item() .withPrimaryKey("mirthid", mirthId) .withString("mirthdate", mirthDate) .withString("type", docType) .withString("FirstName", firstName) .withString("LastName", lastName) .withString("DOB", dob) .withString("Processed", "N") .withJSON("document", ccdJson); table.putItem(item); }