forked from karlc1/dyngeo
/
main.go
135 lines (118 loc) · 2.78 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"time"
"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/gofrs/uuid"
"github.com/mrgleam/dyngeo"
)
var (
dbClient *dynamodb.DynamoDB
dg *dyngeo.DynGeo
err error
)
const BATCH_SIZE = 25
type Starbucks struct {
Position Position `json:"position"`
Name string `json:"name"`
Address string `json:"address"`
Phone string `json:"phone"`
}
type Position struct {
Latitude float64 `json:"lat"`
Longitude float64 `json:"lng"`
}
func main() {
dbClient = dynamodb.New(session.Must(session.NewSession(&aws.Config{
Endpoint: aws.String("http://localhost:8000"),
Region: aws.String("eu-central-1"),
})))
dg, err = dyngeo.New(dyngeo.DynGeoConfig{
DynamoDBClient: dbClient,
HashKeyLength: 5,
TableName: "coffee-shops",
})
if err != nil {
panic(err)
}
setupTable()
loadData()
queryData()
}
func setupTable() {
createTableInput := dyngeo.GetCreateTableRequest(dg.Config)
createTableInput.ProvisionedThroughput.ReadCapacityUnits = aws.Int64(5)
createTableOutput, err := dbClient.CreateTable(createTableInput)
if err != nil {
panic(err)
}
fmt.Println("Table created")
fmt.Println(createTableOutput)
}
func loadData() {
f, err := ioutil.ReadFile("starbucks_us_locations.json")
if err != nil {
panic(err)
}
coffeeShops := []Starbucks{}
err = json.Unmarshal([]byte(f), &coffeeShops)
if err != nil {
panic(err)
}
batchInput := []dyngeo.PutPointInput{}
for _, s := range coffeeShops {
id, err := uuid.NewV4()
if err != nil {
panic(err)
}
input := dyngeo.PutPointInput{
PutItemInput: dynamodb.PutItemInput{
Item: map[string]*dynamodb.AttributeValue{
"name": {S: aws.String(s.Name)},
"address": {S: aws.String(s.Address)},
},
},
}
input.RangeKeyValue = id.String()
input.GeoPoint = dyngeo.GeoPoint{
Latitude: s.Position.Latitude,
Longitude: s.Position.Longitude,
}
batchInput = append(batchInput, input)
}
batches := [][]dyngeo.PutPointInput{}
for BATCH_SIZE < len(batchInput) {
batchInput, batches = batchInput[BATCH_SIZE:], append(batches, batchInput[0:BATCH_SIZE:BATCH_SIZE])
}
batches = append(batches, batchInput)
for count, batch := range batches {
output, err := dg.BatchWritePoints(batch)
if err != nil {
panic(err)
}
fmt.Printf("Batch %d written: %s", count, output)
}
}
func queryData() {
start := time.Now()
sbs := []Starbucks{}
err := dg.QueryRadius(dyngeo.QueryRadiusInput{
CenterPoint: dyngeo.GeoPoint{
Latitude: 40.7769099,
Longitude: -73.9822532,
},
RadiusInMeter: 5000,
}, &sbs)
if err != nil {
panic(err)
}
for _, sb := range sbs {
fmt.Println(sb)
}
fmt.Print("Executed in: ")
fmt.Println(time.Since(start))
}