List of usage examples for com.amazonaws.services.dynamodbv2.model DescribeTableRequest DescribeTableRequest
public DescribeTableRequest()
From source file:DynamoDBUtils.java
License:Open Source License
/** * Helper method to get the status of an Amazon DynamoDB table * * @param client//w w w . ja v a 2 s . c om * The {@link AmazonDynamoDBClient} with Amazon DynamoDB read privileges * @param tableName * The Amazon DynamoDB table to get the status of * @return */ private static TableStatus getTableStatus(AmazonDynamoDBClient client, String tableName) { DescribeTableRequest describeTableRequest = new DescribeTableRequest(); describeTableRequest.setTableName(tableName); DescribeTableResult describeTableResult = client.describeTable(describeTableRequest); String status = describeTableResult.getTable().getTableStatus(); return TableStatus.fromValue(status); }
From source file:DynamoDBUtils.java
License:Open Source License
/** * Verifies if the table has the expected schema. * * @param client//from w ww . java2 s .c om * The {@link AmazonDynamoDBClient} with Amazon DynamoDB read privileges * @param tableName * The Amazon DynamoDB table to check * @param key * The expected hashkey for the Amazon DynamoDB table * @return true if the Amazon DynamoDB table exists and the expected hashkey matches the table schema, * otherwise return false */ private static boolean tableHasCorrectSchema(AmazonDynamoDBClient client, String tableName, String key) { DescribeTableRequest describeTableRequest = new DescribeTableRequest(); describeTableRequest.setTableName(tableName); DescribeTableResult describeTableResult = client.describeTable(describeTableRequest); TableDescription tableDescription = describeTableResult.getTable(); if (tableDescription.getAttributeDefinitions().size() != 1) { LOG.error("The number of attribute definitions does not match the existing table."); return false; } AttributeDefinition attributeDefinition = tableDescription.getAttributeDefinitions().get(0); if (!attributeDefinition.getAttributeName().equals(key) || !attributeDefinition.getAttributeType().equals(ScalarAttributeType.S.toString())) { LOG.error("Attribute name or type does not match existing table."); return false; } List<KeySchemaElement> KSEs = tableDescription.getKeySchema(); if (KSEs.size() != 1) { LOG.error("The number of key schema elements does not match the existing table."); return false; } KeySchemaElement kse = KSEs.get(0); if (!kse.getAttributeName().equals(key) || !kse.getKeyType().equals(KeyType.HASH.toString())) { LOG.error("The hash key does not match the existing table."); return false; } return true; }
From source file:DynamoDBUtils.java
License:Open Source License
/** * Helper method to determine if an Amazon DynamoDB table exists. * * @param client//www . ja v a2s .c om * The {@link AmazonDynamoDBClient} with Amazon DynamoDB read privileges * @param tableName * The Amazon DynamoDB table to check for * @return true if the Amazon DynamoDB table exists, otherwise return false */ private static boolean tableExists(AmazonDynamoDBClient client, String tableName) { DescribeTableRequest describeTableRequest = new DescribeTableRequest(); describeTableRequest.setTableName(tableName); try { client.describeTable(describeTableRequest); return true; } catch (ResourceNotFoundException e) { return false; } }
From source file:AmazonDynamoDBSample_PutThrottled.java
License:Open Source License
public static void main(String[] args) throws Exception { init();//from w w w .ja v a 2 s . com try { String tableName = "my-favorite-movies-table"; // Create a table with a primary hash key named 'name', which holds // a string CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName) .withKeySchema(new KeySchemaElement().withAttributeName("name").withKeyType(KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition().withAttributeName("name") .withAttributeType(ScalarAttributeType.S)) .withProvisionedThroughput( new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L)); // Create table if it does not exist yet TableUtils.createTableIfNotExists(dynamoDB, createTableRequest); // wait for the table to move into ACTIVE state TableUtils.waitUntilActive(dynamoDB, tableName); // Describe our new table DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName); TableDescription tableDescription = dynamoDB.describeTable(describeTableRequest).getTable(); System.out.println("Table Description: " + tableDescription); // Add an item Map<String, AttributeValue> item = newItem("Bill & Ted's Excellent Adventure", 1989, "****", "James", "Sara"); PutItemRequest putItemRequest = new PutItemRequest(tableName, item); PutItemResult putItemResult = dynamoDB.putItem(putItemRequest); System.out.println("Result: " + putItemResult); // Add another item item = newItem("Airplane", 1980, "*****", "James", "Billy Bob"); putItemRequest = new PutItemRequest(tableName, item); putItemResult = dynamoDB.putItem(putItemRequest); System.out.println("Result: " + putItemResult); Thread[] thrds = new Thread[16]; for (int i = 0; i < thrds.length; i++) { thrds[i] = newItemCreationThread(tableName, i); thrds[i].start(); } for (Thread thrd : thrds) { thrd.join(); } // Scan items for movies with a year attribute greater than 1985 HashMap<String, Condition> scanFilter = new HashMap<String, Condition>(); Condition condition = new Condition().withComparisonOperator(ComparisonOperator.GT.toString()) .withAttributeValueList(new AttributeValue().withN("1985")); scanFilter.put("year", condition); ScanRequest scanRequest = new ScanRequest(tableName).withScanFilter(scanFilter); ScanResult scanResult = dynamoDB.scan(scanRequest); System.out.println("Result: " + scanResult); } 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()); } }
From source file:TableCreator.java
License:Open Source License
public static void main(String[] args) throws Exception { init();// w w w . ja va2 s . c o m long readCapacity = Long.valueOf(args[0]); long writeCapacity = Long.valueOf(args[1]); try { String tableName = "messages"; // DeleteTableRequest deleteTableRequest = new DeleteTableRequest(tableName); // dynamoDB.deleteTable(deleteTableRequest); // // waitForTableToBecomeAvailable(tableName); // Create a table with a primary hash key named 'name', which holds a string CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName) .withKeySchema(new KeySchemaElement().withAttributeName("messageId").withKeyType(KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition().withAttributeName("messageId") .withAttributeType(ScalarAttributeType.S)) .withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(readCapacity) .withWriteCapacityUnits(writeCapacity)); TableDescription createdTableDescription = dynamoDB.createTable(createTableRequest) .getTableDescription(); System.out.println("Created Table: " + createdTableDescription); // Wait for it to become active waitForTableToBecomeAvailable(tableName); // Describe our new table DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName); TableDescription tableDescription = dynamoDB.describeTable(describeTableRequest).getTable(); System.out.println("Table Description: " + tableDescription); } 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()); } }
From source file:TableCreator.java
License:Open Source License
private static void waitForTableToBecomeAvailable(String tableName) { System.out.println("Waiting for " + tableName + " to become ACTIVE..."); long startTime = System.currentTimeMillis(); long endTime = startTime + (10 * 60 * 1000); while (System.currentTimeMillis() < endTime) { try {/* w w w. j a va 2 s . c o m*/ Thread.sleep(1000 * 20); } catch (Exception e) { } try { DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName); TableDescription tableDescription = dynamoDB.describeTable(request).getTable(); String tableStatus = tableDescription.getTableStatus(); System.out.println(" - current state: " + tableStatus); if (tableStatus.equals(TableStatus.ACTIVE.toString())) return; } catch (AmazonServiceException ase) { if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == false) throw ase; } } throw new RuntimeException("Table " + tableName + " never went active"); }
From source file:TableCreator.java
License:Open Source License
private static void waitForTableToDelete(String tableName) { System.out.println("Waiting for " + tableName + " to become ACTIVE..."); long startTime = System.currentTimeMillis(); long endTime = startTime + (10 * 60 * 1000); while (System.currentTimeMillis() < endTime) { try {/*from w w w . j av a2 s . c om*/ Thread.sleep(1000 * 20); } catch (Exception e) { } try { DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName); TableDescription tableDescription = dynamoDB.describeTable(request).getTable(); String tableStatus = tableDescription.getTableStatus(); System.out.println(" - current state: " + tableStatus); if (tableStatus.equals(TableStatus.ACTIVE.toString())) return; } catch (AmazonServiceException ase) { if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == false) throw ase; } } throw new RuntimeException("Table " + tableName + " never went active"); }
From source file:ProductCategoryPriceIndex.java
License:Open Source License
public static void main(String[] args) throws Exception { init();/* w w w .ja va 2 s . c o m*/ try { String tableName = "product_cat_pi"; // Create table if it does not exist yet if (!Tables.doesTableExist(dynamoDB, tableName)) { System.out.println("Table " + tableName + " is does not exist"); } // Describe our new table DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName); TableDescription tableDescription = dynamoDB.describeTable(describeTableRequest).getTable(); System.out.println("Table Description: " + tableDescription); // Add an item Map<String, AttributeValue> item = newItem("TV", 2, "TV Type one", "TV Type two"); PutItemRequest putItemRequest = new PutItemRequest(tableName, item); PutItemResult putItemResult = dynamoDB.putItem(putItemRequest); System.out.println("Result: " + putItemResult); } 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()); } }
From source file:AmazonDynamoDBSample.java
License:Open Source License
public static void main(String[] args) throws Exception { init();// w w w . jav a 2s. co m try { String tableName = "storyTable"; // Create table if it does not exist yet if (Tables.doesTableExist(dynamoDB, tableName)) { System.out.println("Table " + tableName + " is already ACTIVE"); } else { // Create a table with a primary hash key named 'name', which holds a string CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName) .withKeySchema( new KeySchemaElement().withAttributeName("storyID").withKeyType(KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition().withAttributeName("storyID") .withAttributeType(ScalarAttributeType.N)) .withProvisionedThroughput( new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L)); TableDescription createdTableDescription = dynamoDB.createTable(createTableRequest) .getTableDescription(); System.out.println("Created Table: " + createdTableDescription); // Wait for it to become active System.out.println("Waiting for " + tableName + " to become ACTIVE..."); Tables.awaitTableToBecomeActive(dynamoDB, tableName); } // Describe our new table DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName); TableDescription tableDescription = dynamoDB.describeTable(describeTableRequest).getTable(); System.out.println("Table Description: " + tableDescription); Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); System.out.println(currentTimestamp); ByteBuffer b = createBody(); int id1 = 1; int id2 = 2; double d1 = 0; double d2 = 1; Timestamp inputTimestamp = currentTimestamp; // Add an item Map<String, AttributeValue> item = newItem(id1, "JAM", "This is JAM", b, inputTimestamp, inputTimestamp, d1, d2); System.out.println("here"); PutItemRequest putItemRequest = new PutItemRequest(tableName, item); System.out.println("here2"); System.out.println(item); System.out.println(putItemRequest); PutItemResult putItemResult = dynamoDB.putItem(putItemRequest); System.out.println("here3"); System.out.println("Result: " + putItemResult); // Add another item item = newItem(id2, "JAM2", "This is JAM2", b, inputTimestamp, inputTimestamp, d1, d2); putItemRequest = new PutItemRequest(tableName, item); putItemResult = dynamoDB.putItem(putItemRequest); System.out.println("Result: " + putItemResult); // Scan items for movies with a year attribute greater than 1985 // HashMap<String, Condition> scanFilter = new HashMap<String, Condition>(); // Condition condition = new Condition() // .withComparisonOperator(ComparisonOperator.GT) // .withAttributeValueList(new AttributeValue()); // scanFilter.put("year", condition); // ScanRequest scanRequest = new ScanRequest(tableName).withScanFilter(scanFilter); // ScanResult scanResult = dynamoDB.scan(scanRequest); // System.out.println("Result: " + scanResult); } 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()); } }
From source file:RandomQuery1OnDynamoDB.java
License:Open Source License
public static void main(String[] args) throws Exception { init();//from ww w. j av a 2s .co m try { // Describe our new table DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName); TableDescription tableDescription = dynamoDB.describeTable(describeTableRequest).getTable(); System.out.println("Table Description: " + tableDescription); System.out.println("Table count :" + tableDescription.getItemCount()); long lStartTime = new Date().getTime(); System.out.println("start time: " + lStartTime); Random rn = new Random(); // Scan items for movies with a year attribute greater than 1985 Long lItem = tableDescription.getItemCount(); int iNoOfItems = lItem.intValue(); System.out.println("no of item " + lItem); for (int i = 0; i <= 49999; i++) { String randomInt = Integer.toString(rn.nextInt(iNoOfItems)); HashMap<String, Condition> scanFilter = new HashMap<String, Condition>(); Condition condition = new Condition().withComparisonOperator(ComparisonOperator.EQ.toString()) .withAttributeValueList(new AttributeValue().withN(randomInt)); scanFilter.put("id", condition); ScanRequest scanRequest = new ScanRequest(tableName).withScanFilter(scanFilter); ScanResult scanResult = dynamoDB.scan(scanRequest); System.out.println("Random No :" + randomInt + ":: Query no: " + (i + 1)); } // calculate time difference for update file time long lEndTime = new Date().getTime(); long difference = lEndTime - lStartTime; System.out.println("Elapsed milliseconds: " + difference); System.out.println("Elapsed seconds: " + difference * 0.001); System.out.println("Elapsed Minutes: " + (int) ((difference / (1000 * 60)) % 60)); } 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()); } }