【问题标题】:Dynamodb can't create tablesDynamodb 无法创建表
【发布时间】:2015-09-29 04:35:41
【问题描述】:

我正在本地测试 AWS DynamoDB,但无法使用全局索引创建表。

我收到以下输出,但不确定原因。我想我正在向表和索引添加一个哈希键,所以我不确定它为什么会抱怨?

Issuing CreateTable request for Users
Waiting for Users to be created...this may take a while...
Issuing CreateTable request for Contacts
CreateTable request failed for Contacts
No Hash Key specified in schema.  All Dynamo DB tables must have exactly one hash key (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: b2c826df-a6e6-4bc5-af60-ff6b6c3a0065)
Issuing CreateTable request for Messages
CreateTable request failed for Messages
No Hash Key specified in schema.  All Dynamo DB tables must have exactly one hash key (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 2117d767-f87f-4147-bfee-cafce6cf0ad8)

这是我的代码(我正在创建 3 个表,2 个带有全局索引):

public class Main{

    AmazonDynamoDBClient client;
    static DynamoDB dynamoDB;

    static String users = "Users";
    static String contacts = "Contacts";
    static String messages = "Messages";


    public Main() throws Exception {

        client = new AmazonDynamoDBClient(new ProfileCredentialsProvider());
        client.setEndpoint("http://localhost:8000");
        dynamoDB = new DynamoDB(client);


        try {

            deleteTable(users);
            deleteTable(contacts);
            deleteTable(messages);

            createTable(users, 10L, 5L, "un", "S");
            createTable(contacts, 10L, 5L, "to", "S");
            createTable(messages, 10L, 5L, "fr", "S", "cr", "S");


        } catch (Exception e) {
            System.err.println("Program failed:");
            System.err.println(e.getMessage());
        }
        System.out.println("Success.");


    private static void deleteTable(String tableName) {
        Table table = dynamoDB.getTable(tableName);
        try {
            System.out.println("Issuing DeleteTable request for " + tableName);
            table.delete();
            System.out.println("Waiting for " + tableName
                    + " to be deleted...this may take a while...");
            table.waitForDelete();

        } catch (Exception e) {
            System.err.println("DeleteTable request failed for " + tableName);
            System.err.println(e.getMessage());
        }
    }


    private static void createTable(
            String tableName, long readCapacityUnits, long writeCapacityUnits,
            String hashKeyName, String hashKeyType) {

        createTable(tableName, readCapacityUnits, writeCapacityUnits,
                hashKeyName, hashKeyType, null, null);
    }

    private static void createTable(
            String tableName, long readCapacityUnits, long writeCapacityUnits,
            String hashKeyName, String hashKeyType,
            String rangeKeyName, String rangeKeyType) {

        try {

            ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
            keySchema.add(new KeySchemaElement()
                    .withAttributeName(hashKeyName)
                    .withKeyType(KeyType.HASH));

            ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
            attributeDefinitions.add(new AttributeDefinition()
                    .withAttributeName(hashKeyName)
                    .withAttributeType(hashKeyType));

            if (rangeKeyName != null) {
                keySchema.add(new KeySchemaElement()
                        .withAttributeName(rangeKeyName)
                        .withKeyType(KeyType.RANGE));
                attributeDefinitions.add(new AttributeDefinition()
                        .withAttributeName(rangeKeyName)
                        .withAttributeType(rangeKeyType));
            }

            CreateTableRequest request = new CreateTableRequest()
                    .withTableName(tableName)
                    .withKeySchema(keySchema)
                    .withProvisionedThroughput( new ProvisionedThroughput()
                            .withReadCapacityUnits(readCapacityUnits)
                            .withWriteCapacityUnits(writeCapacityUnits));



            if(contacts.equals(tableName)){

                // PrecipIndex
                GlobalSecondaryIndex contactsFromIndex = new GlobalSecondaryIndex()
                        .withIndexName("ContactsFromIndex")
                        .withProvisionedThroughput(new ProvisionedThroughput()
                                .withReadCapacityUnits((long) 10)
                                .withWriteCapacityUnits((long) 5))
                        .withProjection(new Projection().withProjectionType(ProjectionType.KEYS_ONLY));

                ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<KeySchemaElement>();

                indexKeySchema.add(new KeySchemaElement()
                        .withAttributeName("fr")
                        .withKeyType("S"));
                indexKeySchema.add(new KeySchemaElement()
                        .withAttributeName("st")
                        .withKeyType("N"));

                contactsFromIndex.setKeySchema(indexKeySchema);
                request.withGlobalSecondaryIndexes(contactsFromIndex);

            }


            if(messages.equals(tableName)){

                // PrecipIndex
                GlobalSecondaryIndex messagesFromIndex = new GlobalSecondaryIndex()
                        .withIndexName("messagesFromIndex")
                        .withProvisionedThroughput(new ProvisionedThroughput()
                                .withReadCapacityUnits((long) 10)
                                .withWriteCapacityUnits((long) 5))
                        .withProjection(new Projection().withProjectionType(ProjectionType.INCLUDE)
                                .withNonKeyAttributes("mg"));

                ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<KeySchemaElement>();

                indexKeySchema.add(new KeySchemaElement()
                        .withAttributeName("to")
                        .withKeyType("S"));
                indexKeySchema.add(new KeySchemaElement()
                        .withAttributeName("cr")
                        .withKeyType("N"));

                messagesFromIndex.setKeySchema(indexKeySchema);
                request.withGlobalSecondaryIndexes(messagesFromIndex);

            }


            request.setAttributeDefinitions(attributeDefinitions);

            System.out.println("Issuing CreateTable request for " + tableName);
            Table table = dynamoDB.createTable(request);
            System.out.println("Waiting for " + tableName
                    + " to be created...this may take a while...");
            table.waitForActive();

        } catch (Exception e) {
            System.err.println("CreateTable request failed for " + tableName);
            System.err.println(e.getMessage());
        }
    }

}

更新:错误消息“不能有两个同名的属性”和新代码:

