Reputation: 86266
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.
Upvotes: 2
Views: 561
Reputation: 86266
I (the OP) ended up using .. aws-sdk-go
And it works fine, the code I used:
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)
}
Upvotes: 1
Reputation: 39206
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
Upvotes: 1