-
Notifications
You must be signed in to change notification settings - Fork 2
/
ingest_databend_test.go
208 lines (191 loc) · 6.27 KB
/
ingest_databend_test.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"database/sql"
"encoding/json"
"fmt"
"os"
"testing"
"time"
"github.com/test-go/testify/assert"
"github.com/databendcloud/bend-ingest-kafka/config"
"github.com/databendcloud/bend-ingest-kafka/message"
)
type ingestDatabendTest struct {
databendDSN string
}
func prepareIngestDatabendTest() *ingestDatabendTest {
testDatabendDSN := os.Getenv("TEST_DATABEND_DSN")
if testDatabendDSN == "" {
testDatabendDSN = "http://databend:databend@localhost:8000?presigned_url_disabled=true"
}
return &ingestDatabendTest{databendDSN: testDatabendDSN}
}
func TestParseKafkaServers(t *testing.T) {
s1 := []string{"127.0.0.1:8000", "127.0.0.1:8000,127.0.0.2:8000"}
for _, s := range s1 {
res := parseKafkaServers(s)
fmt.Println(res)
}
}
func TestIngestDataIsJsonTransform(t *testing.T) {
tt := prepareIngestDatabendTest()
cfg := config.Config{
KafkaBootstrapServers: "127.0.0.1:9002",
KafkaTopic: "test",
KafkaConsumerGroup: "test",
DatabendDSN: tt.databendDSN,
//DatabendDSN: os.Getenv("TEST_DATABEND_DSN"),
DataFormat: "json",
IsJsonTransform: true,
DatabendTable: "test_ingest",
BatchSize: 10,
BatchMaxInterval: 10,
UserStage: "~",
}
db, err := sql.Open("databend", cfg.DatabendDSN)
assert.NoError(t, err)
execute(db, "create table if not exists test_ingest(name varchar, age int, isMarried boolean);")
defer execute(db, "drop table if exists test_ingest;")
testData := []string{"{\"name\": \"Alice\",\"age\": 30,\"isMarried\": true}", "{\"name\": \"Alice\",\"age\": 30,\"isMarried\": true}"}
messageData := message.MessageData{
Data: testData[0],
DataOffset: 1,
Partition: 1,
Key: "1",
CreateTime: time.Now(),
}
messagesBatch := &message.MessagesBatch{Messages: []message.MessageData{messageData}}
ig := NewDatabendIngester(&cfg)
err = ig.IngestData(messagesBatch)
assert.NoError(t, err)
// check the data
result, err := db.Query("select * from test_ingest")
assert.NoError(t, err)
count := 0
for result.Next() {
count += 1
var name string
var age int
var isMarried bool
err = result.Scan(&name, &age, &isMarried)
fmt.Println(name, age, isMarried)
}
assert.NotEqual(t, 0, count)
}
type recordMetadata struct {
CreateTime time.Time `json:"create_time"`
Offset int64 `json:"offset"`
Partition int32 `json:"partition"`
Key string `json:"key"`
Topic string `json:"topic"`
}
func TestIngestDataWithoutJsonTransform(t *testing.T) {
tt := prepareIngestDatabendTest()
cfg := config.Config{
KafkaBootstrapServers: "127.0.0.1:9002",
KafkaTopic: "test",
KafkaConsumerGroup: "test",
DatabendDSN: tt.databendDSN,
//DatabendDSN: os.Getenv("TEST_DATABEND_DSN"),
DataFormat: "json",
IsJsonTransform: false,
DatabendTable: "default.test_ingest_without",
BatchSize: 10,
BatchMaxInterval: 10,
UserStage: "~",
}
db, err := sql.Open("databend", cfg.DatabendDSN)
assert.NoError(t, err)
execute(db, "create or replace table test_ingest_without(uuid String, raw_data JSON, record_metadata JSON, add_time timestamp);")
defer execute(db, "drop table if exists test_ingest_without;")
testData := []string{"{\"name\": \"Alice\",\"age\": 30,\"isMarried\": true}", "{\"name\": \"Alice\",\"age\": 30,\"isMarried\": true}"}
messageData := message.MessageData{
Data: testData[0],
DataOffset: 1,
Partition: 1,
Key: "1",
CreateTime: time.Now(),
}
messagesBatch := &message.MessagesBatch{Messages: []message.MessageData{messageData}}
ig := NewDatabendIngester(&cfg)
err = ig.IngestData(messagesBatch)
assert.NoError(t, err)
// check the data
result, err := db.Query("select * from test_ingest_without")
assert.NoError(t, err)
count := 0
var raw_data string
var record_metadata string
var add_time time.Time
for result.Next() {
count += 1
var uuid string
err = result.Scan(&uuid, &raw_data, &record_metadata, &add_time)
fmt.Println(uuid, raw_data, record_metadata, add_time)
}
assert.NotEqual(t, 0, count)
res := &recordMetadata{}
err = json.Unmarshal([]byte(record_metadata), res)
fmt.Println(*res)
assert.NoError(t, err)
assert.Equal(t, messageData.DataOffset, res.Offset)
}
func TestIngestWithReplaceMode(t *testing.T) {
tt := prepareIngestDatabendTest()
tableName := "default.test_ingest_replace"
cfg := config.Config{
KafkaBootstrapServers: "127.0.0.1:9002",
KafkaTopic: "test",
KafkaConsumerGroup: "test",
DatabendDSN: tt.databendDSN,
//DatabendDSN: os.Getenv("TEST_DATABEND_DSN"),
DataFormat: "json",
IsJsonTransform: false,
DatabendTable: tableName,
BatchSize: 10,
BatchMaxInterval: 10,
UseReplaceMode: true,
UserStage: "~",
}
db, err := sql.Open("databend", cfg.DatabendDSN)
assert.NoError(t, err)
err = execute(db, fmt.Sprintf("create or replace table %s (uuid String, koffset bigint, kpartition int, raw_data JSON, record_metadata JSON, add_time timestamp);", tableName))
assert.NoError(t, err)
defer execute(db, fmt.Sprintf("drop table if exists %s;", tableName))
testData := []string{"{\"name\": \"Alice\",\"age\": 30,\"isMarried\": true}", "{\"name\": \"Alice\",\"age\": 30,\"isMarried\": true}"}
messageData := message.MessageData{
Data: testData[0],
DataOffset: 1,
Partition: 1,
Key: "1",
CreateTime: time.Now(),
}
messagesBatch := &message.MessagesBatch{Messages: []message.MessageData{messageData}}
ig := NewDatabendIngester(&cfg)
err = ig.IngestParquetData(messagesBatch)
assert.NoError(t, err)
// double replace into, but only on record inserted
err = ig.IngestParquetData(messagesBatch)
assert.NoError(t, err)
// check the data
result, err := db.Query(fmt.Sprintf("select * from %s", tableName))
assert.NoError(t, err)
count := 0
var uuid string
var koffset int64
var kpartition int
var raw_data string
var record_metadata string
var add_time string
for result.Next() {
count += 1
err = result.Scan(&uuid, &koffset, &kpartition, &raw_data, &record_metadata, &add_time)
fmt.Println(uuid, koffset, kpartition, raw_data, record_metadata, add_time)
}
assert.Equal(t, 1, count)
res := &recordMetadata{}
err = json.Unmarshal([]byte(record_metadata), res)
fmt.Println(*res)
assert.NoError(t, err)
assert.Equal(t, messageData.DataOffset, res.Offset)
}