Java Dynamodb can';不要创建表

Java Dynamodb can';不要创建表,java,amazon-web-services,amazon-dynamodb,dynamo-local,Java,Amazon Web Services,Amazon Dynamodb,Dynamo Local,我正在本地服务器上测试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 faile

我正在本地服务器上测试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个带有全局索引):

公共类主{
AmazonDynamoDBClient;
静态发电机;
静态字符串users=“users”;
静态字符串contacts=“contacts”;
静态字符串messages=“messages”;
public Main()引发异常{
client=newamazondynamodbclient(newprofilecredentialsprovider());
client.setEndpoint(“http://localhost:8000");
dynamoDB=新的dynamoDB(客户);
试一试{
删除表(用户);
删除表(联系人);
删除表(消息);
createTable(用户,10L,5L,“un”,“S”);
createTable(触点,10L,5L,“至”,“S”);
createTable(消息,10L,5L,“fr”,“S”,“cr”,“S”);
}捕获(例外e){
System.err.println(“程序失败:”);
System.err.println(e.getMessage());
}
System.out.println(“成功”);
私有静态void deleteTable(字符串tableName){
Table Table=dynamoDB.getTable(tableName);
试一试{
System.out.println(“为“+tableName”发出deletetetable请求);
表1.delete();
System.out.println(“等待”+表名
+“要删除…这可能需要一段时间…”);
table.waitForDelete();
}捕获(例外e){
System.err.println(“针对“+tableName”的DeleteTable请求失败);
System.err.println(e.getMessage());
}
}
私有静态void createTable(
字符串表名、长读容量单位、长写容量单位、,
字符串hashKeyName、字符串hashKeyType){
createTable(tableName、readCapacityUnits、writeCapacityUnits、,
hashKeyName,hashKeyType,null,null);
}
私有静态void createTable(
字符串表名、长读容量单位、长写容量单位、,
字符串hashKeyName、字符串hashKeyType、,
字符串rangeKeyName、字符串rangeKeyType){
试一试{
ArrayList keySchema=新的ArrayList();
添加(新的KeySchemaElement()
.withAttributeName(hashKeyName)
.withKeyType(KeyType.HASH));
ArrayList attributeDefinitions=新的ArrayList();
添加(新的AttributeDefinition()
.withAttributeName(hashKeyName)
.withAttributeType(hashKeyType));
如果(rangeKeyName!=null){
添加(新的KeySchemaElement()
.withAttributeName(rangeKeyName)
.withKeyType(KeyType.RANGE));
添加(新的AttributeDefinition()
.withAttributeName(rangeKeyName)
.withAttributeType(rangeKeyType));
}
CreateTableRequest请求=新建CreateTableRequest()
.withTableName(tableName)
.withKeySchema(keySchema)
.withProvisionedThroughput(新的ProvisionedThroughput()
.带ReadCapacityInits(ReadCapacityInits)
.具有书面容量单位(书面容量单位));
if(contacts.equals(tableName)){
//精密指数
GlobalSecondaryIndex contactsFromIndex=新的GlobalSecondaryIndex()
.withIndexName(“来自索引的联系人”)
.withProvisionedThroughput(新的ProvisionedThroughput()
.带读取容量(长)10)
.带写容量单位((长)5))
.withProjection(新投影().withProjectionType(仅ProjectionType.KEYS_));
ArrayList indexKeySchema=新的ArrayList();
添加(新的KeySchemaElement()
.withAttributeName(“fr”)
.带按键类型(“S”);
添加(新的KeySchemaElement()
.withAttributeName(“st”)
.带按键类型(“N”);
联系人fromIndex.setKeySchema(indexKeySchema);
请求。使用全局二次索引(来自索引的联系人);
}
if(messages.equals(tableName)){
//精密指数
GlobalSecondaryIndex messagesFromIndex=新的GlobalSecondaryIndex()
.withIndexName(“来自索引的消息”)
.withProvisionedThroughput(新的ProvisionedThroughput()
.带读取容量(长)10)
.带写容量单位((长)5))
.withProjection(新投影().withProjectionType(ProjectionType.INCLUDE)
.具有非关键属性(“mg”);
ArrayList indexKeySchema=新的ArrayList();
添加(新的KeySchemaElement()
.withAttributeName(“to”)
.带按键类型(“S”);
添加(新的KeySchemaElement()
.withAttributeName(“cr”)
.带按键类型(“N”);
messagesFromIndex.setKeySchema(indexKeySchema);
请求。带有全局二次索引(来自索引的消息);
}
request.setAttributeDefinitions(attributeDefinitions);
System.out.println(“为“+tableName”发出CreateTable请求);
Table Table=dynamoDB.createTable(请求);
System.out.println(“等待”+表名
+“到
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());
        }
    }

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