-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqltocsvgzip.go
351 lines (299 loc) · 8.05 KB
/
sqltocsvgzip.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// sqltocsvgzip package converts database query results
// (in the form of database/sql Rows) into CSV.GZIP output.
//
// Source and README at https://github.com/thatInfrastructureGuy/sqltocsvgzip
package sqltocsvgzip
import (
"bytes"
"database/sql"
"fmt"
"io"
"log"
"net/url"
"os"
"os/signal"
"sync"
"syscall"
)
// WriteFile will write a CSV.GZIP file to the file name specified (with headers)
// based on whatever is in the sql.Rows you pass in.
func WriteFile(csvGzipFileName string, rows *sql.Rows) (rowCount int64, err error) {
return WriteConfig(rows).WriteFile(csvGzipFileName)
}
// UploadToS3 will upload a CSV.GZIP file to AWS S3 bucket (with headers)
// based on whatever is in the sql.Rows you pass in.
// UploadToS3 looks for the following environment variables.
// Required: S3_BUCKET, S3_PATH, S3_REGION
// Optional: S3_ACL (default => bucket-owner-full-control)
func UploadToS3(rows *sql.Rows) (rowCount int64, err error) {
return UploadConfig(rows).Upload()
}
// Upload uploads the csv.gzip, return an error if problem.
// Creates a Multipart AWS requests.
// Completes the multipart request if all uploads are successful.
// Aborts the operation when an error is received.
func (c *Converter) Upload() (rowCount int64, err error) {
if c.UploadPartSize < minFileSize {
return 0, fmt.Errorf("UploadPartSize should be greater than %v\n", minFileSize)
}
// Create MultiPart S3 Upload
err = c.createS3Session()
if err != nil {
return 0, err
}
err = c.createMultipartRequest()
if err != nil {
return 0, err
}
wg := sync.WaitGroup{}
buf := bytes.Buffer{}
c.uploadQ = make(chan *obj, c.UploadThreads)
c.quit = make(chan bool, 1)
// Upload Parts to S3
for i := 0; i < c.UploadThreads; i++ {
wg.Add(1)
go func() {
defer wg.Done()
err = c.UploadPart()
if err != nil {
c.writeLog(Error, err.Error())
}
}()
}
err = c.Write(&buf)
if err != nil {
// Abort S3 Upload
awserr := c.abortMultipartUpload()
if awserr != nil {
return 0, awserr
}
return 0, err
}
close(c.uploadQ)
wg.Wait()
if c.partNumber == 0 {
// Upload one time
c.writeLog(Info, "Gzip file < 5 MB. Enable direct upload. Abort multipart upload.")
err = c.abortMultipartUpload()
if err != nil {
return 0, err
}
err = c.UploadObjectToS3(&buf)
if err != nil {
return 0, err
}
return c.RowCount, nil
}
// Sort completed parts
c.sortCompletedParts()
// Complete S3 upload
completeResponse, err := c.completeMultipartUpload()
if err != nil {
// Abort S3 Upload
awserr := c.abortMultipartUpload()
if awserr != nil {
return 0, awserr
}
return 0, err
}
uploadPath, err := url.PathUnescape(completeResponse.String())
if err != nil {
return 0, err
}
c.writeLog(Info, "Successfully uploaded file: "+uploadPath)
return c.RowCount, nil
}
// WriteFile writes the csv.gzip to the filename specified, return an error if problem
func (c *Converter) WriteFile(csvGzipFileName string) (rowCount int64, err error) {
f, err := os.Create(csvGzipFileName)
if err != nil {
return 0, err
}
defer f.Close()
// Explicitely unset s3 upload
c.S3Upload = false
err = c.Write(f)
if err != nil {
return 0, err
}
return c.RowCount, nil
}
// Write writes the csv.gzip to the Writer provided
func (c *Converter) Write(w io.Writer) error {
writeRow := true
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(interrupt)
csvWriter, csvBuffer := c.getCSVWriter()
// Set headers
columnNames, totalColumns, err := c.setCSVHeaders(csvWriter)
if err != nil {
return err
}
// Buffers for each iteration
values := make([]interface{}, totalColumns, totalColumns)
valuePtrs := make([]interface{}, totalColumns, totalColumns)
for i := range columnNames {
valuePtrs[i] = &values[i]
}
zw, err := c.getGzipWriter(w)
if err != nil {
return err
}
defer zw.Close()
// Iterate over sql rows
for c.rows.Next() {
select {
case <-c.quit:
c.abortMultipartUpload()
return fmt.Errorf("Received quit signal. Exiting.")
case <-interrupt:
c.abortMultipartUpload()
return fmt.Errorf("Received quit signal. Exiting.")
default:
// Do nothing
}
if err = c.rows.Scan(valuePtrs...); err != nil {
return err
}
row := c.stringify(values)
if c.rowPreProcessor != nil {
writeRow, row = c.rowPreProcessor(row, columnNames)
}
if writeRow {
c.RowCount = c.RowCount + 1
// Write to CSV Buffer
err = csvWriter.Write(row)
if err != nil {
return err
}
csvWriter.Flush()
// Convert from csv to gzip
// Writes from buffer to underlying file
if csvBuffer.Len() >= (c.GzipBatchPerGoroutine * c.GzipGoroutines) {
_, err = zw.Write(csvBuffer.Bytes())
if err != nil {
return err
}
err = zw.Flush()
if err != nil {
return err
}
// Reset buffer
csvBuffer.Reset()
// Upload partially created file to S3
// If size of the gzip file exceeds maxFileStorage
if c.S3Upload {
// GZIP writer to underline file.csv.gzip
gzipBuffer, ok := w.(*bytes.Buffer)
if !ok {
return fmt.Errorf("Expected buffer. Got %T", w)
}
if gzipBuffer.Len() >= c.UploadPartSize {
if c.partNumber == 10000 {
return fmt.Errorf("Number of parts cannot exceed 10000. Please increase UploadPartSize and try again.")
}
// Add to Queue
c.AddToQueue(gzipBuffer, false)
//Reset writer
gzipBuffer.Reset()
}
}
}
}
}
err = c.rows.Err()
if err != nil {
return err
}
_, err = zw.Write(csvBuffer.Bytes())
if err != nil {
return err
}
err = zw.Close()
if err != nil {
return err
}
//Wipe the buffer
csvBuffer.Reset()
// Upload last part of the file to S3
if c.S3Upload {
if c.partNumber == 0 {
return nil
}
// GZIP writer to underline file.csv.gzip
gzipBuffer, ok := w.(*bytes.Buffer)
if !ok {
return fmt.Errorf("Expected buffer. Got %T", w)
}
// Add to Queue for multipart upload
c.AddToQueue(gzipBuffer, true)
//Reset writer
gzipBuffer.Reset()
}
// Log the total number of rows processed.
c.writeLog(Info, fmt.Sprintf("Total sql rows processed: %v", c.RowCount))
return nil
}
// AddToQueue sends obj over the upload queue.
// Currently, It is designed to work with AWS multipart upload.
// If the part body is less than 5Mb in size, 2 parts are combined together before sending.
func (c *Converter) AddToQueue(buf *bytes.Buffer, lastPart bool) {
// Increament PartNumber
c.partNumber++
if buf.Len() >= minFileSize {
if c.partNumber > 1 {
// Add part to queue
c.writeLog(Debug, fmt.Sprintf("Add part to queue: #%v", c.partNumber-1))
c.uploadQ <- &obj{
partNumber: c.partNumber - 1,
buf: c.gzipBuf,
}
}
c.gzipBuf = make([]byte, buf.Len())
copy(c.gzipBuf, buf.Bytes())
if lastPart {
// Add last part to queue
c.writeLog(Debug, fmt.Sprintf("Add part to queue: #%v", c.partNumber))
c.uploadQ <- &obj{
partNumber: c.partNumber,
buf: c.gzipBuf,
}
c.gzipBuf = c.gzipBuf[:0]
}
} else {
c.writeLog(Debug, fmt.Sprintf("Buffer size %v less than %v. Appending to previous part.", buf.Len(), c.UploadPartSize))
c.gzipBuf = append(c.gzipBuf, buf.Bytes()...)
// Add part to queue
c.writeLog(Debug, fmt.Sprintf("Add part to queue: #%v", c.partNumber-1))
c.uploadQ <- &obj{
partNumber: c.partNumber - 1,
buf: c.gzipBuf,
}
c.gzipBuf = c.gzipBuf[:0]
c.partNumber--
}
}
// UploadPart listens to upload queue. Whenever an obj is received,
// it is then uploaded to AWS.
// Abort operation is called if any error is received.
func (c *Converter) UploadPart() (err error) {
mu := &sync.RWMutex{}
for s3obj := range c.uploadQ {
err = c.uploadPart(s3obj.partNumber, s3obj.buf, mu)
if err != nil {
c.writeLog(Error, "Error occurred. Sending quit signal to writer.")
c.quit <- true
c.abortMultipartUpload()
return err
}
}
c.writeLog(Debug, "Received closed signal")
return
}
// writeLog decides whether to write a log to stdout depending on LogLevel.
func (c *Converter) writeLog(logLevel LogLevel, logLine string) {
if logLevel <= c.LogLevel {
log.Println(logLine)
}
}