java - DynamoDB 类型不匹配 : cannot convert from CreateTableResult to Table

标签 java eclipse amazon-web-services amazon-dynamodb

在 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

最佳答案

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

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

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 类型不匹配 : cannot convert from CreateTableResult to Table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40385842/

相关文章:

java - 从 jar 中读取文件在 eclipse 中不起作用

java - 从 .jar 文件启动文本文件(文件路径?)

amazon-web-services - 云形成: Environment failed to launch as it entered Terminated state

linux - 无法从 AWS 外部通过 HTTP 连接到 EC2 实例

python - Boto AWS S3- "NoSuchKey",当 key 确实存在时

java - Jaudiotagger库返回的轨道长度错误

java - 如何开始构建基于 Java 的网络抓取工具

java - 如何为 SOAP 请求中的子元素设置 Type 属性?

eclipse - Eclipse 中的 EGit 导入显示运行时错误

java - 使用 Java 确定计算机打开的时间