1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 // Command arrow-ls displays the listing of an Arrow file.
21 // $> arrow-ls ./testdata/primitives.data
25 // - bools: type=bool, nullable
26 // - int8s: type=int8, nullable
27 // - int16s: type=int16, nullable
28 // - int32s: type=int32, nullable
29 // - int64s: type=int64, nullable
30 // - uint8s: type=uint8, nullable
31 // - uint16s: type=uint16, nullable
32 // - uint32s: type=uint32, nullable
33 // - uint64s: type=uint64, nullable
34 // - float32s: type=float32, nullable
35 // - float64s: type=float64, nullable
38 // $> gen-arrow-stream | arrow-ls
41 // - bools: type=bool, nullable
42 // - int8s: type=int8, nullable
43 // - int16s: type=int16, nullable
44 // - int32s: type=int32, nullable
45 // - int64s: type=int64, nullable
46 // - uint8s: type=uint8, nullable
47 // - uint16s: type=uint16, nullable
48 // - uint32s: type=uint32, nullable
49 // - uint64s: type=uint64, nullable
50 // - float32s: type=float32, nullable
51 // - float64s: type=float64, nullable
53 package main // import "github.com/apache/arrow/go/v6/arrow/ipc/cmd/arrow-ls"
63 "github.com/apache/arrow/go/v6/arrow/ipc"
64 "github.com/apache/arrow/go/v6/arrow/memory"
65 "golang.org/x/xerrors"
69 log.SetPrefix("arrow-ls: ")
77 err = processStream(os.Stdout, os.Stdin)
79 err = processFiles(os.Stdout, flag.Args())
86 func processStream(w io.Writer, rin io.Reader) error {
87 mem := memory.NewGoAllocator()
90 r, err := ipc.NewReader(rin, ipc.WithAllocator(mem))
92 if xerrors.Is(err, io.EOF) {
98 fmt.Fprintf(w, "%v\n", r.Schema())
104 fmt.Fprintf(w, "records: %d\n", nrecs)
110 func processFiles(w io.Writer, names []string) error {
111 for _, name := range names {
112 err := processFile(w, name)
120 func processFile(w io.Writer, fname string) error {
122 f, err := os.Open(fname)
128 hdr := make([]byte, len(ipc.Magic))
129 _, err = io.ReadFull(f, hdr)
131 return xerrors.Errorf("could not read file header: %w", err)
133 f.Seek(0, io.SeekStart)
135 if !bytes.Equal(hdr, ipc.Magic) {
137 return processStream(w, f)
140 mem := memory.NewGoAllocator()
142 r, err := ipc.NewFileReader(f, ipc.WithAllocator(mem))
144 if xerrors.Is(err, io.EOF) {
151 fmt.Fprintf(w, "version: %v\n", r.Version())
152 fmt.Fprintf(w, "%v\n", r.Schema())
153 fmt.Fprintf(w, "records: %d\n", r.NumRecords())
159 flag.Usage = func() {
160 fmt.Fprintf(os.Stderr, `Command arrow-ls displays the listing of an Arrow file.
162 Usage: arrow-ls [OPTIONS] [FILE1 [FILE2 [...]]]
166 $> arrow-ls ./testdata/primitives.data
170 - bools: type=bool, nullable
171 - int8s: type=int8, nullable
172 - int16s: type=int16, nullable
173 - int32s: type=int32, nullable
174 - int64s: type=int64, nullable
175 - uint8s: type=uint8, nullable
176 - uint16s: type=uint16, nullable
177 - uint32s: type=uint32, nullable
178 - uint64s: type=uint64, nullable
179 - float32s: type=float32, nullable
180 - float64s: type=float64, nullable
183 $> gen-arrow-stream | arrow-ls
186 - bools: type=bool, nullable
187 - int8s: type=int8, nullable
188 - int16s: type=int16, nullable
189 - int32s: type=int32, nullable
190 - int64s: type=int64, nullable
191 - uint8s: type=uint8, nullable
192 - uint16s: type=uint16, nullable
193 - uint32s: type=uint32, nullable
194 - uint64s: type=uint64, nullable
195 - float32s: type=float32, nullable
196 - float64s: type=float64, nullable