26

I am using aws-sdk-go library for DynamoDb connectivity in Golang.

My DynamoDb table have a Partition key DeviceId (String) and a Sort Key Time (Number). How can I write GetItemInput to get all data with a specific DeviceId?

params := &dynamodb.GetItemInput{

    Key:    map[string]*dynamodb.AttributeValue {
        "DeviceId": {
            S: aws.String("item_1"),
        },
    },
    ExpressionAttributeNames: map[string]*string{
        "DeviceId": "DeviceId",
    },
    TableName:  aws.String("DbName"), 
}

list, err := svc.GetItem(params)
mkral
  • 4,065
  • 4
  • 28
  • 53

3 Answers3

28

You have to use Query or Scan operation, this is a simple example but you can read more on Amazon documentation here

In particular, Query operation

A Query operation finds items in a table or a secondary index using only primary key attribute values

var queryInput = &dynamodb.QueryInput{
    TableName: aws.String(dynamoRestDataTableName),
    KeyConditions: map[string]*dynamodb.Condition{
        "DeviceId": {
            ComparisonOperator: aws.String("EQ"),
            AttributeValueList: []*dynamodb.AttributeValue{
                {
                    S: aws.String("aDeviceId"),
                },
            },
        },
    },
}

var resp, err = dynamoSvc.Query(queryInput)
if err != nil {
    return nil, err
}
Alessio
  • 2,018
  • 25
  • 26
  • 1
    Thanks Bro. Can you please tell me how to access data from the QueryOutput? Suppose if I want to access last entry in the queryoutput how can I do that? –  Aug 12 '16 at 04:35
  • Is this the only way to do it? `*resp.Items[*resp.Count - int64(1)]["attribute"].S` –  Aug 12 '16 at 06:45
  • 1
    This looks perfect Alessio except it should be `S: aws.String("aDeviceId"),` – fIwJlxSzApHEZIl Dec 08 '17 at 22:19
  • @user6681013 if we look in `api.go` we can see that the `QueryOutput` struct has the definition `Items []map[string]*AttributeValue` so the results are returned as an array of maps. each map represents one row of values and each key in the map represents one column name in the table. – fIwJlxSzApHEZIl Dec 08 '17 at 22:21
2

Query operation can be used in that case
Following is one generic example for the same

compositeKey := entity.GetPrimaryKey(inputVar)
expressionAttributeValues := map[string]*dynamodb.AttributeValue{
    ":v1": {
        S: aws.String(compositeKey.PartitionKey.Data.(string)),
    },
}
queryInput := dynamodb.QueryInput{
    TableName:                 &d.TableName,
    KeyConditionExpression:    aws.String("id = :v1"),
    ExpressionAttributeValues: expressionAttributeValues,
}
queryOutput, err := d.DdbSession.Query(&queryInput)
if err != nil {
    log.Error("error in fetching records ", err)
    return nil, err
}

// unmarshal the query output - items to interface

err = dynamodbattribute.UnmarshalListOfMaps(queryOutput.Items, &yourInterface)
Deep Nirmal
  • 1,141
  • 1
  • 15
  • 14
0

You can use getItem in JavaScript SDK v2 like this

const params = {
    TableName: 'tableName',
    Key: {
        id: { S: id },
    },
};

const result = await dynamoDb.getItem(params).promise();
if (result.Item === undefined) {
    throw new Error('not found');
}
const item = AWS.DynamoDB.Converter.unmarshall(result.Item)

If JavaScript SDK can do this, I assume golang SDK can also. If not, that means AWS doesn't take all languages equally ?

Linden X. Quan
  • 584
  • 5
  • 18