-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_query.go
75 lines (60 loc) · 1.45 KB
/
push_query.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
package ksqldbx
import (
"bufio"
"context"
"encoding/json"
"io"
"net/http"
)
// Push run PUSH query that will continuously stream data through channel.
// To stop the query you have two ways: cancel the context, or call KsqlDB.CloseQuery(),
// both methods give the same effect, it will stop the query and close the channels.
// A query is a PUSH query if you use EMIT CHANGES in the query, otherwise you might
// get unexpected behavior or error
func (ksql *KsqlDB) Push(ctx context.Context, q QuerySQL, headChan chan<- Header, rowChan chan<- Row) error {
defer close(headChan)
defer close(rowChan)
// TODO
// check if sql parse option is enabled, if yes
// then parse sql before exec
res, err := ksql.queryStreamRequest(ctx, q)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return newErrFromReader(res.Body)
}
reader := bufio.NewReader(res.Body)
var header *Header
for {
select {
case <-ctx.Done():
return nil
default:
body, err := reader.ReadBytes('\n')
if err != nil {
if err != io.EOF && err != context.Canceled {
return err
}
return nil
}
if len(body) == 0 {
continue
}
if header == nil {
header = new(Header)
if err := json.Unmarshal(body, header); err != nil {
return err
}
headChan <- *header
continue
}
var row Row
if err := json.Unmarshal(body, &row); err != nil {
return err
}
rowChan <- row
}
}
}