2017-05-30 93 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