-
Notifications
You must be signed in to change notification settings - Fork 0
/
Elasticsearch.go
199 lines (170 loc) · 4 KB
/
Elasticsearch.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
package elasticsearch
import (
"bytes"
"database/sql/driver"
"encoding/base64"
"encoding/json"
"errors"
"io"
"net/http"
"strings"
"time"
)
type esQueryRequest struct {
Query string `json:"query"`
}
type esCursorRequest struct {
Cursor string `json:"cursor"`
}
type esColumnInfo struct {
Name string `json:"name"`
Type esType `json:"type"`
}
type esResult struct {
Columns []esColumnInfo `json:"columns"`
Rows [][]interface{} `json:"rows"`
Cursor string `json:"Cursor"`
}
func newRows(dsn string, query string) (*Rows, error) {
byteReqBody, err := json.Marshal(esQueryRequest{query})
if err != nil {
return nil, err
}
return esRequest(dsn, string(byteReqBody))
}
func nextRows(dsn string, cursor string) ([][]driver.Value, string, error) {
byteReqBody, err := json.Marshal(esCursorRequest{cursor})
if err != nil {
return nil, "", err
}
result, err := esRequest(dsn, string(byteReqBody))
return (*result).rows, (*result).cursor, err
}
func parsingDSN(dsn string) (url, username, password string, err error) {
var protocal, address, port, certBase64 string
dnsParts := strings.Split(dsn, "://")
if len(dnsParts) <= 1 {
protocal = "http"
dsn = dnsParts[0]
} else {
protocal = dnsParts[0]
dsn = dnsParts[1]
}
dnsParts = strings.Split(dsn, "@")
if len(dnsParts) <= 1 {
certBase64 = ""
dsn = dnsParts[0]
} else {
certBase64 = dnsParts[0]
dsn = dnsParts[1]
}
if certBase64 != "" {
certByte, err := base64.URLEncoding.DecodeString(certBase64)
if err != nil {
return "", "", "", ErrInvalidArgs
}
certPart := strings.Split(string(certByte), ":")
username, password = certPart[0], certPart[1]
}
dnsParts = strings.Split(dsn, ":")
if len(dnsParts) <= 1 {
address = "localhost"
port = "9200"
} else {
address = dnsParts[0]
if len(dnsParts[1]) == 0 {
port = "9200"
} else {
port = dnsParts[1]
}
}
return protocal + "://" + address + ":" + port + "/_xpack/sql", username, password, nil
}
func postEs(dsn string, body string) (string, error) {
url, username, password, err := parsingDSN(dsn)
if err != nil {
return "", err
}
client := http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte(body)))
if err != nil {
return "", err
}
req.Header.Set("Content-type", "application/json")
if username != "" && password != "" {
req.SetBasicAuth(username, password)
}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
var esResp string
buff := make([]byte, 10)
n, err := io.ReadFull(resp.Body, buff)
for err != io.EOF {
esResp = esResp + string(buff[:n])
n, err = io.ReadFull(resp.Body, buff)
}
return esResp, nil
}
func esRequest(dsn string, body string) (*Rows, error) {
esResp, err := postEs(dsn, body)
if err != nil {
return nil, err
}
esResult := esResult{}
err = json.Unmarshal([]byte(esResp), &esResult)
if err != nil {
return nil, err
}
if esResult.Rows == nil {
return nil, errors.New(esResp)
}
var columns []string
var types []esType
for _, columnInfo := range esResult.Columns {
columns = append(columns, columnInfo.Name)
types = append(types, columnInfo.Type)
}
var rows [][]driver.Value
for _, values := range esResult.Rows {
var row []driver.Value
for i, value := range values {
row = append(row, typeConvert(types[i], value))
}
rows = append(rows, row)
}
return &Rows{
dsn: dsn,
columns: columns,
types: types,
rows: rows,
cursor: esResult.Cursor,
},
nil
}
func typeConvert(t esType, value interface{}) driver.Value {
//Unsupported
//esBinary, esByte, esObject, esNested, esUnsupported
switch t {
case esKeyword, esText, esIP:
return value.(string)
case esShort, esLong, esFloat, esHalfFloat, esScaledFloat, esDouble:
return value.(float64)
case esInteger:
return int(value.(float64))
case esBoolean:
return value.(bool)
case esDatetime:
t, err := time.Parse(time.RFC3339, value.(string))
if err != nil {
return nil
}
return t
case esNull:
return nil
default:
return value
}
}