2016-06-15 90 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