@Override public void doStart() throws Exception { super.doStart(); sdbClient = configuration.getAmazonSDBClient() != null ? configuration.getAmazonSDBClient() : createSdbClient(); if (ObjectHelper.isNotEmpty(configuration.getAmazonSdbEndpoint())) { sdbClient.setEndpoint(configuration.getAmazonSdbEndpoint()); } String domainName = getConfiguration().getDomainName(); LOG.trace("Querying whether domain [{}] already exists...", domainName); try { sdbClient.domainMetadata(new DomainMetadataRequest(domainName)); LOG.trace("Domain [{}] already exists", domainName); return; } catch (NoSuchDomainException ase) { LOG.trace("Domain [{}] doesn't exist yet", domainName); LOG.trace("Creating domain [{}]...", domainName); sdbClient.createDomain(new CreateDomainRequest(domainName)); LOG.trace("Domain [{}] created", domainName); } }
public void execute() { DomainMetadataRequest request = new DomainMetadataRequest() .withDomainName(determineDomainName()); log.trace("Sending request [{}] for exchange [{}]...", request, exchange); DomainMetadataResult result = this.sdbClient.domainMetadata(request); log.trace("Received result [{}]", result); Message msg = getMessageForResponse(exchange); msg.setHeader(SdbConstants.TIMESTAMP, result.getTimestamp()); msg.setHeader(SdbConstants.ITEM_COUNT, result.getItemCount()); msg.setHeader(SdbConstants.ATTRIBUTE_NAME_COUNT, result.getAttributeNameCount()); msg.setHeader(SdbConstants.ATTRIBUTE_VALUE_COUNT, result.getAttributeValueCount()); msg.setHeader(SdbConstants.ATTRIBUTE_NAME_SIZE, result.getAttributeNamesSizeBytes()); msg.setHeader(SdbConstants.ATTRIBUTE_VALUE_SIZE, result.getAttributeValuesSizeBytes()); msg.setHeader(SdbConstants.ITEM_NAME_SIZE, result.getItemNamesSizeBytes()); }
@Override public DomainMetadataResult domainMetadata(DomainMetadataRequest domainMetadataRequest) throws AmazonServiceException, AmazonClientException { this.domainMetadataRequest = domainMetadataRequest; if ("NonExistingDomain".equals(domainMetadataRequest.getDomainName())) { throw new NoSuchDomainException("Domain 'NonExistingDomain' doesn't exist."); } DomainMetadataResult result = new DomainMetadataResult(); result.setTimestamp(new Integer(10)); result.setItemCount(new Integer(11)); result.setAttributeNameCount(new Integer(12)); result.setAttributeValueCount(new Integer(13)); result.setAttributeNamesSizeBytes(new Long(1000000)); result.setAttributeValuesSizeBytes(new Long(2000000)); result.setItemNamesSizeBytes(new Long(3000000)); return result; }
public static void simpleDB_demo(){ System.out.println("- - - - - - - - - - - - - - - - - - - - - -"); System.out.println("SimpleDB DEMO"); /* * Amazon SimpleDB * * The AWS SimpleDB client allows you to query and manage your data * stored in SimpleDB domains (similar to tables in a relational DB). * * In this sample, we use a SimpleDB client to iterate over all the * domains owned by the current user, and add up the number of items * (similar to rows of data in a relational DB) in each domain. */ try { ListDomainsRequest sdbRequest = new ListDomainsRequest().withMaxNumberOfDomains(100); ListDomainsResult sdbResult = sdb.listDomains(sdbRequest); int totalItems = 0; for (String domainName : sdbResult.getDomainNames()) { DomainMetadataRequest metadataRequest = new DomainMetadataRequest().withDomainName(domainName); DomainMetadataResult domainMetadata = sdb.domainMetadata(metadataRequest); totalItems += domainMetadata.getItemCount(); } System.out.println("You have " + sdbResult.getDomainNames().size() + " Amazon SimpleDB domain(s)" + "containing a total of " + totalItems + " items."); } catch (AmazonServiceException ase) { System.out.println("Caught Exception: " + ase.getMessage()); System.out.println("Response Status Code: " + ase.getStatusCode()); System.out.println("Error Code: " + ase.getErrorCode()); System.out.println("Request ID: " + ase.getRequestId()); } }
/** * Collect data for SimpleDB. * * @param stats * current statistics object. * @param account * currently used credentials object. * @param region * currently used aws region. */ public static void scanSimpleDB(AwsStats stats, AwsAccount account, Regions region) { if (region == Regions.EU_CENTRAL_1) return; LOG.debug("Scan for SimpleDB in region " + region.getName() + " in account " + account.getAccountId()); /* * Amazon SimpleDB * * The AWS SimpleDB client allows you to query and manage your data * stored in SimpleDB domains (similar to tables in a relational DB). * * In this sample, we use a SimpleDB client to iterate over all the * domains owned by the current user, and add up the number of items * (similar to rows of data in a relational DB) in each domain. */ try { AmazonSimpleDB simpleDB = new AmazonSimpleDBClient(account.getCredentials()); simpleDB.setRegion(Region.getRegion(region)); ListDomainsRequest sdbRequest = new ListDomainsRequest().withMaxNumberOfDomains(100); ListDomainsResult sdbResult = simpleDB.listDomains(sdbRequest); int totalItems = 0; for (String domainName : sdbResult.getDomainNames()) { DomainMetadataRequest metadataRequest = new DomainMetadataRequest().withDomainName(domainName); DomainMetadataResult domainMetadata = simpleDB.domainMetadata(metadataRequest); int items = domainMetadata.getItemCount(); totalItems += items; AwsResource res = new AwsResource(domainName, account.getAccountId(), AwsResourceType.SimpleDB, region); res.addInfo(AwsTag.Items, items); stats.add(res); } LOG.info(sdbResult.getDomainNames().size() + " SimpleDB domains containing a total of " + totalItems + " items in region " + region.getName() + " in account " + account.getAccountId()); } catch (AmazonServiceException ase) { LOG.error("Exception of SimpleDB: " + ase.getMessage()); } catch (Exception ex) { LOG.error("Exception of SimpleDB: " + ex.getMessage()); } }
private void domainToCSV(String domainPrefix, File csvFile) { log.info("Storing data from SDB domains starting with " + domainPrefix + " to file " + csvFile); Set<String> attributes = null; List<String> domains = getDbClient().listDomains().getDomainNames(); int c = 0; for (String domainName : domains) { if (domainName.startsWith(domainPrefix)) { c++; log.info("Exporting from " + domainName + " (" + c + "/" + domains.size() + ")"); long domainCount = getDbClient().domainMetadata( new DomainMetadataRequest(domainName)).getItemCount(); if (domainCount < MIN_RESULTS) { log.info("Ignoring " + domainName + ", less than " + MIN_RESULTS + " entries."); continue; } if (attributes == null) { attributes = getSdbAttributes(getDbClient(), domainName, MIN_RESULTS); } long total = 0; String select = "select * from `" + domainName + "` limit 2500"; String nextToken = null; SelectResult res; do { res = getDbClient().select( new SelectRequest(select).withNextToken(nextToken) .withConsistentRead(false)); for (Item i : res.getItems()) { Map<String, Object> csvEntry = new HashMap<String, Object>(); csvEntry.put("_key", i.getName()); for (String attr : attributes) { csvEntry.put(attr, ""); } for (Attribute a : i.getAttributes()) { csvEntry.put(a.getName(), a.getValue()); } CSVExport.writeToFile(csvEntry, csvFile); } nextToken = res.getNextToken(); total += res.getItems().size(); log.info("Exported " + total + " of " + domainCount); } while (nextToken != null); log.info("Finished exporting from " + domainName); } } CSVExport.closeWriter(csvFile); }
private static Set<String> getSdbAttributes(AmazonSimpleDBClient client, String domainName, int sampleSize) { if (!client.listDomains().getDomainNames().contains(domainName)) { throw new IllegalArgumentException("SimpleDB domain '" + domainName + "' not accessible from given client instance"); } int domainCount = client.domainMetadata( new DomainMetadataRequest(domainName)).getItemCount(); if (domainCount < sampleSize) { throw new IllegalArgumentException("SimpleDB domain '" + domainName + "' does not have enough entries for accurate sampling."); } int avgSkipCount = domainCount / sampleSize; int processedCount = 0; String nextToken = null; Set<String> attributeNames = new HashSet<String>(); Random r = new Random(); do { int nextSkipCount = r.nextInt(avgSkipCount * 2) + 1; SelectResult countResponse = client.select(new SelectRequest( "select count(*) from `" + domainName + "` limit " + nextSkipCount).withNextToken(nextToken)); nextToken = countResponse.getNextToken(); processedCount += Integer.parseInt(countResponse.getItems().get(0) .getAttributes().get(0).getValue()); SelectResult getResponse = client.select(new SelectRequest( "select * from `" + domainName + "` limit 1") .withNextToken(nextToken)); nextToken = getResponse.getNextToken(); processedCount++; if (getResponse.getItems().size() > 0) { for (Attribute a : getResponse.getItems().get(0) .getAttributes()) { attributeNames.add(a.getName()); } } } while (domainCount > processedCount); return attributeNames; }