AWS,dynamodb 以条件 gsi 启动

deg*_*egr 4 amazon-web-services amazon-dynamodb aws-sdk aws-sdk-nodejs

我需要执行这样的查询: select * from table where抽样日期如“2020-05-%”

为此,我呼吁

 db.query({
        TableName: "Tubes",
        Select: "ALL_ATTRIBUTES",
        IndexName: "sampling_date_idx",
        KeyConditionExpression: " sampling_date > :sampling_date ",
        ExpressionAttributeValues:{ ':sampling_date': {'S': '2020-05-'}}
    }, function(error: AWSError, data: QueryOutput){
            console.log(error, data);
        })
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息:

 {"errorType":"Error","errorMessage":"{\"message\":\"Query key condition not supported\",\"code\":\"ValidationException\",
Run Code Online (Sandbox Code Playgroud)

我的桌子:

this.tubes = new dynamodb.Table(this, "tubes", {
      tableName: "Tubes",
      billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
      partitionKey: {
        name: "id",
        type: dynamodb.AttributeType.STRING
      },
      pointInTimeRecovery: true,
      removalPolicy: cdk.RemovalPolicy.RETAIN
    });
    this.tubes.addGlobalSecondaryIndex({
      indexName: "sampling_date_idx",
      sortKey: {
        name: 'sampling_date_srt',
        type: AttributeType.STRING
      },
      partitionKey: {
        name: "sampling_date",
        type: AttributeType.STRING,
      },
    })
Run Code Online (Sandbox Code Playgroud)

ch_*_*h_g 6

我认为您当前的代码中有两个问题 -

  1. 在 KeyConditionExpression 中,单个分区键值必须存在相等条件。在您的情况下,它必须包含“sampling_date = :sampling_date”。

请阅读“KeyConditionExpression”部分 - https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html

简而言之,您只能对分区键执行相等性测试。

  1. 我不确定你使用哪种语言。我怀疑您的 ExpressionAttributeValues 语法不正确。

AWS 文档中给出的语法是 -

"ExpressionAttributeValues": { 
      "string" : { 
         "B": blob,
         "BOOL": boolean,
         "BS": [ blob ],
         "L": [ 
            "AttributeValue"
         ],
         "M": { 
            "string" : "AttributeValue"
         },
         "N": "string",
         "NS": [ "string" ],
         "NULL": boolean,
         "S": "string",
         "SS": [ "string" ]
      }
   }
Run Code Online (Sandbox Code Playgroud)

在你的情况下,它可能是这样的 -

"ExpressionAttributeValues": {
        ":sampling_date": {"S": "2020-05-01"}
    }
Run Code Online (Sandbox Code Playgroud)

我的经验是 C#,可能是这样的 -

ExpressionAttributeValues = new Dictionary<string, AttributeValue>()
                {
                    { ":sampling_date", new AttributeValue{S = "2005-05-01"} }
                }
Run Code Online (Sandbox Code Playgroud)

为了解决您的问题,您可能需要使用另一个属性作为索引的分区键。Sample_date 只能用作排序键。