public void start() { int mb = 1024 * 1024; LOG.info("Max memory: {} mb", Runtime.getRuntime().maxMemory() / mb); LOG.info("Starting up Kinesis Consumer... (may take a few seconds)"); AmazonKinesisClient kinesisClient = new AmazonKinesisClient(kinesisCfg.getKinesisCredentialsProvider(), kinesisCfg.getKinesisClientConfiguration()); AmazonDynamoDBClient dynamoDBClient = new AmazonDynamoDBClient(kinesisCfg.getDynamoDBCredentialsProvider(), kinesisCfg.getDynamoDBClientConfiguration()); AmazonCloudWatch cloudWatchClient = new AmazonCloudWatchClient(kinesisCfg.getCloudWatchCredentialsProvider(), kinesisCfg.getCloudWatchClientConfiguration()); Worker worker = new Worker.Builder() .recordProcessorFactory(() -> new RecordProcessor(unitOfWorkListener, exceptionStrategy, metricsCallback, dry)) .config(kinesisCfg) .kinesisClient(kinesisClient) .dynamoDBClient(dynamoDBClient) .cloudWatchClient(cloudWatchClient) .build(); worker.run(); }
@Override public PropertySource<?> locate(final Environment environment) { final AmazonDynamoDBClient amazonDynamoDBClient = getAmazonDynamoDbClient(environment); createSettingsTable(amazonDynamoDBClient, false); final ScanRequest scan = new ScanRequest(TABLE_NAME); LOGGER.debug("Scanning table with request [{}]", scan); final ScanResult result = amazonDynamoDBClient.scan(scan); LOGGER.debug("Scanned table with result [{}]", scan); final Properties props = new Properties(); result.getItems() .stream() .map(DynamoDbCloudConfigBootstrapConfiguration::retrieveSetting) .forEach(p -> props.put(p.getKey(), p.getValue())); return new PropertiesPropertySource(getClass().getSimpleName(), props); }
@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); }
@Override public void configure(String name, Properties props, NameSpace ns) throws LDAPException { this.name = name; this.accessKey = props.getProperty("accessKey"); this.secretKey = props.getProperty("secretKey"); this.userTable = props.getProperty("userTable"); this.groupTable = props.getProperty("groupTable"); this.userDN = new DN("ou=users," + ns.getBase().getDN().toString()); this.groupDN = new DN("ou=groups," + ns.getBase().getDN().toString()); this.baseDN = new DN(ns.getBase().getDN().toString()); this.db = new AmazonDynamoDBClient(new BasicAWSCredentials(accessKey,secretKey)); }
public KafkaDynamoStreamAdapter(String regionName, String srcTable, IRecordProcessorFactory processorFactory) { sourceTable = srcTable; credentialsProvider = new DefaultAWSCredentialsProviderChain(); recordProcessorFactory = processorFactory; adapterClient = new AmazonDynamoDBStreamsAdapterClient(credentialsProvider, new ClientConfiguration()); dynamoDBClient = new AmazonDynamoDBClient(credentialsProvider, new ClientConfiguration()); cloudWatchClient = new AmazonCloudWatchClient(credentialsProvider, new ClientConfiguration()); if ("local".equalsIgnoreCase(regionName)) { setClientEndpoints(localddbEndpoint); } else if (regionName != null) { Region region = Region.getRegion(Regions.fromName(regionName)); adapterClient.setRegion(region); dynamoDBClient.setRegion(region); cloudWatchClient.setRegion(region); } }
private String enableStreamForTable(AmazonDynamoDBClient client, StreamViewType viewType, String tableName) { DescribeTableRequest describeTableRequest = new DescribeTableRequest() .withTableName(tableName); DescribeTableResult describeResult = client.describeTable(describeTableRequest); if (describeResult.getTable().getStreamSpecification().isStreamEnabled()) { //TODO: what if the viewtype doesn't match return describeResult.getTable().getLatestStreamId(); } StreamSpecification streamSpecification = new StreamSpecification(); streamSpecification.setStreamEnabled(true); streamSpecification.setStreamViewType(viewType); UpdateTableRequest updateTableRequest = new UpdateTableRequest() .withTableName(tableName) .withStreamSpecification(streamSpecification); UpdateTableResult result = client.updateTable(updateTableRequest); return result.getTableDescription().getLatestStreamId(); }
public static String enableStreamForTable(AmazonDynamoDBClient client, StreamViewType viewType, String tableName) { DescribeTableRequest describeTableRequest = new DescribeTableRequest() .withTableName(tableName); DescribeTableResult describeResult = client.describeTable(describeTableRequest); if (describeResult.getTable().getStreamSpecification().isStreamEnabled()) { //TODO: what if the viewtype doesn't match return describeResult.getTable().getLatestStreamId(); } StreamSpecification streamSpecification = new StreamSpecification(); streamSpecification.setStreamEnabled(true); streamSpecification.setStreamViewType(viewType); UpdateTableRequest updateTableRequest = new UpdateTableRequest() .withTableName(tableName) .withStreamSpecification(streamSpecification); UpdateTableResult result = client.updateTable(updateTableRequest); return result.getTableDescription().getLatestStreamId(); }
public static void updateItem(AmazonDynamoDBClient client, String tableName, String id, String val) { java.util.Map<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put("Id", new AttributeValue().withN(id)); Map<String, AttributeValueUpdate> attributeUpdates = new HashMap<String, AttributeValueUpdate>(); AttributeValueUpdate update = new AttributeValueUpdate() .withAction(AttributeAction.PUT) .withValue(new AttributeValue().withS(val)); attributeUpdates.put("attribute-2", update); UpdateItemRequest updateItemRequest = new UpdateItemRequest() .withTableName(tableName) .withKey(key) .withAttributeUpdates(attributeUpdates); client.updateItem(updateItemRequest); }
public static void awaitTableCreation(AmazonDynamoDBClient dynamoDBClient, String tableName) { Integer retries = 0; Boolean created = false; while(!created && retries < 100) { DescribeTableResult result = StreamAdapterDemoHelper.describeTable(dynamoDBClient, tableName); created = result.getTable().getTableStatus().equals("ACTIVE"); if (created) { System.out.println("Table is active."); return; } else { retries++; try { Thread.sleep(1000); } catch(InterruptedException e) { // do nothing } } } System.out.println("Timeout after table creation. Exiting..."); cleanupAndExit(dynamoDBClient, tableName, 1); }
/** * Creates the DynamoDBBootstrapWorker, calculates the number of segments a * table should have, and creates a thread pool to prepare to scan. * * @throws Exception */ public DynamoDBBootstrapWorker(AmazonDynamoDBClient client, double rateLimit, String tableName, ExecutorService exec, int section, int totalSections, int numSegments, boolean consistentScan) throws SectionOutOfRangeException { if (section > totalSections - 1 || section < 0) { throw new SectionOutOfRangeException( "Section of scan must be within [0...totalSections-1]"); } this.client = client; this.rateLimit = rateLimit; this.tableName = tableName; this.numSegments = numSegments; this.section = section; this.totalSections = totalSections; this.consistentScan = consistentScan; super.threadPool = exec; }
/** * Creates the DynamoDBBootstrapWorker, calculates the number of segments a * table should have, and creates a thread pool to prepare to scan using an * eventually consistent scan. * * @throws Exception */ public DynamoDBBootstrapWorker(AmazonDynamoDBClient client, double rateLimit, String tableName, int numThreads) throws NullReadCapacityException { this.client = client; this.rateLimit = rateLimit; this.tableName = tableName; TableDescription description = client.describeTable(tableName) .getTable(); this.section = 0; this.totalSections = 1; this.consistentScan = false; this.numSegments = getNumberOfSegments(description); int numProcessors = Runtime.getRuntime().availableProcessors() * 4; if (numProcessors > numThreads) { numThreads = numProcessors; } super.threadPool = Executors.newFixedThreadPool(numThreads); }
/** * Collect data for DynamoDB. * * @param stats * current statistics object. * @param account * currently used credentials object. * @param region * currently used aws region. */ public static void scanDynamoDB(AwsStats stats, AwsAccount account, Regions region) { LOG.debug("Scan for DynamoDB in region " + region.getName() + " in account " + account.getAccountId()); /* * Amazon DynamoDB */ try { AmazonDynamoDB dynamoDB = new AmazonDynamoDBClient(account.getCredentials()); dynamoDB.setRegion(Region.getRegion(region)); List<String> list = dynamoDB.listTables().getTableNames(); int totalItems = list.size(); for (String tableName : list) { AwsResource res = new AwsResource(tableName, account.getAccountId(), AwsResourceType.DynamoDB, region); stats.add(res); } LOG.info(totalItems + " DynamoDB tables in region " + region.getName() + " in account " + account.getAccountId()); } catch (AmazonServiceException ase) { LOG.error("Exception of DynamoDB: " + ase.getMessage()); } }
public TableReader(Options options, AmazonDynamoDBClient dynamoDBClient, TableHelper tableHelper, boolean isRunningOnDDBLocal) throws IOException, IllegalArgumentException { TableReader.options = options; TableReader.dynamoDBClient = dynamoDBClient; TableReader.tableHelper = tableHelper; attributesToGet = tableHelper.getListOfAttributesToFetch(options.getGsiHashKeyName(), options.getGsiRangeKeyName()); itemsScanned = new AtomicLong(0); itemsScanLimit = options.getNumOfRecords(); violationsFound = new AtomicLong(0); violationsFindLimit = options.getNumOfViolations(); violationsDeleted = new AtomicLong(0); if (options.recordDetails()) { createViolationWriter(); } TableReader.isRunningOnDDBLocal = isRunningOnDDBLocal; }
public void violationDetection(boolean delete) { try { AmazonDynamoDBClient dynamoDBClient = awsConnection.getDynamoDBClient(options.getDynamoDBRegion(), runOnDDBLocal); tableHelper = new TableHelper(dynamoDBClient, options.getTableName()); tableReader = new TableReader(options, dynamoDBClient, tableHelper, runOnDDBLocal); validateKeyNames(); tableReader.scanTable(delete); } catch (Exception e) { logger.error("Exception!", e); e.printStackTrace(); System.exit(1); } if (options.isDetectionOutputS3Path()) { putOutputFileToS3(options.getDetectionOutputPath(), options.getTmpDetectionOutputPath()); } }
public void violationCorrection(boolean delete, boolean useConditionalUpdate) { /** Get file from S3 to temporary correction file before processing */ if (options.isCorrectionInputS3Path()) { downloadInputFileToLocal(); } try { AmazonDynamoDBClient dynamoDBClient = awsConnection.getDynamoDBClient(options.getDynamoDBRegion(), runOnDDBLocal); tableHelper = new TableHelper(dynamoDBClient, options.getTableName()); validateKeyNames(); correction = new Correction(options, tableHelper, dynamoDBClient, runOnDDBLocal); if (delete) { correction.deleteFromFile(); } else { boolean violationOutputGenerated = correction.updateFromFile(useConditionalUpdate); if(violationOutputGenerated && options.isCorrectionOutputS3Path()) { putOutputFileToS3(options.getCorrectionOutputPath(), options.getTmpCorrectionOutputPath()); } } } catch (Exception e) { logger.error("Exception!", e); e.printStackTrace(); System.exit(1); } }
@Inject KinesisClientManager(KinesisConnectorConfig kinesisConnectorConfig) { log.info("Creating new client for Consumer"); if (nonEmpty(kinesisConnectorConfig.getAccessKey()) && nonEmpty(kinesisConnectorConfig.getSecretKey())) { this.kinesisAwsCredentials = new KinesisAwsCredentials(kinesisConnectorConfig.getAccessKey(), kinesisConnectorConfig.getSecretKey()); this.client = new AmazonKinesisClient(this.kinesisAwsCredentials); this.amazonS3Client = new AmazonS3Client(this.kinesisAwsCredentials); this.dynamoDBClient = new AmazonDynamoDBClient(this.kinesisAwsCredentials); } else { this.kinesisAwsCredentials = null; DefaultAWSCredentialsProviderChain defaultChain = new DefaultAWSCredentialsProviderChain(); this.client = new AmazonKinesisClient(defaultChain); this.amazonS3Client = new AmazonS3Client(defaultChain); this.dynamoDBClient = new AmazonDynamoDBClient(defaultChain); } this.client.setEndpoint("kinesis." + kinesisConnectorConfig.getAwsRegion() + ".amazonaws.com"); this.dynamoDBClient.setEndpoint("dynamodb." + kinesisConnectorConfig.getAwsRegion() + ".amazonaws.com"); }
private AmazonDynamoDBClient getDynamoClient(String tableName) { AWSCredentialsProvider credentials = new AWSCredentialsProviderChain(new StaticCredentialsProvider(new BasicAWSCredentials("test", "test"))); AmazonDynamoDBClient amazonDynamoDBClient = new AmazonDynamoDBClient(credentials); amazonDynamoDBClient.setEndpoint("http://localhost:8000"); ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits(5L) .withWriteCapacityUnits(10L); CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName).withProvisionedThroughput(provisionedThroughput); ArrayList<AttributeDefinition> attributeDefinitions= new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition().withAttributeName("id").withAttributeType("S")); createTableRequest.setAttributeDefinitions(attributeDefinitions); ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>(); tableKeySchema.add(new KeySchemaElement().withAttributeName("id").withKeyType(KeyType.HASH)); createTableRequest.setKeySchema(tableKeySchema); amazonDynamoDBClient.createTable(createTableRequest); return amazonDynamoDBClient; }
public static ContentRepository createRepository(boolean reset) { if (isDynamoDBStore()) { AmazonDynamoDBClient dynamodb = new AmazonDynamoDBClient(new DefaultAWSCredentialsProviderChain()); dynamodb.setEndpoint("http://localhost:8000"); performReset(reset, dynamodb); NodeStore store = new DocumentMK.Builder() .setDynamoDB(dynamodb) .open().getNodeStore(); return new Oak(store) .with(new InitialContent()) .with(new OpenSecurityProvider()) .createContentRepository(); } return null; }
/*** * * All input parameters are required * * @param topic * @param config * @param client * @param dynamoClient * @param cloudwatchClient */ public KinesisConsumer(final String topic, final KinesisClientLibConfiguration config, final AmazonKinesisClient client, final AmazonDynamoDBClient dynamoClient, final AmazonCloudWatchClient cloudwatchClient, final ObjectMapper mapper){ this.topic = Preconditions.checkNotNull(topic, "A valid kinesis topic is required"); this.config = Preconditions.checkNotNull(config, "KinesisClientLibConfiguration is required"); this.client = Preconditions.checkNotNull(client, "AmazonKinesisClient is required"); this.dynamoClient = Preconditions.checkNotNull(dynamoClient, "AmazonDynamoDBClient is required"); this.cloudwatchClient = Preconditions.checkNotNull(cloudwatchClient, "AmazonCloudWatchClient is required"); this.mapper = Preconditions.checkNotNull(mapper, "ObjectMapper is required"); }
@Override public void run(AppConfiguration configuration, Environment environment)throws Exception { if(configuration!=null && configuration.getAws()!=null && configuration.getAws().getDynamo() !=null){ final AmazonDynamoDBClient client = new AmazonDynamoDBClient(); try{ LOGGER.info("Executing configured dynamodb bundle"); final DynamoAdmin admin = new DynamoAdmin(client); final DynamoDbConfig dynamodbConfig = configuration.getAws().getDynamo(); admin.scan(dynamodbConfig.getPackagePrefix()); LOGGER.info("Completed dynamodb bundle execution"); }catch(Exception ex){ LOGGER.error(ex.getMessage(), ex); }finally{ client.shutdown(); } } }
public DynamoDBService2(Tenant tenant) { super(tenant); String accessKey = getParamString("ddb-access-key"); String secretKey = getParamString("ddb-secret-key"); String endpoint = getParamString("ddb-endpoint"); m_readCapacityUnits = getParamInt("ddb-read-capacity-units", 1); m_writeCapacityUnits = getParamInt("ddb-write-capacity-units", 1); BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey); m_client = new AmazonDynamoDBClient(awsCredentials); m_client.setEndpoint(endpoint); // try to connect to check the connection m_client.listTables(); m_logger.info("Started DynamoDB service. Endpoint: {}, read/write capacity units for new namespaces: {}/{}", new Object[] {endpoint, m_readCapacityUnits, m_writeCapacityUnits}); }
/** * Constructs a {@link ImageIngester} with the specified command line arguments and Amazon Web Services credentials * provider. * * @param args * Command line arguments for retrieving the configuration * @param credentialsProvider * Amazon Web Services credentials provider * @throws ExitException * Error parsing configuration */ public ImageIngester(final String[] args, final AWSCredentialsProvider credentialsProvider) throws ExitException { // Parse command line arguments to locate configuration file final ImageIngesterCLI cli = new ImageIngesterCLI(args); config = cli.getConfig(); // Validate the configuration file ConfigParser.validateConfig(config, REQUIRED_STRING_CONFIGURATIONS, REQUIRED_BOOLEAN_CONFIGURATIONS, REQUIRED_INTEGER_CONFIGURATIONS, REQUIRED_LONG_CONFIGURATIONS); // Parse configuration settings resourceTable = ConfigParser.parseString(config, CONFIG_RESOURCE_TABLE); imageTable = ConfigParser.parseString(config, CONFIG_IMAGE_TABLE); waitTime = ConfigParser.parseLong(config, CONFIG_WAIT_TIME, DEFAULT_WAIT_TIME); connectTimeout = ConfigParser.parseInteger(config, CONFIG_CONNECT_TIMEOUT, DEFAULT_CONNECT_TIMEOUT); final String endpoint = ConfigParser.parseString(config, CONFIG_ENDPOINT); final int numManifestThreads = ConfigParser.parseInteger(config, CONFIG_NUM_MANIFEST_THREADS, DEFAULT_THREADS); // Setup state dynamoDB = new AmazonDynamoDBClient(credentialsProvider); dynamoDB.setEndpoint(endpoint); manifestPool = Executors.newFixedThreadPool(numManifestThreads); }
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 Boolean AddRegistration (Registration r) { DynamoServiceFactory dsf = new DynamoServiceFactory(); AmazonDynamoDBClient client = dsf.createDynamoClient(); long RegistrationDate = System.currentTimeMillis(); Map<String, AttributeValue> item = new HashMap<String, AttributeValue>(); item.put(Registration.Fields.DonorUUID, new AttributeValue().withS(r.getDonorUUID())); item.put(Registration.Fields.OpportunityId, new AttributeValue().withS(r.getOpportunityId())); item.put(Registration.Fields.Category, new AttributeValue().withS(r.getCategory())); item.put(Registration.Fields.Timestamp, new AttributeValue().withN(String.valueOf(RegistrationDate))); PutItemRequest putItemRequest = new PutItemRequest() .withTableName(RegistrationTableName) .withItem(item); PutItemResult result = client.putItem(putItemRequest); return true; }
public static void createSessionTable(AmazonDynamoDBClient dynamo, String tableName, long readCapacityUnits, long writeCapacityUnits) { CreateTableRequest request = new CreateTableRequest().withTableName(tableName); request.withKeySchema(new KeySchemaElement().withAttributeName(DynamoSessionItem.SESSION_ID_ATTRIBUTE_NAME) .withKeyType(KeyType.HASH)); request.withAttributeDefinitions( new AttributeDefinition().withAttributeName(DynamoSessionItem.SESSION_ID_ATTRIBUTE_NAME) .withAttributeType(ScalarAttributeType.S)); request.setProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(readCapacityUnits) .withWriteCapacityUnits(writeCapacityUnits)); dynamo.createTable(request); }
/** Starts up an embedded Tomcat process for testing. */ @BeforeClass public static void setupFixture() throws Exception { setUpCredentials(); dynamo = new AmazonDynamoDBClient(credentials); String workingDir = System.getProperty("java.io.tmpdir"); File webappDirectory = Files.createTempDirectory(Paths.get(workingDir), null).toFile(); webappDirectory.deleteOnExit(); tomcat = new Tomcat(); tomcat.setPort(0); tomcat.setBaseDir(workingDir); tomcat.getHost().setAppBase(workingDir); tomcat.getHost().setAutoDeploy(true); tomcat.getHost().setDeployOnStartup(true); webapp = tomcat.addWebapp("/", webappDirectory.getAbsolutePath()); tomcat.start(); }
/** * Creates a {@link Bean}. * * @param <T> * The type of the repository. * @param repositoryType * The class representing the repository. * @param beanManager * The BeanManager instance. * @return The bean. */ private <T> Bean<T> createRepositoryBean(Class<T> repositoryType, Set<Annotation> qualifiers, BeanManager beanManager) { // Determine the amazondbclient bean which matches the qualifiers of the // repository. Bean<AmazonDynamoDB> amazonDynamoDBBean = amazonDynamoDBs.get(qualifiers); // Determine the dynamo db mapper configbean which matches the // qualifiers of the repository. Bean<DynamoDBMapperConfig> dynamoDBMapperConfigBean = dbMapperConfigs.get(qualifiers); if (amazonDynamoDBBean == null) { throw new UnsatisfiedResolutionException(String.format("Unable to resolve a bean for '%s' with qualifiers %s.", AmazonDynamoDBClient.class.getName(), qualifiers)); } Bean<DynamoDBOperations> dynamoDBOperationsBean = dynamoDBOperationss.get(qualifiers); // Construct and return the repository bean. return new DynamoDBRepositoryBean<T>(beanManager, amazonDynamoDBBean, dynamoDBMapperConfigBean,dynamoDBOperationsBean,qualifiers, repositoryType); }
public static String getTestTableStatus() { try { AmazonDynamoDBClient ddb = UserPreferenceDemoActivity.clientManager .ddb(); DescribeTableRequest request = new DescribeTableRequest() .withTableName(Constants.TEST_TABLE_NAME); DescribeTableResult result = ddb.describeTable(request); String status = result.getTable().getTableStatus(); return status == null ? "" : status; } catch (ResourceNotFoundException e) { } catch (AmazonServiceException ex) { UserPreferenceDemoActivity.clientManager .wipeCredentialsOnAuthError(ex); } return ""; }
public static void insertUsers() { AmazonDynamoDBClient ddb = UserPreferenceDemoActivity.clientManager .ddb(); DynamoDBMapper mapper = new DynamoDBMapper(ddb); try { for (int i = 1; i <= 10; i++) { UserPreference userPreference = new UserPreference(); userPreference.setUserNo(i); userPreference.setFirstName(Constants.getRandomName()); userPreference.setLastName(Constants.getRandomName()); Log.d(TAG, "Inserting users"); mapper.save(userPreference); Log.d(TAG, "Users inserted"); } } catch (AmazonServiceException ex) { Log.e(TAG, "Error inserting users"); UserPreferenceDemoActivity.clientManager .wipeCredentialsOnAuthError(ex); } }
public static ArrayList<UserPreference> getUserList() { AmazonDynamoDBClient ddb = UserPreferenceDemoActivity.clientManager .ddb(); DynamoDBMapper mapper = new DynamoDBMapper(ddb); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression(); try { PaginatedScanList<UserPreference> result = mapper.scan( UserPreference.class, scanExpression); ArrayList<UserPreference> resultList = new ArrayList<UserPreference>(); for (UserPreference up : result) { resultList.add(up); } return resultList; } catch (AmazonServiceException ex) { UserPreferenceDemoActivity.clientManager .wipeCredentialsOnAuthError(ex); } return null; }
public static UserPreference getUserPreference(int userNo) { AmazonDynamoDBClient ddb = UserPreferenceDemoActivity.clientManager .ddb(); DynamoDBMapper mapper = new DynamoDBMapper(ddb); try { UserPreference userPreference = mapper.load(UserPreference.class, userNo); return userPreference; } catch (AmazonServiceException ex) { UserPreferenceDemoActivity.clientManager .wipeCredentialsOnAuthError(ex); } return null; }
/** * The only information needed to create a client are security credentials * consisting of the AWS Access Key ID and Secret Access Key. All other * configuration, such as the service endpoints, are performed * automatically. Client parameters, such as proxies, can be specified in an * optional ClientConfiguration object when constructing a client. * * @see com.amazonaws.auth.BasicAWSCredentials * @see com.amazonaws.auth.PropertiesCredentials * @see com.amazonaws.ClientConfiguration */ private PersistentStore(Region region, long readCapacity, long writeCapacity) throws Exception { /* * This credentials provider implementation loads your AWS credentials * from a properties file at the root of your classpath. */ dynamoDB = new AmazonDynamoDBClient( new ClasspathPropertiesFileCredentialsProvider()); dynamoDB.setRegion(region); try { if (!tablesExist()) { createTables(readCapacity, writeCapacity); } waitForTableToBecomeAvailable(TABLE_NAME); } catch (Exception e) { handleException(e); } }
/** * Creates tables. * @throws IOException if something goes wrong */ public void create() throws IOException { final AmazonDynamoDB aws = new AmazonDynamoDBClient( new BasicAWSCredentials(this.key, this.secret) ); aws.setEndpoint(String.format("%s:%d", this.endpoint, this.port)); for (final String table : this.locations) { final JsonObject json = this.readJson(table); if (json.containsKey("TableName")) { final String name = json.getString("TableName"); if (Tables.exists(aws, name)) { Logger.info( this, "Table '%s' already exists, skipping...", name ); } else { this.createTable(aws, json); } } else { throw new IOException( String.format( "File '%s' does not specify TableName attribute", table ) ); } } }
public DynamoDbTicketRegistryFacilitator(final TicketCatalog ticketCatalog, final DynamoDbTicketRegistryProperties dynamoDbProperties, final AmazonDynamoDBClient amazonDynamoDBClient) { this.ticketCatalog = ticketCatalog; this.dynamoDbProperties = dynamoDbProperties; this.amazonDynamoDBClient = amazonDynamoDBClient; createTicketTables(dynamoDbProperties.isDropTablesOnStartup()); }
private static void createSettingsTable(final AmazonDynamoDBClient amazonDynamoDBClient, final boolean deleteTables) { try { final CreateTableRequest request = new CreateTableRequest() .withAttributeDefinitions(new AttributeDefinition(ColumnNames.ID.getName(), ScalarAttributeType.S)) .withKeySchema(new KeySchemaElement(ColumnNames.ID.getName(), KeyType.HASH)) .withProvisionedThroughput(new ProvisionedThroughput(PROVISIONED_THROUGHPUT, PROVISIONED_THROUGHPUT)) .withTableName(TABLE_NAME); if (deleteTables) { final DeleteTableRequest delete = new DeleteTableRequest(request.getTableName()); LOGGER.debug("Sending delete request [{}] to remove table if necessary", delete); TableUtils.deleteTableIfExists(amazonDynamoDBClient, delete); } LOGGER.debug("Sending delete request [{}] to create table", request); TableUtils.createTableIfNotExists(amazonDynamoDBClient, request); LOGGER.debug("Waiting until table [{}] becomes active...", request.getTableName()); TableUtils.waitUntilActive(amazonDynamoDBClient, request.getTableName()); final DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(request.getTableName()); LOGGER.debug("Sending request [{}] to obtain table description...", describeTableRequest); final TableDescription tableDescription = amazonDynamoDBClient.describeTable(describeTableRequest).getTable(); LOGGER.debug("Located newly created table with description: [{}]", tableDescription); } catch (final Exception e) { throw Throwables.propagate(e); } }
@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; }