dongruidian3064 2018-08-17 03:31
浏览 119

Amazon DynamoDB如何在Golang中读取填充的数据库?

I have populated a local DynamoDB instance with some values from this JSON

[
    {
        "Id": 1,
        "Type": "fruit",
        "Name": "bananas",
        "Price": 0.25
    },
    {   
        "Id": 2,
        "Type": "fruit",
        "Name": "apples",
        "Price": 0.50
    },
    {
        "Id": 3,
        "Type": "fruit",
        "Name": "avocados",
        "Price": 1.25
    },
    {
        "Id": 4, 
        "Type": "vegetable",
        "Name": "carrots",
        "Price": 1.00
    } ]

And want to read, say all the items in this database. I believe Querying the database can do this, but in for my query input code I have

func main() {

    type Product struct {
        Id      int         `json:"Id"`
        Type    string      `json:"Type"`
        Name  string         `json:"Name"`
        Price float64      `json:"Price"`
    }

    config := &aws.Config{
        Region:   aws.String("us-west-2"),
        Endpoint: aws.String("http://localhost:8000"),
    }

    sess := session.Must(session.NewSession(config))

    svc := dynamodb.New(sess)


    input := &dynamodb.QueryInput{
        TableName:              aws.String("Products"),
        KeyConditions: map[string]*dynamodb.Condition{
            "Id": {
                ComparisonOperator: aws.String("EQ"),
                AttributeValueList: []*dynamodb.AttributeValue{
                    {
                        N: aws.String("1"),
                    },
                },

            },
        },

    }

    result, err := svc.Query(input)
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    var products []Product
    err = dynamodbattribute.UnmarshalListOfMaps(result.Items, &products)

    // print the response data
    for _, m := range products {
        fmt.Printf("Product: '%s' (%f)
", m.Name, m.Price)
    }
}

so far and it only outputs bananas as the since the query is trying to match id = 1 (Id is my hash primary key).

I tried to modify the keycondition to something like this instead

"Id": {
        ComparisonOperator: aws.String("BETWEEN"),
        AttributeValueList: []*dynamodb.AttributeValue{
            {
                N: aws.String("0"),
            },
            {
                N: aws.String("5"),
            },
        },
    },

in an attempt to read every item but it says "ValidationException: Query key condition not supported". Am I doing the query statement incorrectly? Or is there some better way of listing items in dynamoDB? (Like if I wanted to list everything with "Type" == "fruit" for example, then output the first 3 items in the database.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 delta降尺度方法,未来数据怎么降尺度
    • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
    • ¥15 高德地图点聚合中Marker的位置无法实时更新
    • ¥15 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错