acs254 acs254 - 1 month ago 6
Java Question

DynamoDB Type mismatch: cannot convert from CreateTableResult to Table

In Eclipse Java EE IDE , I am trying to programmatically create a DynamoDB table. I have the method below, which is borrowed from several other sources. At

Table table = DYNAMODB.createTable(request)
, Eclipse is giving the exception: Type mismatch: cannot convert from CreateTableResult to Table.

public static final String S3_BUCKET_CHANNELS = "channels";
public static final String S3_BUCKET_EPISODES = "episodes";
public static final String SQS_QUEUE_NAME = "queue";
public static final String DYNAMODB_TABLE_CHANNELS = "channels";
public static final String DYNAMODB_TABLE_EPISODES = "episodes";

public static final String MACRO_PATH = "macros/";
public static final String FINISHED_PATH = "final/";

public static final AWSCredentialsProvider CREDENTIALS_PROVIDER =
new ClasspathPropertiesFileCredentialsProvider();

public static final Region REGION = Region.getRegion(Regions.US_WEST_2);

public static final AmazonS3Client S3 = new AmazonS3Client(CREDENTIALS_PROVIDER);
public static final AmazonSQSClient SQS = new AmazonSQSClient(CREDENTIALS_PROVIDER);
public static final AmazonDynamoDBClient DYNAMODB = new AmazonDynamoDBClient(CREDENTIALS_PROVIDER);
public static final DynamoDBMapper DYNAMODB_MAPPER = new DynamoDBMapper(DYNAMODB, CREDENTIALS_PROVIDER);

static {
DYNAMODB.setRegion(REGION);
SQS.setRegion(REGION);
}

public static void createChannelsTable() throws Exception {
List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(1);
attributeDefinitions.add(new AttributeDefinition().withAttributeName("url").withAttributeType(ScalarAttributeType.S));
attributeDefinitions.add(new AttributeDefinition().withAttributeName("title").withAttributeType(ScalarAttributeType.S));

List<KeySchemaElement> keyDefinitions = new ArrayList<KeySchemaElement>(2);
keyDefinitions.add(new KeySchemaElement().withAttributeName("url").withKeyType(KeyType.HASH));

ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput(50l, 50l);

GlobalSecondaryIndex globalSecondaryIndex =
new GlobalSecondaryIndex().withIndexName("channelIndex")
.withProjection(new Projection().withProjectionType(ProjectionType.ALL))
.withKeySchema(
new KeySchemaElement("title", KeyType.HASH),
new KeySchemaElement("url", KeyType.RANGE)
)
.withProvisionedThroughput(provisionedThroughput);

CreateTableRequest request =
new CreateTableRequest().withTableName(DYNAMODB_TABLE_CHANNELS)
.withKeySchema(keyDefinitions)
.withAttributeDefinitions(attributeDefinitions)
.withProvisionedThroughput(provisionedThroughput)
.withGlobalSecondaryIndexes(globalSecondaryIndex);

try {
Table table = DYNAMODB.createTable(request);
table.waitForActive();
} catch (Exception e) {
System.err.println("Unable to create table: ");
System.err.println(e.getMessage());
}

}


Casting the Table does not work, and oddly enough, when I import this into Eclipse Neon there is no error. I'm using
com.amazonaws.services.dynamodbv2.document.Table
from aws-java-sdk-dynamodb-1.10.42.jar.

Answer

You are calling createTable on DYNAMODB, which in your case is an instance of AmazonDynamoDBClient.

Rather, you need to call createTable on an instance of com.amazonaws.services.dynamodbv2.document.DynamoDB. More specifically, change your code like this:

public static final AmazonDynamoDBClient DYNAMODB_CLIENT = new AmazonDynamoDBClient(CREDENTIALS_PROVIDER);
public static final DynamoDB DYNAMODB = new DynamoDB(DYNAMODB_CLIENT);

...

Table table = DYNAMODB.createTable(request);
table.waitForActive();

See a complete example here.