-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
189 lines (171 loc) · 4.14 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package datasource
import (
"bufio"
"bytes"
"compress/gzip"
"database/sql/driver"
"encoding/json"
"io"
"os"
"strings"
u "github.com/araddon/gou"
"github.com/lytics/qlbridge/expr"
"github.com/lytics/qlbridge/schema"
"github.com/lytics/qlbridge/value"
)
var (
_ schema.Source = (*JsonSource)(nil)
_ schema.Conn = (*JsonSource)(nil)
_ schema.ConnScanner = (*JsonSource)(nil)
)
type FileLineHandler func(line []byte) (schema.Message, error)
// JsonSource implements qlbridge schema DataSource, SourceConn, Scanner
// to allow new line delimited json files to be full featured databases.
// - very, very naive scanner, forward only single pass
// - can open a file with .Open()
// - not thread-safe
// - does not implement write operations
type JsonSource struct {
table string
tbl *schema.Table
exit <-chan bool
complete bool
err error
r *bufio.Reader
gz *gzip.Reader
rc io.ReadCloser
rowct uint64
lhSpec FileLineHandler
lh FileLineHandler
columns []string
colindex map[string]int
indexCol int
filter expr.Node
}
// NewJsonSource reader assumes we are getting NEW LINE delimted json file
// - optionally may be gzipped
func NewJsonSource(table string, rc io.ReadCloser, exit <-chan bool, lh FileLineHandler) (*JsonSource, error) {
js := &JsonSource{
table: table,
exit: exit,
rc: rc,
lhSpec: lh,
lh: lh,
}
buf := bufio.NewReader(rc)
first2, err := buf.Peek(2)
if err != nil {
u.Warnf("error opening bufio.peek for json reader: %v", err)
return nil, err
}
// Gzip Files have these 2 byte prefix
if err == nil && len(first2) == 2 && bytes.Equal(first2, []byte{'\x1F', '\x8B'}) {
gr, err := gzip.NewReader(buf)
if err != nil {
u.Warnf("could not open gzip reader: %v", err)
return nil, err
}
js.gz = gr
js.r = bufio.NewReader(gr)
} else {
js.r = buf
}
if lh == nil {
js.lh = js.jsonDefaultLine
}
//m.loadTable()
return js, nil
}
func (m *JsonSource) Init() {}
func (m *JsonSource) Setup(*schema.Schema) error { return nil }
func (m *JsonSource) Tables() []string { return []string{m.table} }
func (m *JsonSource) Columns() []string { return m.columns }
func (m *JsonSource) CreateIterator() schema.Iterator { return m }
func (m *JsonSource) Table(tableName string) (*schema.Table, error) {
if m.tbl != nil {
return m.tbl, nil
}
return nil, schema.ErrNotFound
}
func (m *JsonSource) loadTable() error {
tbl := schema.NewTable(strings.ToLower(m.table))
columns := m.Columns()
for i := range columns {
columns[i] = strings.ToLower(columns[i])
tbl.AddField(schema.NewFieldBase(columns[i], value.StringType, 64, "string"))
}
tbl.SetColumns(columns)
m.tbl = tbl
return nil
}
func (m *JsonSource) Open(connInfo string) (schema.Conn, error) {
if connInfo == "stdio" || connInfo == "stdin" {
connInfo = "/dev/stdin"
}
f, err := os.Open(connInfo)
if err != nil {
return nil, err
}
exit := make(<-chan bool, 1)
return NewJsonSource(connInfo, f, exit, m.lhSpec)
}
func (m *JsonSource) Close() error {
defer func() {
if r := recover(); r != nil {
u.Errorf("close error: %v", r)
}
}()
if m.gz != nil {
m.gz.Close()
}
if m.rc != nil {
m.rc.Close()
}
return nil
}
func (m *JsonSource) Next() schema.Message {
select {
case <-m.exit:
return nil
default:
for {
line, err := m.r.ReadBytes('\n')
if err != nil {
if err == io.EOF {
m.complete = true
} else {
m.err = err
return nil
}
}
if len(line) == 0 {
return nil
}
m.rowct++
msg, err := m.lh(line)
if err != nil {
m.err = err
return nil
}
return msg
}
}
}
func (m *JsonSource) jsonDefaultLine(line []byte) (schema.Message, error) {
jm := make(map[string]interface{})
err := json.Unmarshal(line, &jm)
if err != nil {
u.Warnf("could not read json line: %v %s", err, string(line))
return nil, err
}
vals := make([]driver.Value, len(jm))
keys := make(map[string]int, len(jm))
i := 0
for k, val := range jm {
vals[i] = val
keys[k] = i
i++
}
u.Debugf("json data: %#v \n%#v", keys, vals)
return NewSqlDriverMessageMap(m.rowct, vals, keys), nil
}