-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
188 lines (164 loc) · 5.2 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"math"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
parquets3 "github.com/xitongsys/parquet-go-source/s3"
"github.com/xitongsys/parquet-go/reader"
"github.com/xitongsys/parquet-go/writer"
)
type customerICE struct {
CustomerID *int64 `parquet:"name=customer_id, type=INT64, repetitiontype=OPTIONAL"`
Compensationfactor *int32 `parquet:"name=compensation_rate, type=INT32, convertedtype=DECIMAL, scale=6, precision=8, repetitiontype=OPTIONAL"`
Tier *string `parquet:"name=tier, type=BYTE_ARRAY, convertedtype=UTF8, repetitiontype=OPTIONAL"`
Allocation *string `parquet:"name=allocation, type=BYTE_ARRAY, convertedtype=UTF8, repetitiontype=OPTIONAL"`
}
type CompensationConverted struct {
CustomerID string
Multiplier float64
}
const (
numRows = 20
)
func main() {
// s3Example provides a sample write and read using the S3 Parquet File
ctx := context.Background()
bucket := "mesh-pa-consumer"
key := "test/foobar.parquet"
num := 100
manager := SetupForTesting()
// create new S3 file writer
fw, err := parquets3.NewS3FileWriterWithClient(ctx, manager.client, bucket, key, "bucket-owner-full-control", nil)
if err != nil {
log.Println("Can't open file", err)
return
}
// create new parquet file writer
pw, err := writer.NewParquetWriter(fw, new(customerICE), 4)
if err != nil {
log.Println("Can't create parquet writer", err)
return
}
// write 100 student records to the parquet file
for i := 0; i < num; i++ {
Allocation := "Irrelevant"
Tier := "Irrelevant"
stu := customerICE{
CustomerID: create64(1234),
Compensationfactor: create32(00100000),
Allocation: &Allocation,
Tier: &Tier,
}
if err = pw.Write(stu); err != nil {
log.Println("Write error", err)
}
}
// write parquet file footer
if err = pw.WriteStop(); err != nil {
log.Println("WriteStop err", err)
}
err = fw.Close()
if err != nil {
log.Println("Error closing S3 file writer")
log.Println(err)
}
log.Println("Write Finished")
pw.WriteStop()
fw.Close()
Manager := SetupForTesting()
testreader, err := parquets3.NewS3FileReaderWithClient(ctx, Manager.client, "mesh-pa-consumer", "test/foobar.parquet")
if err != nil {
log.Println(err)
}
fmt.Println("Created Reader, trying to download something")
prs3, err := reader.NewParquetReader(testreader, new(customerICE), 4)
if err != nil {
log.Println("Can't create parquet reader", err)
return
}
num = int(prs3.GetNumRows())
converted := make([]CompensationConverted, 0)
for i := 0; i < num/10; i++ {
structArray := make([]customerICE, 10) //read 10 rows
if err = prs3.Read(&structArray); err != nil {
log.Println("Read error", err)
}
converted = append(converted, CustomerICEToCompensationConverted(structArray)...)
//log.Println(stus)
}
numRemaining := num % (num / numRows)
structArray := make([]customerICE, numRemaining) //read 10 rows
if err = prs3.Read(&structArray); err != nil {
fmt.Print(err)
}
converted = append(converted, CustomerICEToCompensationConverted(structArray)...)
fmt.Print(converted)
prs3.ReadStop()
}
func create64(x int64) *int64 {
return &x
}
func create32(x int32) *int32 {
return &x
}
type S3Manager struct {
client *s3.S3
}
func SetupForTesting() *S3Manager {
awsConf := &aws.Config{
Credentials: credentials.NewStaticCredentials("minio_access_key", "minio_secret_key", ""),
Endpoint: aws.String("http://127.0.0.1:9000"),
Region: aws.String("eu-west-1"),
DisableSSL: aws.Bool(true),
S3ForcePathStyle: aws.Bool(true),
}
client := s3.New(session.Must(session.NewSession(awsConf)))
_, err := client.CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String("mesh-pa-consumer"),
})
if err != nil {
er := err.(awserr.Error)
if er.Code() != s3.ErrCodeBucketAlreadyExists && er.Code() != s3.ErrCodeBucketAlreadyOwnedByYou {
panic(err)
}
}
return &S3Manager{
client: s3.New(session.Must(session.NewSession(awsConf))),
}
}
func DECIMAL_INT_ToString(dec int32, precision int, scale int) string {
s := int(math.Pow10(scale))
integer, fraction := int(dec)/s, int(dec)%s
ans := strconv.Itoa(integer)
if scale > 0 {
ans += "." + strconv.Itoa(fraction)
}
return ans
}
func IntToFloat(toConvert int32) float64 {
result := float64(toConvert) / float64(math.Pow10(6))
return result
}
func CustomerICEToCompensationConverted(ICEs []customerICE) []CompensationConverted {
converted := make([]CompensationConverted, len(ICEs))
for index, data := range ICEs {
conv := CompensationConverted{
CustomerID: strconv.Itoa(int(*data.CustomerID)),
Multiplier: intToFloat(*data.Compensationfactor),
}
converted[index] = conv
}
return converted
}
// Converts the int32 of given parquetfile into the float64 that we save
func intToFloat(toConvert int32) float64 {
result := float64(toConvert) / float64(math.Pow10(6))
return result
}