├── README.md ├── .gitignore ├── main.go └── data ├── uploadForumData.go ├── uploadReplyData.go └── uploadProductCatalog.go /README.md: -------------------------------------------------------------------------------- 1 | # go-dynamodb-create-table-example 2 | Write data to a table on DynamoDB using golang 3 | 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio 3 | 4 | *.iml 5 | 6 | ## Directory-based project format: 7 | .idea/ 8 | # if you remove the above rule, at least ignore the following: 9 | 10 | # User-specific stuff: 11 | # .idea/workspace.xml 12 | # .idea/tasks.xml 13 | # .idea/dictionaries 14 | 15 | # Sensitive or high-churn files: 16 | # .idea/dataSources.ids 17 | # .idea/dataSources.xml 18 | # .idea/sqlDataSources.xml 19 | # .idea/dynamic.xml 20 | # .idea/uiDesigner.xml 21 | 22 | # Gradle: 23 | # .idea/gradle.xml 24 | # .idea/libraries 25 | 26 | # Mongo Explorer plugin: 27 | # .idea/mongoSettings.xml 28 | 29 | ## File-based project format: 30 | *.ipr 31 | *.iws 32 | 33 | ## Plugin-specific files: 34 | 35 | # IntelliJ 36 | /out/ 37 | 38 | # mpeltonen/sbt-idea plugin 39 | .idea_modules/ 40 | 41 | # JIRA plugin 42 | atlassian-ide-plugin.xml 43 | 44 | # Crashlytics plugin (for Android Studio and IntelliJ) 45 | com_crashlytics_export_strings.xml 46 | crashlytics.properties 47 | crashlytics-build.properties 48 | .DS_Store 49 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/aws/aws-sdk-go/aws" 5 | "github.com/aws/aws-sdk-go/service/dynamodb" 6 | "github.com/sinistersig/aws-practice-dynamodb-add-data/data" 7 | "log" 8 | ) 9 | 10 | func main() { 11 | config := &aws.Config{ 12 | Region: aws.String("us-east-1"), 13 | } 14 | 15 | svc := dynamodb.New(config) 16 | 17 | //Batch write to forums table 18 | resp, err := data.UploadForumData(svc) 19 | 20 | if err != nil { 21 | log.Println("An error occurred while writing to the Forums table") 22 | log.Println(err.Error()) 23 | } 24 | 25 | log.Println(resp) 26 | 27 | //Batch write to Reply Table 28 | resp, err = data.UploadReplyData(svc) 29 | 30 | if err != nil { 31 | log.Println("An error occurred while writing to the Reply table") 32 | log.Println(err.Error()) 33 | } 34 | 35 | log.Println(resp) 36 | 37 | //Upload product catalog 38 | resp, err = data.UploadProductCatalog(svc) 39 | 40 | if err != nil { 41 | log.Println("An error occurred while writing to the ProductCatalog table") 42 | log.Println(err.Error()) 43 | } 44 | 45 | log.Println(resp) 46 | 47 | } 48 | -------------------------------------------------------------------------------- /data/uploadForumData.go: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | import "github.com/aws/aws-sdk-go/service/dynamodb" 4 | import "github.com/aws/aws-sdk-go/aws" 5 | 6 | func UploadForumData(svc *dynamodb.DynamoDB) (*dynamodb.BatchWriteItemOutput, error) { 7 | params := &dynamodb.BatchWriteItemInput{ 8 | RequestItems: map[string][]*dynamodb.WriteRequest{ 9 | "Forum": { 10 | &dynamodb.WriteRequest{ 11 | PutRequest: &dynamodb.PutRequest{ 12 | Item: map[string]*dynamodb.AttributeValue{ 13 | "Name": { 14 | S: aws.String("Amazon DynamoDB"), 15 | }, 16 | "Category": { 17 | S: aws.String("Amazon Web Services"), 18 | }, 19 | "Threads": { 20 | N: aws.String("0"), 21 | }, 22 | "Messages": { 23 | N: aws.String("0"), 24 | }, 25 | "Views": { 26 | N: aws.String("1"), 27 | }, 28 | }, 29 | }, 30 | }, 31 | &dynamodb.WriteRequest{ 32 | PutRequest: &dynamodb.PutRequest{ 33 | Item: map[string]*dynamodb.AttributeValue{ 34 | "Name": { 35 | S: aws.String("Amazon S3"), 36 | }, 37 | "Category": { 38 | S: aws.String("Amazon Web Services"), 39 | }, 40 | "Threads": { 41 | N: aws.String("0"), 42 | }, 43 | }, 44 | }, 45 | }, 46 | }, 47 | }, 48 | } 49 | 50 | return svc.BatchWriteItem(params) 51 | } 52 | -------------------------------------------------------------------------------- /data/uploadReplyData.go: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | import ( 4 | "github.com/aws/aws-sdk-go/aws" 5 | "github.com/aws/aws-sdk-go/service/dynamodb" 6 | "time" 7 | ) 8 | 9 | const timeFormat = "2006-01-02 15:04:05" 10 | 11 | func UploadReplyData(svc *dynamodb.DynamoDB) (*dynamodb.BatchWriteItemOutput, error) { 12 | oneDayAgo := time.Now().AddDate(0, 0, -1).String() 13 | sevenDaysAgo := time.Now().AddDate(0, 0, -7).String() 14 | fourteenDaysAgo := time.Now().AddDate(0, 0, -14).String() 15 | twentyOneDaysAgo := time.Now().AddDate(0, 0, -21).String() 16 | 17 | params := &dynamodb.BatchWriteItemInput{ 18 | RequestItems: map[string][]*dynamodb.WriteRequest{ 19 | "Reply": { 20 | &dynamodb.WriteRequest{ 21 | PutRequest: &dynamodb.PutRequest{ 22 | Item: map[string]*dynamodb.AttributeValue{ 23 | "Id": { 24 | S: aws.String("Amazon DynamoDB#DynamoDB Thread 1"), 25 | }, 26 | "ReplyDateTime": { 27 | S: aws.String(fourteenDaysAgo), 28 | }, 29 | "Message": { 30 | S: aws.String("DynamoDB Thread 1 Reply 2 text"), 31 | }, 32 | "PostedBy": { 33 | S: aws.String("User B"), 34 | }, 35 | }, 36 | }, 37 | }, 38 | &dynamodb.WriteRequest{ 39 | PutRequest: &dynamodb.PutRequest{ 40 | Item: map[string]*dynamodb.AttributeValue{ 41 | "Id": { 42 | S: aws.String("Amazon DynamoDB#DynamoDB Thread 2"), 43 | }, 44 | "ReplyDateTime": { 45 | S: aws.String(twentyOneDaysAgo), 46 | }, 47 | "Message": { 48 | S: aws.String("DynamoDB Thread 2 Reply 3 text"), 49 | }, 50 | "PostedBy": { 51 | S: aws.String("User B"), 52 | }, 53 | }, 54 | }, 55 | }, 56 | &dynamodb.WriteRequest{ 57 | PutRequest: &dynamodb.PutRequest{ 58 | Item: map[string]*dynamodb.AttributeValue{ 59 | "Id": { 60 | S: aws.String("Amazon DynamoDB#DynamoDB Thread 2"), 61 | }, 62 | "ReplyDateTime": { 63 | S: aws.String(sevenDaysAgo), 64 | }, 65 | "Message": { 66 | S: aws.String("DynamoDB Thread 2 Reply 2 text"), 67 | }, 68 | "PostedBy": { 69 | S: aws.String("User A"), 70 | }, 71 | }, 72 | }, 73 | }, 74 | &dynamodb.WriteRequest{ 75 | PutRequest: &dynamodb.PutRequest{ 76 | Item: map[string]*dynamodb.AttributeValue{ 77 | "Id": { 78 | S: aws.String("Amazon DynamoDB#DynamoDB Thread 2"), 79 | }, 80 | "ReplyDateTime": { 81 | S: aws.String(oneDayAgo), 82 | }, 83 | "Message": { 84 | S: aws.String("DynamoDB Thread 2 Reply 1 text"), 85 | }, 86 | "PostedBy": { 87 | S: aws.String("User A"), 88 | }, 89 | }, 90 | }, 91 | }, 92 | }, 93 | }, 94 | } 95 | 96 | return svc.BatchWriteItem(params) 97 | } 98 | -------------------------------------------------------------------------------- /data/uploadProductCatalog.go: -------------------------------------------------------------------------------- 1 | package data 2 | 3 | import ( 4 | "github.com/aws/aws-sdk-go/aws" 5 | "github.com/aws/aws-sdk-go/service/dynamodb" 6 | ) 7 | 8 | func UploadProductCatalog(svc *dynamodb.DynamoDB) (*dynamodb.BatchWriteItemOutput, error) { 9 | params := &dynamodb.BatchWriteItemInput{ 10 | RequestItems: map[string][]*dynamodb.WriteRequest{ 11 | "ProductCatalog": { 12 | &dynamodb.WriteRequest{ 13 | PutRequest: &dynamodb.PutRequest{ 14 | Item: map[string]*dynamodb.AttributeValue{ 15 | 16 | "Id": { 17 | N: aws.String("1101"), 18 | }, 19 | "Title": { 20 | S: aws.String("Book 101 Title"), 21 | }, 22 | "ISBN": { 23 | S: aws.String("111-1111111111"), 24 | }, 25 | "Authors": { 26 | SS: []*string{ 27 | aws.String("Author1"), 28 | }, 29 | }, 30 | "Price": { 31 | N: aws.String("2"), 32 | }, 33 | "Dimensions": { 34 | S: aws.String("8.5 x 11.0 x 0.5"), 35 | }, 36 | "PageCount": { 37 | N: aws.String("500"), 38 | }, 39 | "InPublication": { 40 | N: aws.String("1"), 41 | }, 42 | "ProductCategory": { 43 | S: aws.String("Book"), 44 | }, 45 | }, 46 | }, 47 | }, 48 | &dynamodb.WriteRequest{ 49 | PutRequest: &dynamodb.PutRequest{ 50 | Item: map[string]*dynamodb.AttributeValue{ 51 | 52 | "Id": { 53 | N: aws.String("102"), 54 | }, 55 | "Title": { 56 | S: aws.String("Book 102 Title"), 57 | }, 58 | "ISBN": { 59 | S: aws.String("222-2222222222"), 60 | }, 61 | "Authors": { 62 | SS: []*string{ 63 | aws.String("Author1"), 64 | aws.String("Author2"), 65 | }, 66 | }, 67 | "Price": { 68 | N: aws.String("20"), 69 | }, 70 | "Dimensions": { 71 | S: aws.String("8.5 x 11.0 x 0.8"), 72 | }, 73 | "PageCount": { 74 | N: aws.String("600"), 75 | }, 76 | "InPublication": { 77 | N: aws.String("1"), 78 | }, 79 | "ProductCategory": { 80 | S: aws.String("Book"), 81 | }, 82 | }, 83 | }, 84 | }, 85 | &dynamodb.WriteRequest{ 86 | PutRequest: &dynamodb.PutRequest{ 87 | Item: map[string]*dynamodb.AttributeValue{ 88 | 89 | "Id": { 90 | N: aws.String("103"), 91 | }, 92 | "Title": { 93 | S: aws.String("Book 103 Title"), 94 | }, 95 | "ISBN": { 96 | S: aws.String("333-3333333333"), 97 | }, 98 | "Authors": { 99 | SS: []*string{ 100 | aws.String("Author1"), 101 | aws.String("Author2"), 102 | }, 103 | }, 104 | "Price": { 105 | N: aws.String("2000"), 106 | }, 107 | "Dimensions": { 108 | S: aws.String("8.5 x 11.0 x 1.5"), 109 | }, 110 | "PageCount": { 111 | N: aws.String("600"), 112 | }, 113 | "InPublication": { 114 | N: aws.String("0"), 115 | }, 116 | "ProductCategory": { 117 | S: aws.String("Book"), 118 | }, 119 | }, 120 | }, 121 | }, 122 | &dynamodb.WriteRequest{ 123 | PutRequest: &dynamodb.PutRequest{ 124 | Item: map[string]*dynamodb.AttributeValue{ 125 | 126 | "Id": { 127 | N: aws.String("201"), 128 | }, 129 | "Title": { 130 | S: aws.String("18-Bike-201"), 131 | }, 132 | "Description": { 133 | S: aws.String("201 Description"), 134 | }, 135 | "BicycleType": { 136 | S: aws.String("Road"), 137 | }, 138 | "Brand": { 139 | S: aws.String("Mountain A"), 140 | }, 141 | "Price": { 142 | N: aws.String("100"), 143 | }, 144 | "Gender": { 145 | S: aws.String("M"), 146 | }, 147 | "Color": { 148 | SS: []*string{ 149 | aws.String("Red"), 150 | aws.String("Black"), 151 | }, 152 | }, 153 | "ProductCategory": { 154 | S: aws.String("Bicycle"), 155 | }, 156 | }, 157 | }, 158 | }, 159 | &dynamodb.WriteRequest{ 160 | PutRequest: &dynamodb.PutRequest{ 161 | Item: map[string]*dynamodb.AttributeValue{ 162 | 163 | "Id": { 164 | N: aws.String("202"), 165 | }, 166 | "Title": { 167 | S: aws.String("21-Bike-202"), 168 | }, 169 | "Description": { 170 | S: aws.String("202 Description"), 171 | }, 172 | "BicycleType": { 173 | S: aws.String("Road"), 174 | }, 175 | "Brand": { 176 | S: aws.String("Brand-Company A"), 177 | }, 178 | "Price": { 179 | N: aws.String("200"), 180 | }, 181 | "Gender": { 182 | S: aws.String("M"), 183 | }, 184 | "Color": { 185 | SS: []*string{ 186 | aws.String("Green"), 187 | aws.String("Black"), 188 | }, 189 | }, 190 | "ProductCategory": { 191 | S: aws.String("Bicycle"), 192 | }, 193 | }, 194 | }, 195 | }, 196 | &dynamodb.WriteRequest{ 197 | PutRequest: &dynamodb.PutRequest{ 198 | Item: map[string]*dynamodb.AttributeValue{ 199 | "Id": { 200 | N: aws.String("203"), 201 | }, 202 | "Title": { 203 | S: aws.String("19-Bike-203"), 204 | }, 205 | "Description": { 206 | S: aws.String("203 Description"), 207 | }, 208 | "BicycleType": { 209 | S: aws.String("Road"), 210 | }, 211 | "Brand": { 212 | S: aws.String("Brand-Company B"), 213 | }, 214 | "Price": { 215 | N: aws.String("300"), 216 | }, 217 | "Gender": { 218 | S: aws.String("W"), 219 | }, 220 | "Color": { 221 | SS: []*string{ 222 | aws.String("Green"), 223 | aws.String("Black"), 224 | aws.String("Red"), 225 | }, 226 | }, 227 | "ProductCategory": { 228 | S: aws.String("Bicycle"), 229 | }, 230 | }, 231 | }, 232 | }, 233 | &dynamodb.WriteRequest{ 234 | PutRequest: &dynamodb.PutRequest{ 235 | Item: map[string]*dynamodb.AttributeValue{ 236 | 237 | "Id": { 238 | N: aws.String("205"), 239 | }, 240 | "Title": { 241 | S: aws.String("20-Bike-205"), 242 | }, 243 | "Description": { 244 | S: aws.String("205 Description"), 245 | }, 246 | "BicycleType": { 247 | S: aws.String("Hybrid"), 248 | }, 249 | "Brand": { 250 | S: aws.String("Brand-Company C"), 251 | }, 252 | "Price": { 253 | N: aws.String("500"), 254 | }, 255 | "Gender": { 256 | S: aws.String("B"), 257 | }, 258 | "Color": { 259 | SS: []*string{ 260 | aws.String("Red"), 261 | aws.String("Black"), 262 | }, 263 | }, 264 | "ProductCategory": { 265 | S: aws.String("Bicycle"), 266 | }, 267 | }, 268 | }, 269 | }, 270 | }, 271 | }, 272 | } 273 | 274 | return svc.BatchWriteItem(params) 275 | } 276 | --------------------------------------------------------------------------------