-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
115 lines (89 loc) · 2.23 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
package main
import (
"fmt"
"time"
"github.com/Azure/azure-sdk-for-go/storage"
)
var (
ts storage.TableServiceClient
)
const (
fullmetadata = "application/json;odata=fullmetadata"
)
func main() {
insert("1", "3")
insertBatch("1", "3")
}
func insert(partitionkey string, rowkey string) {
client, err := storage.NewEmulatorClient()
if err != nil {
fmt.Printf("%s: \n", err)
}
ts = client.GetTableService()
t := ts.GetTableReference("InsertTestTable")
entity := t.GetEntityReference(partitionkey, rowkey)
props := map[string]interface{}{
"AmountDue": 200.23,
"CustomerCode": "123",
"CustomerSince": time.Date(1992, time.December, 20, 21, 55, 0, 0, time.UTC),
"IsActive": true,
"NumberOfOrders": int64(255),
}
entity.Properties = props
err = entity.Insert(fullmetadata, nil)
if cerr, ok := err.(storage.AzureStorageServiceError); ok {
if cerr.Code == "TableNotFound" {
if cerr := t.Create(uint(10), storage.FullMetadata, nil); cerr != nil {
fmt.Printf("error creating table: %v.", cerr)
return
}
// retry
err = entity.Insert(fullmetadata, nil)
}
}
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Inserted!")
}
}
func insertBatch(partitionkey string, rowkey string) {
client, err := storage.NewEmulatorClient()
if err != nil {
fmt.Printf("%s: \n", err)
}
ts = client.GetTableService()
t := ts.GetTableReference("InsertBatchTestTable")
entity := t.GetEntityReference(partitionkey, rowkey)
props := map[string]interface{}{
"AmountDue": 200.23,
"CustomerCode": "123",
"CustomerSince": time.Date(1992, time.December, 20, 21, 55, 0, 0, time.UTC),
"IsActive": true,
"NumberOfOrders": int64(255),
}
entity.Properties = props
tb := t.NewBatch()
tb.InsertOrMergeEntity(entity, true)
if err := tb.ExecuteBatch(); err != nil {
if cerr, ok := err.(storage.AzureStorageServiceError); ok {
if cerr.Code == "TableNotFound" {
if cerr := t.Create(uint(10), storage.FullMetadata, nil); cerr != nil {
fmt.Printf("error creating table: %v.", cerr)
return
}
// retry
err = tb.ExecuteBatch()
}
}
if err != nil {
fmt.Println(err)
return
}
}
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Inserted! ")
}
}