DynamoDB查询混淆

时间:2016-06-15 14:51:39

标签: amazon-dynamodb

我有以下用于DynamoDB(C#)的表创建代码:

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的所有记录&lt;&gt; “内部”?

此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

我想我真的不明白该怎么做。

1 个答案:

答案 0 :(得分:0)

根据您的表格结构,您无法在桌面上执行查询,但您必须扫描我们需要避免的内容。< / p>

要执行查询,您需要修改某些内容

1)使用字段 CLI_RID 添加全局二级索引(GSI)作为哈希

2)现在,您通过传递CLI_RID并添加条件&lt;&gt;的查询过滤器来查询GSI你的价值

以下是参考link

编辑:您的主表结构将无需更改,但您需要再添加一个带有Hash密钥的GSI作为CLI_RID和项目所需的表属性。

现在您需要使用散列键作为CLI_RID来查询您的GSI而不是表,您不需要在此处传递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" }}}
};

希望有所帮助