在Eclipse Java EE IDE中,我试图以编程方式创建DynamoDB表。我有下面的方法,它是从其他几个来源借来的。在Table table = DYNAMODB.createTable(request)处,Eclipse给出了例外:类型不匹配:无法从CreateTableResult转换为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());
            }

        }


强制转换表无效,奇怪的是,当我将其导入Eclipse Neon时没有错误。我正在使用aws-java-sdk-dynamodb-1.10.42.jar中的com.amazonaws.services.dynamodbv2.document.Table

最佳答案

您正在createTable上调用DYNAMODB,在您的情况下,它是AmazonDynamoDBClient的实例。

相反,您需要在createTable的实例上调用com.amazonaws.services.dynamodbv2.document.DynamoDB。更具体地说,像这样更改代码:

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();


请参见完整示例here

关于java - DynamoDB类型不匹配:无法从CreateTableResult转换为Table,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40385842/

10-09 06:31
查看更多