无法通过goamz dynamodb使GetItem正常工作

时间:2016-11-02 19:04:09

标签: go amazon-dynamodb

我正在关注this教程,并使用Python设置表,我可以使用Python获取项目,但不能使用golang。我做错了什么?

我这样做了我的表(几乎从教程中复制和粘贴):

from __future__ import print_function # Python 2/3 compatibility
import boto3

dynamodb = boto3.resource('dynamodb', region_name='us-west-2')

table = dynamodb.create_table(
    TableName='Movies',
    KeySchema=[
        {
            'AttributeName': 'year',
            'KeyType': 'HASH'  #Partition key
        },
        {
            'AttributeName': 'title',
            'KeyType': 'RANGE'  #Sort key
        }
    ],
    AttributeDefinitions=[
        {
            'AttributeName': 'year',
            'AttributeType': 'N'
        },
        {
            'AttributeName': 'title',
            'AttributeType': 'S'
        },

    ],
    ProvisionedThroughput={
        'ReadCapacityUnits': 10,
        'WriteCapacityUnits': 10
    }
)

我加载了这样的数据(在Python中):

my_item = {"year": 1999, "title": "MyMovie", "info": {"Plot": "DynamoDB"}}
table.put_item(Item=my_item)

当我读取数据时(在Python中):

response = table.get_item(Key={"year":1999,
                        "title":"MyMovie",
                    }
    )
print response

我的输出是:

{u'Item': {u'info': {u'Plot': u'DynamoDB'}, u'year': Decimal('1999'), u'title': u'MyMovie'}, 'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': 'C8I2Q46K06T030INBCFN35RELVVV4KQNSO5AEMVJF66Q9ASUAAJG', 'HTTPHeaders': {'x-amzn-requestid': 'C8I2Q46K06T030INBCFN35RELVVV4KQNSO5AEMVJF66Q9ASUAAJG', 'content-length': '93', 'server': 'Server', 'connection': 'keep-alive', 'x-amz-crc32': '4025342458', 'date': 'Wed, 02 Nov 2016 18:20:14 GMT', 'content-type': 'application/x-amz-json-1.0'}}}

我的golang代码:

package main

import (
    "fmt"
    "github.com/goamz/goamz/aws"
    "github.com/goamz/goamz/dynamodb"
)


func main() {

    access_key := "MyAccessKey"
    secret_key := "MySecretKey"

    auth := aws.Auth{AccessKey: access_key, SecretKey: secret_key}

    server := &dynamodb.Server{Auth: auth, Region: aws.USWest2}

    desc_ptr, err := server.DescribeTable("Movies")

    if err != nil {
        fmt.Println(err)
    }

    primary_key, err := desc_ptr.BuildPrimaryKey()

    if err != nil {
        fmt.Println(err)
    }

    table := server.NewTable("Movies", primary_key)

    my_key := dynamodb.Key{HashKey: "1999", RangeKey: "MyMovie"}

    result, err := table.GetItem(&my_key)

    if err != nil {
        fmt.Println(err)
    }

    fmt.Println("Printing Result")

    for k, v := range(result) {
        fmt.Println(k, *v)
    }
}

结果:

Printing Result
title {S title MyMovie [] }
year {N year 1999 [] }

这除了我提供的titleyear之外的所有内容。我错过了什么?

编辑:

我的go版本:

go version go1.7.3 linux/amd64

我不知道我拥有的goamz版本,但我非常确定自己是主人。

2 个答案:

答案 0 :(得分:1)

我认为它可能是 goamz 库中的错误。函数parseAttribute具有解析不同数据类型的逻辑。但是,它看起来似乎不适用于MAP,LIST,BOOL等数据类型。

根据我的观察,String,Number和SS数据类型工作正常。如果您可以添加非键字符串属性并执行该程序。您应该在结果中获取该属性。

goamz item go source code for reference

答案 1 :(得分:1)

我(OP)最终使用.. aws-sdk-go

它运行正常,我使用的代码:

package main

import (
    "fmt"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/endpoints"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/dynamodb"
)

func main() {

    sess := session.Must(session.NewSession(&aws.Config{
        Region: aws.String(endpoints.UsWest2RegionID),
    }))

    svc := dynamodb.New(sess)

    input := &dynamodb.GetItemInput{
        Key: map[string]*dynamodb.AttributeValue{
            "year": {
                N: aws.String("1999"),
            },
            "title": {
                S: aws.String("MyMovie"),
            },
        },
        TableName: aws.String("Movies"),
    }

    result, err := svc.GetItem(input)

    fmt.Println(result)
    fmt.Println(err)
}