ds211107 2019-04-30 09:07
浏览 84

如何使用Golang过滤器构建器过滤空值?

Carrying on from How to filter on NULL? I am struggling to replicate the Go code to filter for null values of a particular column:

import (
        "github.com/apex/log"
        "github.com/aws/aws-sdk-go-v2/aws"
        "github.com/aws/aws-sdk-go-v2/aws/endpoints"
        "github.com/aws/aws-sdk-go-v2/aws/external"
        "github.com/aws/aws-sdk-go-v2/service/dynamodb"
        "github.com/aws/aws-sdk-go-v2/service/dynamodb/expression"
)

// aws --profile dd dynamodb scan --table-name plocal2 --filter-expression 'paymentmethod = :null' --expression-attribute-values '{":null" :{"NULL":true}}'

var table = "plocal2"

func main() {
        cfg, err := external.LoadDefaultAWSConfig(external.WithSharedConfigProfile("dd"))
        if err != nil {
                log.WithError(err).Fatal("setting up credentials")
                return
        }
        cfg.Region = endpoints.ApSoutheast1RegionID
        db := dynamodb.New(cfg)
        filt := expression.Name("paymentmethod").Equal(expression.Value(expression.Null))
        expr, err := expression.NewBuilder().
                WithFilter(filt).
                Build()
        if err != nil {
                log.WithError(err).Error("failed to build expression")
                return
        }
        scanReq := db.ScanRequest(&dynamodb.ScanInput{
                ExpressionAttributeValues: expr.Values(),
                FilterExpression:          expr.Filter(),
                TableName:                 aws.String(table),
        })
        result, err := scanReq.Send()
        if err != nil {
                log.WithError(err).Error("failed to query table")
                return
        }
        log.Infof("%d", len(result.Items))
}

The error is:

2019/04/30 16:59:54 error failed to query table     error=ValidationException: Invalid FilterExpression: An expression attribute name used in the document path
 is not defined; attribute name: #0
        status code: 400, request id: 8CFTOAGVB7M1IM3DC6BFPEVLUNVV4KQNSO5AEMVJF66Q9ASUAAJG

Any tips how to debug this or where I am going wrong using the expression builder?

Really wish there were examples, since filtering on NULL should be a pretty common use case to me if one is using MarshalMap since empty strings becomes NULLs!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?