        client = new AmazonDynamoDBClient(new ProfileCredentialsProvider());
        client.setEndpoint("http://localhost:8000");
        dynamoDB = new DynamoDB(client);


        try {

            deleteTable(users);
            deleteTable(contacts);
            deleteTable(messages);

            createTable(users, 10L, 5L, "un", "S");
            createTable(contacts, 10L, 5L, "to", "S");
            createTable(messages, 10L, 5L, "fr", "S", "cr", "S");


        } catch (Exception e) {
            System.err.println("Program failed:");
            System.err.println(e.getMessage());
        }
        System.out.println("Success.");


    private static void deleteTable(String tableName) {
        Table table = dynamoDB.getTable(tableName);
        try {
            System.out.println("Issuing DeleteTable request for " + tableName);
            table.delete();
            System.out.println("Waiting for " + tableName
                    + " to be deleted...this may take a while...");
            table.waitForDelete();

        } catch (Exception e) {
            System.err.println("DeleteTable request failed for " + tableName);
            System.err.println(e.getMessage());
        }
    }


    private static void createTable(
            String tableName, long readCapacityUnits, long writeCapacityUnits,
            String hashKeyName, String hashKeyType) {

        createTable(tableName, readCapacityUnits, writeCapacityUnits,
                hashKeyName, hashKeyType, null, null);
    }

