/
main.go
185 lines (160 loc) · 4.18 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"log"
"time"
)
type Record struct {
Key string
RemoteID string
OtherData map[string]int
Timestamp int64
}
type NginxLog struct {
Id string
Time string
BodyBytesSent string
BytesSent string
ForWardedFor string
QueryString string
Referer string
RemoteAddr string
RequestLength string
RequestMethod string
RequestTime string
RequestUri string
Status string
Tag string
Useragent string
}
func main() {
//testTableDelete()
//chack table status
//testTableCreate()
//chack table status
//if creating,wait for active
svc := dynamodb.New(&aws.Config{Region: aws.String("ap-northeast-1")})
/*
scanParams := &dynamodb.ScanInput{
TableName:aws.String("access_log_range"),
AttributesToGet:[]*string{
aws.String("id"),
aws.String("time"),
aws.String("body_bytes_sent"),
aws.String("bytes_sent"),
aws.String("forwardedfor"),
aws.String("query_string"),
aws.String("referer"),
aws.String("remote_addr"),
aws.String("request_length"),
aws.String("request_method"),
aws.String("request_time"),
aws.String("request_uri"),
aws.String("status"),
aws.String("tag"),
aws.String("useragent"),
},
//Limit: aws.Int64(1000000),
}
*/
r := Record{
Key: "key127.0.0.1",
RemoteID: "abc-001",
OtherData: map[string]int{"a": 1, "b": 2, "c": 3},
Timestamp: time.Now().UTC().Unix(),
}
item, err := dynamodbattribute.ConvertToMap(r)
log.Println(item)
result, err := svc.PutItem(&dynamodb.PutItemInput{
Item: item,
TableName: aws.String("result"),
})
fmt.Println(result, err)
if err != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
return
}
fmt.Println(result)
}
func testScanTable() {
t := time.Now()
svc := dynamodb.New(&aws.Config{Region: aws.String("ap-northeast-1")})
scanParams := &dynamodb.ScanInput{
TableName: aws.String("result"),
}
resp, err := svc.Scan(scanParams)
if err != nil {
fmt.Println(err.Error())
return
}
log.Println(resp.LastEvaluatedKey)
pageNum := 0
itemCount := 0
err = svc.ScanPages(scanParams, func(page *dynamodb.ScanOutput, lastPage bool) bool {
pageNum++
items := page.Items
for i := 0; i < len(items); i++ {
fmt.Println("count :", itemCount, " item : ", *items[i]["time"].S)
itemCount++
}
fmt.Println("scan output:", *page.Count)
fmt.Println(page.LastEvaluatedKey)
return page.LastEvaluatedKey != nil
})
if err != nil {
fmt.Println(err.Error())
return
}
log.Println("-------scan end--------")
log.Println("start time:", t)
log.Println("end time:", time.Now())
}
func testTableCreate() (*dynamodb.CreateTableOutput, error) {
svc := dynamodb.New(&aws.Config{Region: aws.String("ap-northeast-1")})
params := &dynamodb.CreateTableInput{
AttributeDefinitions: []*dynamodb.AttributeDefinition{ // Required
{ // Required
AttributeName: aws.String("Key"), // Required
AttributeType: aws.String("S"), // Required
},
},
KeySchema: []*dynamodb.KeySchemaElement{ // Required
{ // Required
AttributeName: aws.String("Key"), // Required
KeyType: aws.String("HASH"), // Required
},
},
ProvisionedThroughput: &dynamodb.ProvisionedThroughput{ // Required
ReadCapacityUnits: aws.Int64(100), // Required
WriteCapacityUnits: aws.Int64(100), // Required
},
TableName: aws.String("result"), // Required
StreamSpecification: &dynamodb.StreamSpecification{
StreamEnabled: aws.Bool(true),
StreamViewType: aws.String("NEW_AND_OLD_IMAGES"),
},
}
resp, err := svc.CreateTable(params)
if err != nil {
log.Println(err.Error())
}
fmt.Println(resp)
return resp, err
}
func testTableDelete() {
svc := dynamodb.New(&aws.Config{Region: aws.String("ap-northeast-1")})
params := &dynamodb.DeleteTableInput{
TableName: aws.String("result"), // Required
}
resp, err := svc.DeleteTable(params)
if err != nil {
log.Println(err.Error())
return
}
fmt.Println(resp.TableDescription.TableStatus)
}