-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpss_utility.go
197 lines (155 loc) · 5.82 KB
/
gpss_utility.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
package main
import (
"context"
"flag"
"fmt"
"gpssclient/gpss"
"log"
"strconv"
"strings"
"github.com/golang/protobuf/proto"
"google.golang.org/grpc"
)
type gpssClient struct {
client gpss.GpssClient
session *gpss.Session
conn *grpc.ClientConn
GpssAddress string
GreenplumAddress string
GreenplumPort int32
GreenplumUser string
GreenplumPasswd string
Database string
SchemaName string
TableName string
}
func MakeGpssClient(gpssAddress string, greenplumAddress string, greenplumPort int32, user string, password string, database string, schema string, table string) *gpssClient {
gpssClient := new(gpssClient)
gpssClient.GpssAddress = gpssAddress
gpssClient.GreenplumAddress = greenplumAddress
gpssClient.GreenplumPort = greenplumPort
gpssClient.GreenplumUser = user
gpssClient.GreenplumPasswd = password
gpssClient.Database = database
gpssClient.SchemaName = schema
gpssClient.TableName = table
return gpssClient
}
func (client *gpssClient) ConnectToGrpcServer() {
/* Connecting to the grpc server */
serverAddr := flag.String("server_addr", client.GpssAddress, "The server address in the format of host:port")
var err error
client.conn, err = grpc.Dial(*serverAddr, grpc.WithInsecure(), grpc.WithMaxMsgSize(309895008))
if err != nil {
log.Fatalf("fail to dial: %v", err)
}
client.client = gpss.NewGpssClient(client.conn)
}
func (client *gpssClient) ConnectToGreenplumDatabase() {
log.Printf("connecting to a greenplum database")
connReq := gpss.ConnectRequest{Host: client.GreenplumAddress, Port: client.GreenplumPort, Password: client.GreenplumPasswd, Username: client.GreenplumUser, DB: client.Database}
var err error
client.session, err = client.client.Connect(context.Background(), &connReq)
if err != nil {
log.Fatalf("fail to connect to database: %v", err)
}
}
func (client *gpssClient) DisconnectToGreenplumDatabase() {
log.Printf("disconnecting to a greenplum database")
var err error
_, err = client.client.Disconnect(context.Background(), client.session)
if err != nil {
log.Fatalf("fail to connect to database: %v", err)
}
}
func (client *gpssClient) DescribeTable() *gpss.Columns {
log.Printf("table informations")
describeReq := gpss.DescribeTableRequest{Session: client.session, TableName: client.TableName, SchemaName: client.SchemaName}
columns, _ := client.client.DescribeTable(context.Background(), &describeReq)
return columns
}
func (client *gpssClient) prepareForWriting(cols *gpss.Columns) {
log.Printf("prepare for writing")
// Prepare for writing
columns := make([]string, len(cols.Columns))
// Looping over table columns as defined in greenplum
for i, col := range cols.Columns {
//fmt.Println("field: " + col.Name)
columns[i] = col.Name
}
insertOption := gpss.InsertOption{ErrorLimitCount: 25, ErrorLimitPercentage: 25, TruncateTable: false, InsertColumns: columns}
openRequestInsertOption := gpss.OpenRequest_InsertOption{InsertOption: &insertOption}
openRequest := gpss.OpenRequest{Session: client.session, SchemaName: client.SchemaName, TableName: client.TableName, Timeout: 5, Option: &openRequestInsertOption}
_, err := client.client.Open(context.Background(), &openRequest)
if err != nil {
log.Fatalf("fail to open request to write: %v", err)
}
}
func convertType(field string, databaseType string) *gpss.DBValue {
// *DBValue_Int32Value
// *DBValue_Int64Value
// *DBValue_Float32Value
// *DBValue_Float64Value
// *DBValue_StringValue
// *DBValue_BytesValue
// *DBValue_TimeStampValue
// *DBValue_NullValue
//fmt.Println("field is: " + field + "length: " + string(len(field)))
dbValue := new(gpss.DBValue)
// Consider NULL value
if field == "NULL" {
dbValue.DBType = &gpss.DBValue_NullValue{}
return dbValue
}
if strings.Contains(databaseType, "int") || strings.Contains(databaseType, "serial") {
if n, err := strconv.Atoi(field); err == nil {
dbValue.DBType = &gpss.DBValue_Int64Value{Int64Value: int64(n)}
} else {
fmt.Println(field, "is not an integer.")
}
} else if strings.Contains(databaseType, "float") || strings.Contains(databaseType, "numeric") || strings.Contains(databaseType, "decimal") {
if n, err := strconv.ParseFloat(field, 64); err == nil {
dbValue.DBType = &gpss.DBValue_Float64Value{Float64Value: float64(n)}
}
} else {
dbValue.DBType = &gpss.DBValue_StringValue{StringValue: field}
} /*else if strings.Contains(databaseType, "timestamp") {
dbValue.DBType = &gpss.DBValue_TimeStampValue{TimeStampValue: string(field)}
} */
return dbValue
}
func (client *gpssClient) WriteToGreenplum(buffer []string) {
log.Printf("Beginning to write to greenplum")
// Take table information from greenplum
cols := client.DescribeTable()
// Prepare table for writing
client.prepareForWriting(cols)
// For every entry buffered in memory check for fields
rowData := make([]*gpss.RowData, len(buffer))
for i, line := range buffer {
dbValue := make([]*gpss.DBValue, len(cols.Columns))
// From an entry composed by lines return a list
fields := strings.Split(strings.Replace(line, "\r\n", "\n", -1), "\n")
for j, field := range fields {
dbValue[j] = convertType(field, cols.Columns[j].DatabaseType)
}
rowLine := gpss.Row{Columns: dbValue}
rowData[i] = new(gpss.RowData)
//databytes, _ := rowLine.Descriptor()
rowData[i].Data, _ = proto.Marshal(&rowLine)
}
req := gpss.WriteRequest{Session: client.session, Rows: rowData}
_, err := client.client.Write(context.Background(), &req)
if err != nil {
log.Fatalf("fail to open request to write: %v", err)
}
client.closeRequest()
}
func (client *gpssClient) closeRequest() {
closeRequest := gpss.CloseRequest{Session: client.session}
tStats, err := client.client.Close(context.Background(), &closeRequest)
if err != nil {
log.Fatalf("fail close write to database: %v", err)
}
fmt.Println("Result: ", tStats.String())
}