    private static void createTable(
            String tableName, long readCapacityUnits, long writeCapacityUnits,
            String hashKeyName, String hashKeyType,
            String rangeKeyName, String rangeKeyType) {

        try {

            ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
            keySchema.add(new KeySchemaElement()
                    .withAttributeName(hashKeyName)
                    .withKeyType(KeyType.HASH));

            ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
            attributeDefinitions.add(new AttributeDefinition()
                    .withAttributeName(hashKeyName)
                    .withAttributeType(hashKeyType));

            if (rangeKeyName != null) {
                keySchema.add(new KeySchemaElement()
                        .withAttributeName(rangeKeyName)
                        .withKeyType(KeyType.RANGE));
                attributeDefinitions.add(new AttributeDefinition()
                        .withAttributeName(rangeKeyName)
                        .withAttributeType(rangeKeyType));
            }

            CreateTableRequest request = new CreateTableRequest()
                    .withTableName(tableName)
                    .withKeySchema(keySchema)
                    .withProvisionedThroughput( new ProvisionedThroughput()
                            .withReadCapacityUnits(readCapacityUnits)
                            .withWriteCapacityUnits(writeCapacityUnits));



            if(contacts.equals(tableName)){
 attributeDefinitions.add(new AttributeDefinition().withAttributeName("fr").withAttributeType("S"));
                     attributeDefinitions.add(new AttributeDefinition().withAttributeName("st").withAttributeType("S"));
                // PrecipIndex
                GlobalSecondaryIndex contactsFromIndex = new GlobalSecondaryIndex()
                        .withIndexName("ContactsFromIndex")
                        .withProvisionedThroughput(new ProvisionedThroughput()
                                .withReadCapacityUnits((long) 10)
                                .withWriteCapacityUnits((long) 5))
                        .withProjection(new Projection().withProjectionType(ProjectionType.KEYS_ONLY));

                ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<KeySchemaElement>();

                indexKeySchema.add(new KeySchemaElement()
                        .withAttributeName("fr")
                        .withKeyType(KeyType.HASH));
                indexKeySchema.add(new KeySchemaElement()
                        .withAttributeName("st")
                        .withKeyType(KeyType.RANGE));

                contactsFromIndex.setKeySchema(indexKeySchema);
                request.withGlobalSecondaryIndexes(contactsFromIndex);

            }


            if(messages.equals(tableName)){
attributeDefinitions.add(new AttributeDefinition().withAttributeName("to").withAttributeType("S"));
                    attributeDefinitions.add(new AttributeDefinition().withAttributeName("cr").withAttributeType("N"));

                // PrecipIndex
                GlobalSecondaryIndex messagesFromIndex = new GlobalSecondaryIndex()
                        .withIndexName("messagesFromIndex")
                        .withProvisionedThroughput(new ProvisionedThroughput()
                                .withReadCapacityUnits((long) 10)
                                .withWriteCapacityUnits((long) 5))
                        .withProjection(new Projection().withProjectionType(ProjectionType.INCLUDE)
                                .withNonKeyAttributes("mg"));

                ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<KeySchemaElement>();

                indexKeySchema.add(new KeySchemaElement()
                        .withAttributeName("to")
                        .withKeyType(KeyType.HASH));
                indexKeySchema.add(new KeySchemaElement()
                        .withAttributeName("cr")
                        .withKeyType(KeyType.RANGE));

                messagesFromIndex.setKeySchema(indexKeySchema);
                request.withGlobalSecondaryIndexes(messagesFromIndex);

            }


            request.setAttributeDefinitions(attributeDefinitions);

            System.out.println("Issuing CreateTable request for " + tableName);
            Table table = dynamoDB.createTable(request);
            System.out.println("Waiting for " + tableName
                    + " to be created...this may take a while...");
            table.waitForActive();

        } catch (Exception e) {
            System.err.println("CreateTable request failed for " + tableName);
            System.err.println(e.getMessage());
        }
    }

}

【问题讨论】:

    标签: java amazon-web-services amazon-dynamodb dynamo-local


    【解决方案1】:

    在您的代码构造中根据表名添加 GSI,

    if(contacts.equals(tableName)){
       ...
       .withKeyType("S"));
       .withKeyType("N"));
       ...
    }
    

    if(messages.equals(tableName)){
       ...
       .withKeyType("S"));
       .withKeyType("N"));
       ...
    }
    

    API withKeyType(...) 接受密钥类型,例如哈希 |范围,而不是数据类型。

    正确示例

    .withKeyType(KeyType.HASH));
    

    【讨论】:

    • 感谢您的回复。我已经向两者添加了哈希和范围键类型,但现在收到一条错误消息,提示“属性定义中未指定全局二级索引哈希键。类型未知。”
    • 异常是无法确定 GSI 指定的哈希键的数据类型。在这种情况下,您需要在您的 attributeDefinitions 数组列表中包含 GSI 哈希键。
    • 示例:attributeDefinitions.add(new AttributeDefinition().withAttributeName("to").withAttributeType("S"));
    • 我的 attributesDefenitions arraylist 不是已经从方法'create table'的传入参数中添加了键和类型吗?
    • 对于消息表,“cr”被添加到属性定义列表中两次,一次用于表,一次用于 GSI。仅当列表中不存在它时才应检查并添加。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2016-04-08
    • 2018-01-05
    相关资源
    最近更新 更多