/* * Creates a table with the following attributes: * * Table name: testTableName Hash key: userNo type N Read Capacity Units: 10 * Write Capacity Units: 5 */ public static void createTable() { /*AmazonDynamoDBClient ddb = UserPreferenceDemoActivity.clientManager .ddb();*/ AmazonDynamoDBClient ddb = LoginActivity.clientManager.ddb(); KeySchemaElement kse = new KeySchemaElement().withAttributeName("userNo").withKeyType(KeyType.HASH); AttributeDefinition ad = new AttributeDefinition() .withAttributeName("userNo") .withAttributeType(ScalarAttributeType.N); ProvisionedThroughput pt = new ProvisionedThroughput().withReadCapacityUnits(10l).withWriteCapacityUnits(5l); CreateTableRequest request = new CreateTableRequest() .withTableName(PropertyLoader.getInstance().getTestTableName()) .withKeySchema(kse) .withAttributeDefinitions(ad) .withProvisionedThroughput(pt); try { ddb.createTable(request); } catch (AmazonServiceException ex) { /*UserPreferenceDemoActivity.clientManager .wipeCredentialsOnAuthError(ex);*/ LoginActivity.clientManager.wipeCredentialsOnAuthError(ex); } }
public static void main(String[] args) { AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider()); client.setRegion(Region.getRegion(Regions.US_WEST_2)); DynamoDBMapper mapper = new DynamoDBMapper(client); // retrieve an object using a "normal" (non-composite) id Movie m = new Movie(); m.setTitle("foxy brown"); Movie m2 = mapper.load(m); if (m2.getExtra() != null) { System.out.println("title:" + m2.getTitle() + " extra:" + m2.getExtra()); } // retrieve an object using a composite key Artist a = new Artist(); a.setId(5); a.setName("Van Gogh"); Artist a2 = mapper.load(a); // scan through a bunch of entries in a table and print them out ScanRequest scanRequest = new ScanRequest().withTableName("Artists"); ScanResult result = client.scan(scanRequest); for (Map<String, AttributeValue> item : result.getItems()) { String o1 = item.get("id").getN(); String o2 = item.get("name").getS(); Artist artist = mapper.load(Artist.class, new Integer(o1), o2); artist.dump(); } }
public AuthDaoImpl(AwsRegionCreds credentials) { try { AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials); client.setRegion(Region.getRegion(credentials.getAWSRegion())); db = new DynamoDBMapper(client); } catch (Exception e) { throw new RuntimeException(e); } }
/* * Deletes the test table and all of its users and their attribute/value * pairs. */ public static void cleanUp() { AmazonDynamoDBClient ddb = LoginActivity.clientManager.ddb(); DeleteTableRequest request = new DeleteTableRequest().withTableName(PropertyLoader.getInstance().getTestTableName()); try { ddb.deleteTable(request); } catch (AmazonServiceException ex) { LoginActivity.clientManager.wipeCredentialsOnAuthError(ex); } }
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"); HashMap<String, String> nameMap = new HashMap<String, String>(); nameMap.put("#yr", "year"); HashMap<String, Object> valueMap = new HashMap<String, Object>(); valueMap.put(":yyyy", 1985); QuerySpec querySpec = new QuerySpec() .withKeyConditionExpression("#yr = :yyyy") .withNameMap(new NameMap().with("#yr", "year")) .withValueMap(valueMap); ItemCollection<QueryOutcome> items = table.query(querySpec); Iterator<Item> iterator = items.iterator(); Item item = null; System.out.println("Movies from 1985"); while (iterator.hasNext()) { item = iterator.next(); System.out.println(item.getNumber("year") + ": " + item.getString("title")); } valueMap.put(":yyyy", 1992); valueMap.put(":letter1", "A"); valueMap.put(":letter2", "L"); querySpec .withProjectionExpression("#yr, title, info.genres, info.actors[0]") .withKeyConditionExpression("#yr = :yyyy and title between :letter1 and :letter2") .withNameMap(nameMap) .withValueMap(valueMap); items = table.query(querySpec); iterator = items.iterator(); System.out.println("Movies from 1992 - titles A-L, with genres and lead actor"); while (iterator.hasNext()) { item = iterator.next(); System.out.println(item.toString()); } }
public static void init() { try { credentials = new ProfileCredentialsProvider("default").getCredentials(); } catch (Exception e) { throw new AmazonClientException( "Cannot load the credentials from the credential profiles file. " + "Please make sure that your credentials file is at the correct " + "location (/Users/sandiwibowo/.aws/credentials), and is in valid format.", e); } AmazonDynamoDBClient dynamoDBAWS = new AmazonDynamoDBClient(credentials); Region usWest2 = Region.getRegion(Regions.US_WEST_2); dynamoDBAWS.setRegion(usWest2); dynamoDB = new DynamoDB(dynamoDBAWS); }
/** * This method performs a batch request into DynamoDB and returns records that were unsuccessfully * processed by the batch request. Throws IOException if the client calls to DynamoDB encounter an * exception. * * @param rList list of WriteRequests to batch * @param requestMap map of WriteRequests to records * @return records that did not get put in the table by the batch request * @throws IOException if DynamoDB client encounters an exception */ private List<Map<String, AttributeValue>> performBatchRequest( List<WriteRequest> rList, Map<WriteRequest, Map<String, AttributeValue>> requestMap) throws IOException { // Requests in the batch Map<String, List<WriteRequest>> requestItems = new HashMap<String, List<WriteRequest>>(); if (rList.isEmpty()) { return Collections.emptyList(); } requestItems.put(dynamoDBTableName, rList); BatchWriteItemResult result; BatchWriteItemRequest batchWriteItemRequest = new BatchWriteItemRequest().withRequestItems(requestItems); try { result = dynamoDBClient.batchWriteItem(batchWriteItemRequest); return unproccessedItems(result, requestMap); } catch (AmazonClientException e) { String message = "DynamoDB Client could not perform batch request"; LOG.error(message, e); throw new IOException(message, e); } catch (Exception e) { String message = "Unexpected Exception while performing batch request"; LOG.error(message, e); throw new IOException(message, e); } }
public void createSimTradeTable() { CreateTableRequest createTableRequest = mapper.generateCreateTableRequest(SimDetails.class); // Table provision throughput is still required since it cannot be // specified in your POJO createTableRequest.setProvisionedThroughput(new ProvisionedThroughput(5L, 5L)); // Fire off the CreateTableRequest using the low-level client amazonDynamoDBClient.createTable(createTableRequest); }
/* * Retrieves the table description and returns the table status as a string. */ public static String getTestTableStatus() { try { /*AmazonDynamoDBClient ddb = UserPreferenceDemoActivity.clientManager .ddb();*/ AmazonDynamoDBClient ddb = LoginActivity.clientManager.ddb(); DescribeTableRequest request = new DescribeTableRequest().withTableName(PropertyLoader.getInstance().getTestTableName()); DescribeTableResult result = ddb.describeTable(request); String status = result.getTable().getTableStatus(); return status == null ? "" : status; } catch (ResourceNotFoundException e) { } catch (AmazonServiceException ex) { LoginActivity.clientManager.wipeCredentialsOnAuthError(ex); } return ""; }
/** * 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.ProfilesConfigFile * @see com.amazonaws.ClientConfiguration */ private static void init() throws Exception { /* * The ProfileCredentialsProvider will return your [default] * credential profile by reading from the credentials file located at * (~/.aws/credentials). */ AWSCredentials credentials = null; try { credentials = new ProfileCredentialsProvider().getCredentials(); } catch (Exception e) { throw new AmazonClientException( "Cannot load the credentials from the credential profiles file. " + "Please make sure that your credentials file is at the correct " + "location (~/.aws/credentials), and is in valid format.", e); } dynamoDB = new AmazonDynamoDBClient(credentials); Region usEast1 = Region.getRegion(Regions.US_EAST_1); dynamoDB.setRegion(usEast1); }
// // ONE TIME ADD CONTACTS // // private static void oneTimeAddContacts() { String firstNameLastName = ""; String saltS = ""; String ssnhash = ""; Set<String> unionSet = new HashSet<>(); unionSet.addAll(set1); unionSet.addAll(set2); String strArrr[] = unionSet.toArray(new String[unionSet.size()]); int numEntries = saltHashMap.size(); for (int i = 0; i < numEntries; i++) { firstNameLastName = strArrr[i]; saltS = saltHashMap.get(strArrr[i]); ssnhash = hashedssnHashMap.get(strArrr[i]); Map<String, AttributeValue> item = newContactItem(firstNameLastName, saltS, ssnhash); PutItemRequest putItemRequest = new PutItemRequest("contacts-table", item); PutItemResult putItemResult = dynamoDB.putItem(putItemRequest); } }
@Override public void shutdown() { dynamoDBClient.shutdown(); }
private static void retrieveMultipleItemsBatchGet() { try { Map<String, KeysAndAttributes> requestItems = new HashMap<String, KeysAndAttributes>(); List<Map<String, AttributeValue>> tableKeys = new ArrayList<Map<String, AttributeValue>>(); Map<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put("Name", new AttributeValue().withS("Amazon S3")); tableKeys.add(key); key = new HashMap<String, AttributeValue>(); key.put("Name", new AttributeValue().withS("Amazon DynamoDB")); tableKeys.add(key); requestItems.put(table1Name, new KeysAndAttributes().withKeys(tableKeys)); tableKeys = new ArrayList<Map<String, AttributeValue>>(); key = new HashMap<String, AttributeValue>(); key.put("ForumName", new AttributeValue().withS("Amazon DynamoDB")); key.put("Subject", new AttributeValue().withS("DynamoDB Thread 1")); tableKeys.add(key); key = new HashMap<String, AttributeValue>(); key.put("ForumName", new AttributeValue().withS("Amazon DynamoDB")); key.put("Subject", new AttributeValue().withS("DynamoDB Thread 2")); tableKeys.add(key); key = new HashMap<String, AttributeValue>(); key.put("ForumName", new AttributeValue().withS("Amazon S3")); key.put("Subject", new AttributeValue().withS("S3 Thread 1")); tableKeys.add(key); requestItems.put(table2Name, new KeysAndAttributes().withKeys(tableKeys)); BatchGetItemResult result; BatchGetItemRequest batchGetItemRequest = new BatchGetItemRequest(); do { System.out.println("Making the request."); batchGetItemRequest.withRequestItems(requestItems); result = client.batchGetItem(batchGetItemRequest); List<Map<String, AttributeValue>> table1Results = result.getResponses().get(table1Name); if (table1Results != null) { System.out.println("Items in table " + table1Name); for (Map<String, AttributeValue> item : table1Results) { printItem(item); } } List<Map<String, AttributeValue>> table2Results = result.getResponses().get(table2Name); if (table2Results != null) { System.out.println("\nItems in table " + table2Name); for (Map<String, AttributeValue> item : table2Results) { printItem(item); } } // Check for unprocessed keys which could happen if you exceed provisioned // throughput or reach the limit on response size. for (Map.Entry<String, KeysAndAttributes> pair : result.getUnprocessedKeys().entrySet()) { System.out.println("Unprocessed key pair: " + pair.getKey() + ", " + pair.getValue()); } requestItems = result.getUnprocessedKeys(); } while (result.getUnprocessedKeys().size() > 0); } catch (AmazonServiceException ase) { System.err.println("Failed to retrieve items."); } }
// // ONE TIME ADD WARRANTS // // private static void oneTimeAddWarrants() { String jsonString = ""; try { jsonString = readFile("./json/warrants.json", StandardCharsets.UTF_8); } catch (IOException e) { System.out.println(e); } try { JSONObject rootObject = new JSONObject(jsonString); // Parse the JSON to a JSONObject JSONArray rows = rootObject.getJSONArray("stuff"); // Get all JSONArray rows // System.out.println("row lengths: " + rows.length()); set2 = new HashSet<>(); for (int j = 0; j < rows.length(); j++) { // Iterate each element in the elements array JSONObject element = rows.getJSONObject(j); // Get the element object String defendant = element.getString("Defendant"); String strarr[] = defendant.split(" "); String temp = strarr[1]; int len = strarr[0].length(); strarr[0] = strarr[0].substring(0, len - 1); strarr[1] = strarr[0]; strarr[0] = temp; String firstLast = strarr[0] + strarr[1]; firstLast = firstLast.toLowerCase(); set2.add(firstLast); // System.out.println(firstLast); int zipCode = 0; Boolean isZipCodeNull = element.isNull("ZIP Code"); if (!isZipCodeNull) zipCode = element.getInt("ZIP Code"); String dob = element.getString("Date of Birth"); String caseNumber = element.getString("Case Number"); String firstLastDOB = firstLast + dob; // pick a ssn from list String ssn = ssnList.get(ssnCounter); ssnCounter--; ssnHashMap.put(firstLast, ssn); // compute salt final Random ran = new SecureRandom(); byte[] salt = new byte[32]; ran.nextBytes(salt); String saltString = Base64.encodeBase64String(salt); // System.out.println("saltstring: " + saltString); saltHashMap.put(firstLast, saltString); // compute ripemd160 hash of ssn + salt String saltPlusSsn = saltString + ssn; // System.out.println("salt plus ssn: " + saltPlusSsn); String resultingHash = ""; try { byte[] r = saltPlusSsn.getBytes("US-ASCII"); RIPEMD160Digest d = new RIPEMD160Digest(); d.update(r, 0, r.length); byte[] o = new byte[d.getDigestSize()]; d.doFinal(o, 0); ByteArrayOutputStream baos = new ByteArrayOutputStream(40); Hex.encode(o, baos); resultingHash = new String(baos.toByteArray(), StandardCharsets.UTF_8); hashedssnHashMap.put(firstLast, resultingHash); } catch (UnsupportedEncodingException e) { System.out.println(e); } catch (IOException i) { System.out.println(i); } // compareNames(); Map<String, AttributeValue> item = newWarrantItem( firstLast, firstLastDOB, resultingHash, defendant, zipCode, dob, caseNumber); PutItemRequest putItemRequest = new PutItemRequest("warrants-table", item); PutItemResult putItemResult = dynamoDB.putItem(putItemRequest); } } catch (JSONException e) { // JSON Parsing error e.printStackTrace(); } }
public static void main(String[] args) throws Exception { init(); try { String violationsTable = "violations-table"; String citationsTable = "citations-table"; String warrantsTable = "warrants-table"; String contactsTable = "contacts-table"; String notificationsTable = "notifications-table"; // VIOLATIONS TABLE // // // Create violationsTable if it does not exist yet if (Tables.doesTableExist(dynamoDB, violationsTable)) { System.out.println("Table " + violationsTable + " is already ACTIVE"); } else { // Create a table with a primary hash key named 'name', which holds a string CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(violationsTable) .withKeySchema( new KeySchemaElement() .withAttributeName("citation_number") .withKeyType(KeyType.HASH)) .withAttributeDefinitions( new AttributeDefinition() .withAttributeName("citation_number") .withAttributeType(ScalarAttributeType.N)) .withProvisionedThroughput( new ProvisionedThroughput() .withReadCapacityUnits(25L) .withWriteCapacityUnits(25L)); TableDescription createdTableDescription = dynamoDB.createTable(createTableRequest).getTableDescription(); System.out.println("Created Table: " + createdTableDescription); // Wait for it to become active System.out.println("Waiting for " + violationsTable + " to become ACTIVE..."); Tables.awaitTableToBecomeActive(dynamoDB, violationsTable); } // Describe our new table DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(violationsTable); TableDescription tableDescription = dynamoDB.describeTable(describeTableRequest).getTable(); System.out.println("Table Description: " + tableDescription); // CITATIONS TABLE // // // Create citationsTable if it does not exist yet if (Tables.doesTableExist(dynamoDB, citationsTable)) { System.out.println("Table " + citationsTable + " is already ACTIVE"); } else { // Create a table with a primary hash key named 'name', which holds a string /* GlobalSecondaryIndex _firstLast = new GlobalSecondaryIndex() .withIndexName("first_last") .withProvisionedThroughput(new ProvisionedThroughput() .withReadCapacityUnits((long) 10) .withWriteCapacityUnits((long) 1)) .withProjection(new Projection().withProjectionType(ProjectionType.ALL)); GlobalSecondaryIndex _firstLastDOB = new GlobalSecondaryIndex() .withIndexName("first_last_dob") .withProvisionedThroughput(new ProvisionedThroughput() .withReadCapacityUnits((long) 10) .withWriteCapacityUnits((long) 1)) .withProjection(new Projection().withProjectionType(ProjectionType.ALL)); */ CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(citationsTable) .withKeySchema( new KeySchemaElement() .withAttributeName("first_last") .withKeyType(KeyType.HASH)) .withAttributeDefinitions( new AttributeDefinition() .withAttributeName("first_last") .withAttributeType(ScalarAttributeType.S)) .withProvisionedThroughput( new ProvisionedThroughput() .withReadCapacityUnits(25L) .withWriteCapacityUnits(25L)); // .withGlobalSecondaryIndexes(_firstLast, _firstLastDOB); TableDescription createdTableDescription = dynamoDB.createTable(createTableRequest).getTableDescription(); System.out.println("Created Table: " + createdTableDescription); // Wait for it to become active System.out.println("Waiting for " + citationsTable + " to become ACTIVE..."); Tables.awaitTableToBecomeActive(dynamoDB, citationsTable); } // Describe our new table describeTableRequest = new DescribeTableRequest().withTableName(citationsTable); tableDescription = dynamoDB.describeTable(describeTableRequest).getTable(); System.out.println("Table Description: " + tableDescription); // WARRANTS TABLE // // // Create warrantsTable if it does not exist yet if (Tables.doesTableExist(dynamoDB, warrantsTable)) { System.out.println("Table " + warrantsTable + " is already ACTIVE"); } else { /* GlobalSecondaryIndex _firstLastDOB = new GlobalSecondaryIndex() .withIndexName("first_last_dob") .withProvisionedThroughput(new ProvisionedThroughput() .withReadCapacityUnits((long) 10) .withWriteCapacityUnits((long) 1)) .withProjection(new Projection().withProjectionType(ProjectionType.ALL)); */ // Create a table with a primary hash key named 'name', which holds a string CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(warrantsTable) .withKeySchema( new KeySchemaElement().withAttributeName("id").withKeyType(KeyType.HASH)) .withAttributeDefinitions( new AttributeDefinition() .withAttributeName("id") .withAttributeType(ScalarAttributeType.S)) .withProvisionedThroughput( new ProvisionedThroughput() .withReadCapacityUnits(25L) .withWriteCapacityUnits(25L)); // .withGlobalSecondaryIndexes(_firstLastDOB); TableDescription createdTableDescription = dynamoDB.createTable(createTableRequest).getTableDescription(); System.out.println("Created Table: " + createdTableDescription); // Wait for it to become active System.out.println("Waiting for " + warrantsTable + " to become ACTIVE..."); Tables.awaitTableToBecomeActive(dynamoDB, warrantsTable); } // Describe our new table describeTableRequest = new DescribeTableRequest().withTableName(warrantsTable); tableDescription = dynamoDB.describeTable(describeTableRequest).getTable(); System.out.println("Table Description: " + tableDescription); // CONTACTS TABLE // // // Create violationsTable if it does not exist yet if (Tables.doesTableExist(dynamoDB, contactsTable)) { System.out.println("Table " + contactsTable + " is already ACTIVE"); } else { // Create a table with a primary hash key named 'name', which holds a string CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(contactsTable) .withKeySchema( new KeySchemaElement() .withAttributeName("first_last") .withKeyType(KeyType.HASH)) .withAttributeDefinitions( new AttributeDefinition() .withAttributeName("first_last") .withAttributeType(ScalarAttributeType.S)) .withProvisionedThroughput( new ProvisionedThroughput() .withReadCapacityUnits(25L) .withWriteCapacityUnits(25L)); TableDescription createdTableDescription = dynamoDB.createTable(createTableRequest).getTableDescription(); System.out.println("Created Table: " + createdTableDescription); // Wait for it to become active System.out.println("Waiting for " + contactsTable + " to become ACTIVE..."); Tables.awaitTableToBecomeActive(dynamoDB, contactsTable); } // Describe our new table describeTableRequest = new DescribeTableRequest().withTableName(violationsTable); tableDescription = dynamoDB.describeTable(describeTableRequest).getTable(); System.out.println("Table Description: " + tableDescription); ssnHashMap = new HashMap<>(); saltHashMap = new HashMap<>(); hashedssnHashMap = new HashMap<>(); ssnList = new ArrayList<>(); for (int i = 1010001; i < 1200001; i++) { SocialSecurityNumber ssn = new SocialSecurityNumber(i); String ssnString = ssn.toString(); ssnList.add(ssnString); } /* for (String str : ssnList) { System.out.println("SSN: " + str); } */ // oneTimeAddViolations(); // oneTimeAddCitations(); oneTimeAddWarrants(); // oneTimeAddContacts(); } catch (AmazonServiceException ase) { System.out.println( "Caught an AmazonServiceException, which means your request made it " + "to AWS, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println( "Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with AWS, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }