douningzhi1991 2016-11-02 19:04
浏览 42
已采纳

不能使getItem与goamz dynamodb一起正常工作

I was following this tutorial, and set up the table using Python, and I can get item with Python but not with golang. What am I doing wrong?

I made my table like this (almost copy and paste from the tutorial):

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
    }
)

I loaded the data like this (in Python):

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

And when I read data (in Python):

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

My output is:

{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'}}}

My golang code:

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)
    }
}

Results in:

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

This is missing everything besides title and year that I provide. What am I missing?

Edit:

My go version:

go version go1.7.3 linux/amd64

I don't know what goamz version I have, but I am very sure that I am on the tip of master.

  • 写回答

2条回答 默认 最新

  • drq9991 2016-11-04 09:59
    关注

    I believe it could be a bug in the goamz library. The function parseAttribute has logic to parse the different data types. However, it looks like it doesn't seems to be working for data types MAP, LIST, BOOL etc.

    Per my observation, String, Number and SS data types are working fine. If you can add a non-key string attribute and execute the program. You should get the attribute in the result.

    goamz item go source code for reference

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示