forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 23
/
read-batch.go
100 lines (94 loc) · 2.88 KB
/
read-batch.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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package main
import (
"os"
"log"
"fmt"
"strings"
)
import "gir/arrow-1.0"
func PrintColumnValue(column *arrow.Array, i int64) {
valueType := column.GetValueType()
switch valueType {
case arrow.TypeUint8:
fmt.Print(arrow.ToUInt8Array(column).GetValue(i))
case arrow.TypeUint16:
fmt.Print(arrow.ToUInt16Array(column).GetValue(i))
case arrow.TypeUint32:
fmt.Print(arrow.ToUInt32Array(column).GetValue(i))
case arrow.TypeUint64:
fmt.Print(arrow.ToUInt64Array(column).GetValue(i))
case arrow.TypeInt8:
fmt.Print(arrow.ToInt8Array(column).GetValue(i))
case arrow.TypeInt16:
fmt.Print(arrow.ToInt16Array(column).GetValue(i))
case arrow.TypeInt32:
fmt.Print(arrow.ToInt32Array(column).GetValue(i))
case arrow.TypeInt64:
fmt.Print(arrow.ToInt64Array(column).GetValue(i))
case arrow.TypeFloat:
fmt.Print(arrow.ToFloatArray(column).GetValue(i))
case arrow.TypeDouble:
fmt.Print(arrow.ToDoubleArray(column).GetValue(i))
default:
fmt.Printf("unknown(%s)", valueType)
}
}
func PrintRecordBatch(recordBatch *arrow.RecordBatch) {
nColumns := recordBatch.GetNColumns()
for i := uint32(0); i < nColumns; i++ {
column := recordBatch.GetColumn(int32(i))
columnName := recordBatch.GetColumnName(int32(i))
fmt.Printf(" %s: [", columnName)
nRows := recordBatch.GetNRows()
for j := int64(0); j < nRows; j++ {
if j > 0 {
fmt.Print(", ")
}
PrintColumnValue(column, j)
}
fmt.Println("]")
}
}
func main() {
var path string
if len(os.Args) < 2 {
path = "/tmp/batch.arrow"
} else {
path = os.Args[1]
}
input, err := arrow.NewMemoryMappedInputStream(path);
if err != nil {
log.Fatalf("Failed to open path: <%s>: %v", path, err)
}
reader, err := arrow.NewRecordBatchFileReader(input)
if err != nil {
log.Fatalf("Failed to parse data: %v", err)
}
var i uint32
nRecordBatches := reader.GetNRecordBatches()
for i = 0; i < nRecordBatches; i++ {
recordBatch, err := reader.ReadRecordBatch(i)
if err != nil {
log.Fatalf("Failed to read record batch[%d]: %v", i, err)
}
fmt.Println(strings.Repeat("=", 40))
fmt.Printf("record-batch[%d]:\n", i)
PrintRecordBatch(recordBatch)
}
}