-
Notifications
You must be signed in to change notification settings - Fork 0
/
partition.go
123 lines (100 loc) · 3.23 KB
/
partition.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
package hive
import (
"encoding/json"
"fmt"
)
var (
ENDPOINT_PARTITION = `%v:%v/templeton/v1/ddl/database/%v/table/:%v/partition?user.name=%v`
ENDPOINT_PARTITION_DETAIL = `%v:%v/templeton/v1/ddl/database/%v/table/:%v/partition/%v?user.name=%v`
)
type ListPartitionResponse struct {
Database string `json:"database"`
Table string `json:"table"`
Partitions []struct {
Name string `json:"name"`
Values []Value `json:"values"`
} `json:"Partitions"`
}
type ShowPartitionResponse struct {
Database string `json:"database"`
Table string `json:"table"`
Partition string `json:"partition"`
Columns []Column `json:"columns"`
// 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 CreatePartitionResponse struct {
Database string `json:"database"`
Table string `json:"table"`
Partition string `json:"partition"`
}
type DeletePartitionResponse struct {
Database string `json:"database"`
Table string `json:"table"`
Partition string `json:"partition"`
}
type Value struct {
ColumnName string `json:"columnName"`
ColumnValue string `json:"columnValue"`
}
func (this *Client) ListPartition(database, table string) (*ListPartitionResponse, error) {
endpoint := fmt.Sprintf(ENDPOINT_PARTITION, this.BaseUrl, this.Port, database, table, this.User)
resp, err := this.request(HTTP_GET, endpoint, nil)
if err != nil {
return nil, err
}
res := &ListPartitionResponse{}
if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
return nil, err
}
return res, nil
}
func (this *Client) ShowPartition(database, table string) (*ShowPartitionResponse, error) {
endpoint := fmt.Sprintf(ENDPOINT_PARTITION, this.BaseUrl, this.Port, database, table, this.User)
resp, err := this.request(HTTP_GET, endpoint, nil)
if err != nil {
return nil, err
}
res := &ShowPartitionResponse{}
if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
return nil, err
}
return res, nil
}
func (this *Client) CreatePartition(database, table string, partitions map[string]string) (*CreatePartitionResponse, error) {
var partition string
for k, v := range partitions {
partition += fmt.Sprintf("%s=%s", k, v)
}
endpoint := fmt.Sprintf(ENDPOINT_PARTITION, this.BaseUrl, this.Port, database, table, this.User)
resp, err := this.request(HTTP_PUT, endpoint, nil)
if err != nil {
return nil, err
}
res := &CreatePartitionResponse{}
if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
return nil, err
}
return res, nil
}
func (this *Client) DeletePartition(database, table string, partitions map[string]string) (*DeletePartitionResponse, error) {
var partition string
for k, v := range partitions {
partition += fmt.Sprintf("%s=%s", k, v)
}
endpoint := fmt.Sprintf(ENDPOINT_PARTITION, this.BaseUrl, this.Port, database, table, this.User)
resp, err := this.request(HTTP_PUT, endpoint, nil)
if err != nil {
return nil, err
}
res := &DeletePartitionResponse{}
if err := json.NewDecoder(resp.Body).Decode(res); err != nil {
return nil, err
}
return res, nil
}