-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
195 lines (160 loc) · 5.21 KB
/
table.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
package hive
import (
"bytes"
"encoding/json"
"fmt"
)
var (
ENDPOINT_TABLE = `%v:%v/templeton/v1/ddl/database/%v/table?user.name=%v`
ENDPOINT_TABLE_DETAIL = `%v:%v/templeton/v1/ddl/database/%v/table/%v?user.name=%v`
ENDPOINT_TABLE_COPY = `%v:%v/templeton/v1/ddl/database/%v/table/%v/like/%v?user.name=%v`
)
type ListTableResponse struct {
Database string `json:"database"`
Tables []string `json:"tables"`
}
type ShowTableResponse struct {
Columns []Column `json:"columns"`
Database string `json:"database"`
Table string `json:"table"`
// extend
Partitioned bool `json:"partitioned"`
Location string `json:"location"`
OutputFormat string `json:"outputFormat"`
Owner string `json:"owner"`
PartitionColumns []Column `json:"partitionColumns"`
InputFormat string `json:"inputFormat"`
}
type RowFormat struct {
FieldsTerminatedBy string `json:"fieldsTerminatedBy"`
}
type SortedBy []struct {
ColumnName string `json:"columnName"`
Order string `json:"order"`
}
type CreateTableInput struct {
Comment string `json:"comment,omitempty"`
Columns []Column `json:"columns,omitempty"`
PartitionBy []Column `json:"partitionedBy,omitempty"`
ClusteredBy struct {
ColumnNames []string `json:"columnNames"`
SortedBy SortedBy `json:"sortedBy"`
NumberOfBuckets string `json:"numberOfBuckets"`
} `json:"clusteredBy"`
IfNotExists bool `json:"ifNotExists,omitempty"`
External bool `json:"external,omitempty"`
Format interface{} `json:"format,omitempty"`
Location string `json:"location,omitempty"`
}
type CreateTableResponse struct {
Database string `json:"database"`
Table string `json:"table"`
}
type RenameTableInput struct {
Rename string `json:"rename,omitempty"`
}
type RenameTableResponse struct {
Database string `json:"database"`
Table string `json:"table"`
}
type CopyTableResponse struct {
Database string `json:"database"`
Table string `json:"table"`
}
type UpdateTableResponse struct {
Database string `json:"database"`
Table string `json:"table"`
}
type DropTableResponse struct {
Database string `json:"database"`
Table string `json:"table"`
}
func (this *Client) ListTable(database string) (*ListTableResponse, error) {
endpoint := fmt.Sprintf(ENDPOINT_TABLE, this.BaseUrl, this.Port, database, this.User)
resp, err := this.request(HTTP_GET, endpoint, nil)
if err != nil {
return nil, err
}
res := &ListTableResponse{}
if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
return nil, err
}
return res, nil
}
func (this *Client) ShowTable(database, table string) (*ShowTableResponse, error) {
endpoint := fmt.Sprintf(ENDPOINT_TABLE_DETAIL, this.BaseUrl, this.Port, database, table, this.User)
resp, err := this.request(HTTP_GET, endpoint, nil)
if err != nil {
return nil, err
}
res := &ShowTableResponse{}
if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
return nil, err
}
return res, nil
}
func (this *Client) CreateTable(database, table string, in *CreateTableInput) (*CreateTableResponse, error) {
body := new(bytes.Buffer)
json.NewEncoder(body).Encode(in)
endpoint := fmt.Sprintf(ENDPOINT_TABLE_DETAIL, this.BaseUrl, this.Port, database, table, this.User)
resp, err := this.request(HTTP_PUT, endpoint, body)
if err != nil {
return nil, err
}
res := &CreateTableResponse{}
if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
return nil, err
}
return res, nil
}
func (this *Client) RenameTable(database, table string, in *RenameTableInput) (*RenameTableResponse, error) {
body := new(bytes.Buffer)
json.NewEncoder(body).Encode(in)
endpoint := fmt.Sprintf(ENDPOINT_TABLE_DETAIL, this.BaseUrl, this.Port, database, table, this.User)
resp, err := this.request(HTTP_POST, endpoint, body)
if err != nil {
return nil, err
}
res := &RenameTableResponse{}
if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
return nil, err
}
return res, nil
}
func (this *Client) CopyTable(database, existingTable, newTable string) (*CopyTableResponse, error) {
endpoint := fmt.Sprintf(ENDPOINT_TABLE_COPY, this.BaseUrl, this.Port, database, existingTable, newTable, this.User)
resp, err := this.request(HTTP_POST, endpoint, nil)
if err != nil {
return nil, err
}
res := &CopyTableResponse{}
if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
return nil, err
}
return res, nil
}
// TODO: params
func (this *Client) UpdateTable(database, table string) (*UpdateTableResponse, error) {
endpoint := fmt.Sprintf(ENDPOINT_TABLE_DETAIL, this.BaseUrl, this.Port, database, table, this.User)
resp, err := this.request(HTTP_PUT, endpoint, nil)
if err != nil {
return nil, err
}
res := &UpdateTableResponse{}
if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
return nil, err
}
return res, nil
}
func (this *Client) DropTable(database, table string) (*DropTableResponse, error) {
endpoint := fmt.Sprintf(ENDPOINT_TABLE_DETAIL, this.BaseUrl, this.Port, database, table, this.User)
resp, err := this.request(HTTP_DELETE, endpoint, nil)
if err != nil {
return nil, err
}
res := &DropTableResponse{}
if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
return nil, err
}
return res, nil
}