/
readfile.go
142 lines (120 loc) · 3.45 KB
/
readfile.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
package cmds
import (
"fmt"
"io"
"log"
"os"
"sort"
"strings"
"text/tabwriter"
goparquet "github.com/fraugster/parquet-go"
"github.com/fraugster/parquet-go/parquetschema"
)
func catFile(w io.Writer, address string, n int) error {
fl, err := os.Open(address)
if err != nil {
return fmt.Errorf("can not open the file: %q", err)
}
defer fl.Close()
reader, err := goparquet.NewFileReader(fl)
if err != nil {
return fmt.Errorf("failed to read the parquet header: %q", err)
}
columnOrder := getColumnOrder(reader.GetSchemaDefinition())
for i := 0; (n == -1) || i < n; i++ {
data, err := reader.NextRow()
if err == io.EOF {
return nil
}
if err != nil {
log.Printf("Reading data failed with error, skip current row group: %q", err)
continue
}
printData(w, data, "", columnOrder)
fmt.Println()
}
return nil
}
func getColumnOrder(schemaDef *parquetschema.SchemaDefinition) map[string]int {
cols := getColumnList(schemaDef.RootColumn.Children, "")
colOrder := map[string]int{}
for idx, colName := range cols {
colOrder[colName] = idx
}
return colOrder
}
func getColumnList(colDefs []*parquetschema.ColumnDefinition, prefix string) []string {
cols := []string{}
for _, col := range colDefs {
cols = append(cols, prefix+col.SchemaElement.Name)
if col.Children != nil {
cols = append(cols, getColumnList(col.Children, col.SchemaElement.Name+".")...)
}
}
return cols
}
func printPrimitive(w io.Writer, ident, name string, v interface{}) {
_, _ = fmt.Fprintln(w, ident+name+" = "+fmt.Sprint(v))
}
func printData(w io.Writer, m map[string]interface{}, ident string, columnOrder map[string]int) {
cols := []string{}
for colName := range m {
cols = append(cols, colName)
}
sort.Slice(cols, func(i, j int) bool {
return columnOrder[ident+cols[i]] < columnOrder[ident+cols[j]]
})
for _, colName := range cols {
switch t := m[colName].(type) {
case map[string]interface{}:
_, _ = fmt.Fprintln(w, ident+colName+":")
printData(w, t, ident+".", columnOrder)
case []map[string]interface{}:
for j := range t {
_, _ = fmt.Fprintln(w, ident+colName+":")
printData(w, t[j], ident+".", columnOrder)
}
case []byte:
_, _ = fmt.Fprintln(w, ident+colName+" = "+string(t))
case [][]byte:
for j := range t {
_, _ = fmt.Fprintln(w, ident+colName+" = "+string(t[j]))
}
case []interface{}:
for j := range t {
_, _ = fmt.Fprintln(w, ident+colName+" = "+fmt.Sprint(t[j]))
}
default:
printPrimitive(w, ident, colName, t)
}
}
}
func metaFile(w io.Writer, address string) error {
fl, err := os.Open(address)
if err != nil {
return fmt.Errorf("can not open the file: %q", err)
}
defer fl.Close()
reader, err := goparquet.NewFileReader(fl)
if err != nil {
return fmt.Errorf("failed to read the parquet header: %q", err)
}
cols := reader.Columns()
writer := tabwriter.NewWriter(w, 8, 8, 0, '\t', 0)
printFlatSchema(writer, cols, 0)
return writer.Flush()
}
func printFlatSchema(w io.Writer, cols []*goparquet.Column, lvl int) {
dot := strings.Repeat(".", lvl)
for _, column := range cols {
_, _ = fmt.Fprintf(w, "%s%s:\t\t", dot, column.Name())
_, _ = fmt.Fprintf(w, "%s ", column.RepetitionType().String())
if column.DataColumn() {
_, _ = fmt.Fprintf(w, "%s R:%d D:%d\n", column.Type().String(), column.MaxRepetitionLevel(), column.MaxDefinitionLevel())
continue
} else {
_, _ = fmt.Fprintf(w, "F:%d\n", column.ChildrenCount())
printFlatSchema(w, column.Children(), lvl+1)
}
}
}