scanItems.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // +build example
  2. package main
  3. import (
  4. "flag"
  5. "fmt"
  6. "os"
  7. "github.com/aws/aws-sdk-go/aws"
  8. "github.com/aws/aws-sdk-go/aws/session"
  9. "github.com/aws/aws-sdk-go/service/dynamodb"
  10. "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
  11. )
  12. func exitWithError(err error) {
  13. fmt.Fprintln(os.Stderr, err)
  14. os.Exit(1)
  15. }
  16. func main() {
  17. cfg := Config{}
  18. if err := cfg.Load(); err != nil {
  19. exitWithError(fmt.Errorf("failed to load config, %v", err))
  20. }
  21. // Create the config specifying the Region for the DynamoDB table.
  22. // If Config.Region is not set the region must come from the shared
  23. // config or AWS_REGION environment variable.
  24. awscfg := &aws.Config{}
  25. if len(cfg.Region) > 0 {
  26. awscfg.WithRegion(cfg.Region)
  27. }
  28. // Create the session that the DynamoDB service will use.
  29. sess, err := session.NewSession(awscfg)
  30. if err != nil {
  31. exitWithError(fmt.Errorf("failed to create session, %v", err))
  32. }
  33. // Create the DynamoDB service client to make the query request with.
  34. svc := dynamodb.New(sess)
  35. // Build the query input parameters
  36. params := &dynamodb.ScanInput{
  37. TableName: aws.String(cfg.Table),
  38. }
  39. if cfg.Limit > 0 {
  40. params.Limit = aws.Int64(cfg.Limit)
  41. }
  42. // Make the DynamoDB Query API call
  43. result, err := svc.Scan(params)
  44. if err != nil {
  45. exitWithError(fmt.Errorf("failed to make Query API call, %v", err))
  46. }
  47. items := []Item{}
  48. // Unmarshal the Items field in the result value to the Item Go type.
  49. err = dynamodbattribute.UnmarshalListOfMaps(result.Items, &items)
  50. if err != nil {
  51. exitWithError(fmt.Errorf("failed to unmarshal Query result items, %v", err))
  52. }
  53. // Print out the items returned
  54. for i, item := range items {
  55. fmt.Printf("%d: Key: %d, Desc: %s\n", i, item.Key, item.Desc)
  56. fmt.Printf("\tNum Data Values: %d\n", len(item.Data))
  57. for k, v := range item.Data {
  58. fmt.Printf("\t- %q: %v\n", k, v)
  59. }
  60. }
  61. }
  62. type Item struct {
  63. Key int
  64. Desc string
  65. Data map[string]interface{}
  66. }
  67. type Config struct {
  68. Table string // required
  69. Region string // optional
  70. Limit int64 // optional
  71. }
  72. func (c *Config) Load() error {
  73. flag.Int64Var(&c.Limit, "limit", 0, "Limit is the max items to be returned, 0 is no limit")
  74. flag.StringVar(&c.Table, "table", "", "Table to Query on")
  75. flag.StringVar(&c.Region, "region", "", "AWS Region the table is in")
  76. flag.Parse()
  77. if len(c.Table) == 0 {
  78. flag.PrintDefaults()
  79. return fmt.Errorf("table name is required.")
  80. }
  81. return nil
  82. }