我正在尝试在DynamoDB中创建一个表并发布该表,列出所有现有表。我使用的代码是
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Amazon;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
using Amazon.Runtime;
namespace DynamoDBTester
{
class Program
{
private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
private static string tableName = "DummyTable";
static void Main(string[] args)
{
// try
//{
CreateDummyTable();
// ListMyTables();
Console.WriteLine("To continue, press Enter");
Console.ReadLine();
//}
//catch (AmazonDynamoDBException e) { Console.WriteLine(e.Message); }
//catch (AmazonServiceException e) { Console.WriteLine(e.Message); }
//catch (Exception e) { Console.WriteLine(e.Message); }
}
private static void CreateDummyTable()
{
Console.WriteLine("\n*** Creating DummyTable ***");
var request = new CreateTableRequest
{
AttributeDefinitions = new List<AttributeDefinition>()
{
new AttributeDefinition
{
AttributeName = "Id",
AttributeType = "N"
}
,
new AttributeDefinition
{
AttributeName = "DateTime",
AttributeType = "S"
}
,
new AttributeDefinition
{
AttributeName = "Temperature",
AttributeType = "N"
}
},
KeySchema = new List<KeySchemaElement>
{
new KeySchemaElement
{
AttributeName = "Id",
KeyType = "HASH" //Partition key
},
new KeySchemaElement
{
AttributeName = "DateTime",
KeyType = "RANGE" //Partition key
},
new KeySchemaElement
{
AttributeName = "Temperature",
KeyType = "RANGE" //Partition key
}
},
ProvisionedThroughput = new ProvisionedThroughput
{
ReadCapacityUnits = 5,
WriteCapacityUnits = 6
},
TableName = tableName
};
var response = client.CreateTable(request);
var tableDescription = response.TableDescription;
Console.WriteLine("{1}: {0} \t ReadsPerSec: {2} \t WritesPerSec: {3}",
tableDescription.TableStatus,
tableDescription.TableName,
tableDescription.ProvisionedThroughput.ReadCapacityUnits,
tableDescription.ProvisionedThroughput.WriteCapacityUnits);
string status = tableDescription.TableStatus;
Console.WriteLine(tableName + " - " + status);
WaitUntilTableReady(tableName);
}
private static void WaitUntilTableReady(string tableName)
{
string status = null;
// Let us wait until table is created. Call DescribeTable.
do
{
System.Threading.Thread.Sleep(5000); // Wait 5 seconds.
try
{
var res = client.DescribeTable(new DescribeTableRequest
{
TableName = tableName
});
Console.WriteLine("Table name: {0}, status: {1}",
res.Table.TableName,
res.Table.TableStatus);
status = res.Table.TableStatus;
}
catch (ResourceNotFoundException)
{
// DescribeTable is eventually consistent. So you might
// get resource not found. So we handle the potential exception.
}
} while (status != "ACTIVE");
}
private static void ListMyTables()
{
Console.WriteLine("\n*** listing tables ***");
string lastTableNameEvaluated = null;
do
{
var request = new ListTablesRequest
{
Limit = 2,
ExclusiveStartTableName = lastTableNameEvaluated
};
var response = client.ListTables(request);
foreach (string name in response.TableNames)
Console.WriteLine(name);
lastTableNameEvaluated = response.LastEvaluatedTableName;
} while (lastTableNameEvaluated != null);
}
}
}
但是我得到了错误
Additional information: 1 validation error detected: Value '[com.amazonaws.dynamodb.v20120810.KeySchemaElement@21c24a, com.amazonaws.dynamodb.v20120810.KeySchemaElement@7357d4d9, com.amazonaws.dynamodb.v20120810.KeySchemaElement@7b38ae72]' at 'keySchema' failed to satisfy constraint: Member must have length less than or equal to 2
我的表名是DummyTable
它应该有3列:
1.Id
2.DateTime
3.温度
其中Id
是PrimaryKey
答案 0 :(得分:0)
<强>问题: - 强>
1)只能将一个属性定义为RANGE键。您有两个属性DateTime
和Temperature
定义为RANGE键
<强>解决方案: - 强>
如果您需要两个不同的RANGE键,则可以使用本地二级索引(LSI)。一张表可以有5个LSI。
答案 1 :(得分:0)
a.simple主键(仅分区键)或
b.complex主键(分区+排序键)
在您的情况下,它的复杂主键只需要提及分区键和排序键(只有发电机数据库所需的模式)
无需提及附加属性(创建表时附加列名称)。
需要更改代码是删除添加属性定义和架构
var request = new CreateTableRequest
{
AttributeDefinitions = new List<AttributeDefinition>()
{
new AttributeDefinition
{
AttributeName = "Id",
AttributeType = "N"
}
,
new AttributeDefinition
{
AttributeName = "DateTime",
AttributeType = "S"
}
}
KeySchema = new List<KeySchemaElement>
{
new KeySchemaElement
{
AttributeName = "Id",
KeyType = "HASH" //Partition key
},
new KeySchemaElement
{
AttributeName = "DateTime",
KeyType = "RANGE" //Range key
}
}
答案 2 :(得分:0)
在创建表时,您只需要保留具有哈希或范围架构属性的列即可。
对于创建表时无需提及的其他列。在插入项目时,您可以动态地将任意数量的列添加到记录中,并将其保存在指定的hash / range属性中。