不支持查询关键条件

时间:2019-05-20 22:31:43

标签: go amazon-dynamodb

我正在尝试使所有行都超过某个时间戳。

我也尝试在这种情况下使用“ GE”,“ LE”,“ GT”,但是出现语法错误。

我收到以下DynamoDB错误:

Internal Server Error [ValidationException: Query key condition not supported
    status code: 400,

我有下表

Table name  GroupsLambda3
Primary partition key   id (String)
Primary sort key    -
Point-in-time recovery  DISABLEDEnable
Encryption Type DEFAULTManage Encryption
KMS Master Key ARN  Not Applicable
Time to live attribute  DISABLEDManage TTL

然后我在上创建了二级索引

Name: unix_time-index
Status: Active
Type: GSI
Partition Key: unix_time (Number)
Sort Key: unix_time-index

最后我的Golang代码是:

    var cond string
    cond = "unix_time <= :v_unix_time"
    var projection string
    projection = "id, num_users, salesforce_campaign, unix_time"
    input := &dynamodb.QueryInput{
        TableName:              aws.String(table),
        IndexName:              aws.String("unix_time-index"),
        KeyConditionExpression: &cond,
        ProjectionExpression:   &projection,
        ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
            ":v_unix_time": {
                N: aws.String("1558130473454419"),
            },
        },

当我执行cond = "unix_time = :v_unix_time"时,代码有效,仅等于。

我希望所有行的unix_timestamp都较小。

1 个答案:

答案 0 :(得分:0)

您应该使用表达式生成器:

// todo: please check if value should be converted to another type in your case
keyCond := expression.Key("unix_time").LessThenEqual(expression.Value("1558130473454419"))
proj := expression.NamesList(expression.Name("id"), expression.Name("num_users"), expression.Name("salesforce_campaign"), expression.Name("unix_time"))

expr, err := expression.NewBuilder().
    WithKeyCondition(keyCond).
    WithProjection(proj).
    Build()
if err != nil {
    fmt.Println(err)
}

input := &dynamodb.QueryInput{
    ExpressionAttributeValues: expr.Values(),
    KeyConditionExpression:    expr.KeyCondition(),
    ProjectionExpression:      expr.Projection(),
    TableName:                 aws.String(table),
}