forked from ClickHouse/clickhouse-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
172 lines (153 loc) · 4.33 KB
/
json.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
// Licensed to ClickHouse, Inc. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. ClickHouse, Inc. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package clickhouse_api
import (
"context"
"fmt"
"github.com/Perseus/clickhouse-go/v2"
clickhouse_tests "github.com/Perseus/clickhouse-go/v2/tests"
)
func InsertReadJSON() error {
conn, err := GetNativeConnection(clickhouse.Settings{
"allow_experimental_object_type": 1,
}, nil, nil)
if err != nil {
return err
}
ctx := context.Background()
if !clickhouse_tests.CheckMinServerServerVersion(conn, 22, 6, 1) {
return nil
}
if err != nil {
return nil
}
conn.Exec(ctx, "DROP TABLE IF EXISTS example")
if err = conn.Exec(ctx, `
CREATE TABLE example (
Col1 JSON,
Col2 JSON,
Col3 JSON
)
Engine Memory
`); err != nil {
return err
}
type User struct {
Name string `json:"name"`
Age uint8 `json:"age"`
Password string `ch:"-"`
}
defer func() {
conn.Exec(ctx, "DROP TABLE example")
}()
batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
if err != nil {
return err
}
// we can insert JSON as either a string, struct or map
col1Data := `{"name": "Clicky McClickHouse", "age": 40, "password": "password"}`
col2Data := User{
Name: "Clicky McClickHouse Snr",
Age: uint8(80),
Password: "random",
}
col3Data := map[string]any{
"name": "Clicky McClickHouse Jnr",
"age": uint8(10),
"password": "clicky",
}
// both named and unnamed can be added with slices
if err = batch.Append(col1Data, col2Data, col3Data); err != nil {
return err
}
if err = batch.Send(); err != nil {
return err
}
// we can scan JSON into either a map or struct
var (
col1 map[string]any
col2 map[string]any
col3 User
)
// named tuples can be retrieved into a map or slices, unnamed just slices
if err = conn.QueryRow(ctx, "SELECT * FROM example").Scan(&col1, &col2, &col3); err != nil {
return err
}
fmt.Printf("row: col1=%v, col2=%v, col3=%v\n", col1, col2, col3)
return nil
}
func ReadComplexJSON() error {
conn, err := GetNativeConnection(clickhouse.Settings{
"allow_experimental_object_type": 1,
}, nil, nil)
if err != nil {
return err
}
ctx := context.Background()
if !clickhouse_tests.CheckMinServerServerVersion(conn, 22, 6, 1) {
return nil
}
conn.Exec(ctx, "DROP TABLE IF EXISTS example")
if err = conn.Exec(ctx, `
CREATE TABLE example (
Col1 JSON
)
Engine Memory
`); err != nil {
return err
}
type Releases struct {
Version string
}
type Repository struct {
URL string `json:"url"`
Releases []Releases
}
row := map[string]any{
"title": "Document JSON support",
"type": "Issue",
"assignee": map[string]any{
"id": int16(0),
"name": "Dale",
"repositories": []Repository{
{URL: "https://github.com/ClickHouse/clickhouse-python", Releases: []Releases{{Version: "2.0.0"}, {Version: "2.1.0"}}},
{URL: "https://github.com/ClickHouse/clickhouse-go"},
},
"organizations": []string{},
},
"labels": []string{},
"contributors": []map[string]any{
{"Id": int16(2244), "Name": "Dale", "orgs": []string{"Support Engineer", "Consulting", "PM", "Integrations"}, "Repositories": []map[string]any{{"url": "https://github.com/ClickHouse/clickhouse-go", "Releases": []map[string]any{{"Version": "2.0.0"}, {"Version": "2.1.0"}}}, {"url": "https://github.com/grafana/clickhouse"}}},
},
}
batch, err := conn.PrepareBatch(ctx, "INSERT INTO example")
if err != nil {
return err
}
if err = batch.Append(row); err != nil {
return err
}
if err = batch.Send(); err != nil {
return err
}
var event map[string]any
if err = conn.QueryRow(ctx, "SELECT * FROM example").Scan(&event); err != nil {
return err
}
fmt.Printf("%v\n", event)
return nil
}