@Override
  public void createTable(
      String tableName, KeySchema ks, ProvisionedThroughput provisionedThroughput)
      throws DataAccessException {
    CreateTableRequest request =
        new CreateTableRequest()
            .withTableName(tableName)
            .withKeySchema(ks)
            .withProvisionedThroughput(provisionedThroughput);

    try {
      CreateTableResult result = ddb.createTable(request);
      // now we must wait until table is ACTIVE
      TableDescription tableDescription = waitTillTableState(tableName, "ACTIVE");
      if (!"ACTIVE".equals(tableDescription.getTableStatus())) {
        throw new DataStoreOperationException(
            "could not create table "
                + tableName
                + ", current table description: "
                + tableDescription);
      }
    } catch (AmazonClientException e) {
      throw new DataStoreOperationException(
          "problem with table: "
              + tableName
              + ", key schema: "
              + ks
              + ", provisioned throughput: "
              + provisionedThroughput,
          e);
    }
  }
  private static void waitForTableToBecomeAvailable(AmazonDynamoDB dynamo, 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 {
        Thread.sleep(1000 * 20);
      } catch (Exception e) {
      }
      try {
        DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
        TableDescription table = dynamo.describeTable(request).getTable();
        if (table == null) continue;

        String tableStatus = table.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");
  }
 private static boolean doesTableExist(AmazonDynamoDB dynamo, String tableName) {
   try {
     TableDescription table =
         dynamo.describeTable(new DescribeTableRequest().withTableName(tableName)).getTable();
     return "ACTIVE".equals(table.getTableStatus());
   } catch (AmazonServiceException ase) {
     if (ase.getErrorCode().equals("ResourceNotFoundException")) return false;
     throw ase;
   }
 }
 private TableDescription waitTillTableState(String tableName, String desiredState) {
   int attempt = 0;
   TableDescription tableDescription = null;
   do {
     tableDescription =
         ddb.describeTable(new DescribeTableRequest().withTableName(tableName)).getTable();
     attempt++;
     try {
       Thread.sleep(200);
     } catch (InterruptedException e) {
     }
   } while (attempt < 1000 && !desiredState.equals(tableDescription.getTableStatus()));
   return tableDescription;
 }