2017-05-30 92 views
0

我试图在DynamoDB中创建一个表,并发布该表,列出所有现有的表。我使用的代码c#在Dynamo DB中创建表

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 '[[email protected], [email protected], [email protected]]' at 'keySchema' failed to satisfy constraint: Member must have length less than or equal to 2 

我的表名是DummyTable

它应该有3列:

1.Id

2.日期时间

3.温度

其中IdPrimaryKey

+0

它说,关键模式不能超过2个项目。在max中,您可以将一个分区键和一个sortkey作为关键模式的一部分。 –

+0

我试着从'KeySchema'中删除'Temperature'属性。然后,我收到错误信息:附加信息:一个或多个参数值无效:KeySchema中的属性数量与AttributeDefinitions中定义的属性数量不完全匹配 –

回答

0

问题: -

1)只有一个属性可以被定义为RANGE键。你有两个属性DateTimeTemperature定义为RANGE键

解决方案: -

如果您需要两个不同范围的键,你可以使用本地二级索引(LSI)。一张桌子只能有一个LSI。

LSI