forked from benbpyle/go-parquet-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
78 lines (63 loc) · 1.56 KB
/
reader.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
package main
import (
"context"
"errors"
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/fraugster/parquet-go/floor"
_ "github.com/fraugster/parquet-go/floor"
"github.com/segmentio/ksuid"
log "github.com/sirupsen/logrus"
)
var ErrIllegalRow = errors.New("row not fully formed")
func DownloadFile(ctx context.Context, sess *session.Session, bucket string, key string) (string, error) {
log.WithFields(log.Fields{
"key": key,
"bucket": bucket,
}).Debug("printing out the args")
filePath := fmt.Sprintf("files/%s.parquet", ksuid.New().String())
file, err := os.Create(filePath)
if err != nil {
return "", err
}
downloader := s3manager.NewDownloader(sess)
_, err = downloader.DownloadWithContext(ctx, file,
&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
return "", err
}
return filePath, nil
}
func DeleteFile(fileName string) error {
err := os.Remove(fileName)
if err != nil {
return err
}
return nil
}
func ParseFile(fileName string) ([]ParquetUser, error) {
fr, err := floor.NewFileReader(fileName)
var fileContent []ParquetUser
if err != nil {
return nil, err
}
for fr.Next() {
rec := &ParquetUser{}
if err := fr.Scan(rec); err != nil {
// continue along is it's just a malformed row
if errors.Is(err, ErrIllegalRow) {
continue
}
return nil, err
}
fileContent = append(fileContent, *rec)
}
return fileContent, nil
}