2016-06-15 127 views
0

我有DynamoDB(C#)下表創建代碼:DynamoDB查詢混亂

client.CreateTable(new CreateTableRequest 
{ 
    TableName = tableName, 
    ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 20, WriteCapacityUnits = 10 }, 
    KeySchema = new List<KeySchemaElement> 
     { 
      new KeySchemaElement 
      { 
       AttributeName = "RID", 
       KeyType = KeyType.HASH 
      } 
     } 
    , 
    AttributeDefinitions = new List<AttributeDefinition> 
     { 
      new AttributeDefinition { 
       AttributeName = "RID", 
       AttributeType = ScalarAttributeType.N 
      } 
     } 
}); 

是被填充到這個表中的數據來源於此JSON:

[ 
{"RID": 208649, "CLI_RID": 935476, "PRT_DT": "VAL_AA", "DISTR": "INTERNAL"}, 
{"RID": 217427, "CLI_RID": 1009561, "PRT_DT": "VAL_BB", "DISTR": "INTERNAL", "STATE": "VAL_BD"}, 
{"RID": 223331, "CLI_RID": 1325477, "PRT_DT": "VAL_CB", "DISTR": "", "STATE": "VAL_CD", "FNAME": "VAL_CE", "START": "VAL_CF"}, 
{"RID": 227717, "CLI_RID": 1023478, "PRT_DT": "VAL_DB", "DISTR": "", "STATE": "VAL_DD"} 
{"RID": 217462, "CLI_RID": 1009561, "PRT_DT": "VAL_BB", "DISTR": "", "STATE": "VAL_BD"}, 
{"RID": 218679, "CLI_RID": 1009561, "PRT_DT": "VAL_AA", "DISTR": "INTERNAL"}, 
{"RID": 222376, "CLI_RID": 1263978, "PRT_DT": "VAL_DB", "DISTR": "", "STATE": "VAL_DD"} 
] 

我怎麼查詢或者在「CLI_RID」列和「DISTR」列中包含1009561「」的所有記錄過濾「INTERNAL」?

此DynamoDB表中將有大約15 mil的記錄。

我的表是爲這個查詢/過濾器正確定義的嗎?

更新表的創建:

// CLI_RIDIndex 
var cli_ridIndex = new GlobalSecondaryIndex 
{ 
    IndexName = "cli_ridIndex", 
    ProvisionedThroughput = new ProvisionedThroughput 
    { 
     ReadCapacityUnits = 20, 
     WriteCapacityUnits = 10 
    }, 
    KeySchema = { 
     new KeySchemaElement 
     { 
      AttributeName = "CLI_RID", KeyType = "HASH" 
     } 
    }, 
    Projection = new Projection { ProjectionType = "ALL" } 
}; 


client.CreateTable(new CreateTableRequest 
{ 
    TableName = tableName, 
    ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 20, WriteCapacityUnits = 10 }, 
    KeySchema = new List<KeySchemaElement> 
     { 
      new KeySchemaElement 
      { 
       AttributeName = "RID", 
       KeyType = KeyType.HASH // Partiton Key (Unique) 
      }, 
      new KeySchemaElement 
      { 
       AttributeName = "CLI_RID", 
       KeyType = KeyType.RANGE // Sort Key 
      } 
     } 
    , 
    AttributeDefinitions = new List<AttributeDefinition> 
     { 
      new AttributeDefinition { 
       AttributeName = "RID", 
       AttributeType = ScalarAttributeType.N 
      }, 
      new AttributeDefinition { 
       AttributeName = "CLI_RID", 
       AttributeType = ScalarAttributeType.N 
      } 
     }, 
    GlobalSecondaryIndexes = { cli_ridIndex } 
}); 

但試圖查詢它,

var request = new QueryRequest 
{ 
    TableName = "TNAArchive", 
    KeyConditionExpression = "CLI_RID = :v_cli_rid", 
    ExpressionAttributeValues = new Dictionary<string, AttributeValue> { 
     {":v_cli_rid", new AttributeValue { S = "905466" }}} 
}; 

var response = client.Query(request); 

我得到這個錯誤時:

Query condition missed key schema element: RID 

我想我是不是真的瞭解如何做到這一點。

回答

0

根據你的表結構,你將無法對錶執行查詢,但你必須掃描這是我們需要避免的。

通過傳遞CLI_RID進行查詢,你需要修改某些事情

1)添加全局二級索引(GSI)與現場CLI_RID哈希

2)現在您查詢GSI,並添加查詢過濾條件<>您的價值

以下是參考link

編輯:您的主表結構將不會改變,但您需要添加一個帶有Hash密鑰的GSI作爲CLI_RID和項目所需的表屬性。

現在您需要查詢您的GSI而不是使用散列鍵作爲CLI_RID的表,而無需在此處傳遞RID。 here is the link on how to add GSI on table.

如果CLI_RID在主表中不存在,那麼該記錄將不會反映在GSI中,因此不必擔心。

編輯2:只需在查詢時添加(IndexName = NameOFYourIndex)屬性,一切都應該工作。

var request = new QueryRequest 
{ 
    TableName = "TNAArchive", 
    IndexName = "NameOfYourIndex", 
    KeyConditionExpression = "CLI_RID = :v_cli_rid", 
    ExpressionAttributeValues = new Dictionary<string, AttributeValue> { 
     {":v_cli_rid", new AttributeValue { S = "905466" }}} 
}; 

希望幫助

+0

請問我還必須包括在查詢中的RID值?你能告訴我使用的結構嗎?有些記錄可能沒有CLI_RID值。 – MB34

+0

@ MB34我已經更新了答案。 –

+0

在您提供的鏈接示例中,我沒有看到二級索引的創建在哪裏定義了索引所在的列。 – MB34