Skip to content

Instantly share code, notes, and snippets.

@michimani
Created May 18, 2020 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michimani/2e064cab0fd51b4c8844a64c5c8ec400 to your computer and use it in GitHub Desktop.
Save michimani/2e064cab0fd51b4c8844a64c5c8ec400 to your computer and use it in GitHub Desktop.
Sample script to scan DynamoDB table via AWS SDK for Go.
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
// Thread is a struct of a Thread item
type Thread struct {
ForumName string `dynamodbav:"ForumName" json:"forum_name"`
Subject string `dynamodbav:"Subject" json:"subject"`
Answered int `dynamodbav:"Answered" json:"answered"`
LastPostedBy string `dynamodbav:"LastPostedBy" json:"last_posted_by"`
LastPostedDateTime string `dynamodbav:"LastPostedDateTime" json:"last_posted_date_time"`
Message string `dynamodbav:"Message" json:"message"`
Replies int `dynamodbav:"Replies" json:"replies"`
Tags []string `dynamodbav:"Tags" json:"tags"`
Views int `dynamodbav:"Views" json:"views"`
}
// ThreadWithSomeAttr is a struct of a Thread item with some attributes.
type ThreadWithSomeAttr struct {
ForumName string `dynamodbav:"ForumName" json:"forum_name"`
Subject string `dynamodbav:"Subject" json:"subject"`
}
var tableName string = "Thread"
var region string = "ap-northeast-1"
var db = dynamodb.New(session.New(), &aws.Config{
Region: aws.String(region),
})
// Scan is a function to scan table.
func Scan() []Thread {
var threads []Thread = []Thread{}
scanOut, err := db.Scan(&dynamodb.ScanInput{
TableName: aws.String(tableName),
})
if err != nil {
fmt.Println(err.Error())
return threads
}
for _, scanedThread := range scanOut.Items {
var threadTmp Thread
_ = dynamodbattribute.UnmarshalMap(scanedThread, &threadTmp)
threads = append(threads, threadTmp)
}
return threads
}
// ScanSomeAttr is a function to scan table with some attributes.
func ScanSomeAttr() []ThreadWithSomeAttr {
var threads []ThreadWithSomeAttr = []ThreadWithSomeAttr{}
scanOut, err := db.Scan(&dynamodb.ScanInput{
TableName: aws.String(tableName),
ExpressionAttributeNames: map[string]*string{
"#FNAME": aws.String("ForumName"),
"#SUBJ": aws.String("Subject"),
},
ProjectionExpression: aws.String("#FNAME, #SUBJ"),
})
if err != nil {
fmt.Println(err.Error())
return threads
}
for _, scanedThread := range scanOut.Items {
var threadTmp ThreadWithSomeAttr
_ = dynamodbattribute.UnmarshalMap(scanedThread, &threadTmp)
threads = append(threads, threadTmp)
}
return threads
}
// ThreadsToJSONString is a function to convert Thread object to JSON string.
func ThreadsToJSONString(threads interface{}) string {
j, err := json.Marshal(threads)
if err != nil {
fmt.Println(err.Error())
return ""
}
var buf bytes.Buffer
jerr := json.Indent(&buf, j, "", " ")
if jerr != nil {
fmt.Println(jerr.Error())
return ""
}
return buf.String()
}
func main() {
fmt.Println("Scan with all attributes.")
allAttrRes := Scan()
fmt.Println(ThreadsToJSONString(allAttrRes))
fmt.Println("\nScan with some attributes.")
someAttrRes := ScanSomeAttr()
fmt.Println(ThreadsToJSONString(someAttrRes))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment