Repository: arrow Updated Branches: refs/heads/master af0c21e9a -> 74ad4a82b
ARROW-984: [GLib] Add Go examples Author: Kouhei Sutou <k...@clear-code.com> Closes #661 from kou/glib-example-go and squashes the following commits: 22e35cb [Kouhei Sutou] [GLib] Add Go examples Project: http://git-wip-us.apache.org/repos/asf/arrow/repo Commit: http://git-wip-us.apache.org/repos/asf/arrow/commit/74ad4a82 Tree: http://git-wip-us.apache.org/repos/asf/arrow/tree/74ad4a82 Diff: http://git-wip-us.apache.org/repos/asf/arrow/diff/74ad4a82 Branch: refs/heads/master Commit: 74ad4a82b1c6c376809dd0e2a2be9fe2fa54a9ac Parents: af0c21e Author: Kouhei Sutou <k...@clear-code.com> Authored: Tue May 9 08:58:59 2017 -0400 Committer: Wes McKinney <wes.mckin...@twosigma.com> Committed: Tue May 9 08:58:59 2017 -0400 ---------------------------------------------------------------------- c_glib/.gitignore | 5 + c_glib/example/go/Makefile | 35 ++++++ c_glib/example/go/README.md | 78 +++++++++++++ c_glib/example/go/arrow-1.0/arrow.go.in | 38 +++++++ c_glib/example/go/arrow-1.0/config.json | 21 ++++ c_glib/example/go/read-batch.go | 100 +++++++++++++++++ c_glib/example/go/read-stream.go | 101 +++++++++++++++++ c_glib/example/go/write-batch.go | 162 +++++++++++++++++++++++++++ c_glib/example/go/write-stream.go | 162 +++++++++++++++++++++++++++ 9 files changed, 702 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/arrow/blob/74ad4a82/c_glib/.gitignore ---------------------------------------------------------------------- diff --git a/c_glib/.gitignore b/c_glib/.gitignore index 6f2de80..796b842 100644 --- a/c_glib/.gitignore +++ b/c_glib/.gitignore @@ -41,3 +41,8 @@ Makefile.in /example/build /example/read-batch /example/read-stream +!/example/go/Makefile +/example/go/read-batch +/example/go/read-stream +/example/go/write-batch +/example/go/write-stream http://git-wip-us.apache.org/repos/asf/arrow/blob/74ad4a82/c_glib/example/go/Makefile ---------------------------------------------------------------------- diff --git a/c_glib/example/go/Makefile b/c_glib/example/go/Makefile new file mode 100644 index 0000000..d883112 --- /dev/null +++ b/c_glib/example/go/Makefile @@ -0,0 +1,35 @@ +# Licensed 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. See accompanying LICENSE file. + +PROGRAMS = \ + read-batch \ + read-stream \ + write-batch \ + write-stream + +all: build + +generate: + $$GOPATH/bin/gir-generator \ + -o $$GOPATH/src/gir/arrow-1.0 \ + -config arrow-1.0/config.json \ + arrow-1.0/arrow.go.in + +build: $(PROGRAMS) + +clean: + rm -f $(PROGRAMS) + +.SUFFIXES: .go + +.go: + go build -o $@ $< http://git-wip-us.apache.org/repos/asf/arrow/blob/74ad4a82/c_glib/example/go/README.md ---------------------------------------------------------------------- diff --git a/c_glib/example/go/README.md b/c_glib/example/go/README.md new file mode 100644 index 0000000..2054055 --- /dev/null +++ b/c_glib/example/go/README.md @@ -0,0 +1,78 @@ +<!--- + Licensed 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. See accompanying LICENSE file. +--> + +# Arrow Go example + +There are Go example codes in this directory. + +## How to run + +All example codes use +[go-gir-generator](https://github.com/linuxdeepin/go-gir-generator) to +use Arrow GLib based bindings. + +See [../../README.md](../../README.md) how to install Arrow GLib. You +can use packages to install Arrow GLib. The following instructions +assumes that you've installed Arrow GLib by package. Package name is +`libarrow-glib-dev` on Debian GNU/Linux and Ubuntu, `arrow-glib-devel` +on CentOS. + +Here are command lines to install go-gir-generator on Debian GNU/Linux +and Ubuntu: + +```text +% sudo apt install -V -y libarrow-glib-dev golang git libgirepository1.0-dev libgudev-1.0-dev +% export GOPATH=$HOME +% go get github.com/linuxdeepin/go-gir-generator +% cd $GOPATH/src/github.com/linuxdeepin/go-gir-generator +% make build copyfile +% mkdir -p $GOPATH/bin/ +% cp -a out/gir-generator $GOPATH/bin/ +% cp -a out/src/gir/ $GOPATH/src/ +``` + +Now, you can generate Arrow bindings for Go: + +```text +% git clone https://github.com/apache/arrow.git ~/arrow +% cd ~/arrow/c_glib/example/go +% make generate +``` + +Then you can build all example codes: + +```text +% cd ~/arrow/c_glib/example/go +% make +% ./write-batch # Write data in batch mode +% ./read-batch # Read the written batch mode data +% ./write-stream # Write data in stream mode +% ./read-stream # Read the written stream mode data +``` + +## Go example codes + +Here are example codes in this directory: + + * `write-batch.go`: It shows how to write Arrow array to file in + batch mode. + + * `read-batch.go`: It shows how to read Arrow array from file in + batch mode. + + * `write-stream.go`: It shows how to write Arrow array to file in + stream mode. + + * `read-stream.go`: It shows how to read Arrow array from file in + stream mode. http://git-wip-us.apache.org/repos/asf/arrow/blob/74ad4a82/c_glib/example/go/arrow-1.0/arrow.go.in ---------------------------------------------------------------------- diff --git a/c_glib/example/go/arrow-1.0/arrow.go.in b/c_glib/example/go/arrow-1.0/arrow.go.in new file mode 100644 index 0000000..bd124af --- /dev/null +++ b/c_glib/example/go/arrow-1.0/arrow.go.in @@ -0,0 +1,38 @@ +// 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 arrow + +/* +#include "arrow.gen.h" +#cgo pkg-config: arrow-glib + +[<.g_list_funcs>] +[<.g_error_free>] +[<.g_free>] +*/ +import "C" +import "unsafe" + +import ( + "gir/glib-2.0" + "gir/gobject-2.0" +) + +[<.go_utils>] + +[<.go_bindings>] http://git-wip-us.apache.org/repos/asf/arrow/blob/74ad4a82/c_glib/example/go/arrow-1.0/config.json ---------------------------------------------------------------------- diff --git a/c_glib/example/go/arrow-1.0/config.json b/c_glib/example/go/arrow-1.0/config.json new file mode 100644 index 0000000..eea9a89 --- /dev/null +++ b/c_glib/example/go/arrow-1.0/config.json @@ -0,0 +1,21 @@ +// 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. + +{ + "namespace": "Arrow", + "version": "1.0" +} http://git-wip-us.apache.org/repos/asf/arrow/blob/74ad4a82/c_glib/example/go/read-batch.go ---------------------------------------------------------------------- diff --git a/c_glib/example/go/read-batch.go b/c_glib/example/go/read-batch.go new file mode 100644 index 0000000..3ad8423 --- /dev/null +++ b/c_glib/example/go/read-batch.go @@ -0,0 +1,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(i) + columnName := recordBatch.GetColumnName(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.NewFileReader(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.GetRecordBatch(i) + if err != nil { + log.Fatalf("Failed to get record batch[%d]: %v", i, err) + } + fmt.Println(strings.Repeat("=", 40)) + fmt.Printf("record-batch[%d]:\n", i) + PrintRecordBatch(recordBatch) + } +} http://git-wip-us.apache.org/repos/asf/arrow/blob/74ad4a82/c_glib/example/go/read-stream.go ---------------------------------------------------------------------- diff --git a/c_glib/example/go/read-stream.go b/c_glib/example/go/read-stream.go new file mode 100644 index 0000000..f336798 --- /dev/null +++ b/c_glib/example/go/read-stream.go @@ -0,0 +1,101 @@ +// 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(i) + columnName := recordBatch.GetColumnName(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/stream.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.NewStreamReader(input) + if err != nil { + log.Fatalf("Failed to parse data: %v", err) + } + for i := 0; true; i++ { + recordBatch, err := reader.GetNextRecordBatch() + if err != nil { + log.Fatalf("Failed to get next record batch: %v", err) + } + if recordBatch == nil { + break + } + fmt.Println(strings.Repeat("=", 40)) + fmt.Printf("record-batch[%d]:\n", i) + PrintRecordBatch(recordBatch) + } +} http://git-wip-us.apache.org/repos/asf/arrow/blob/74ad4a82/c_glib/example/go/write-batch.go ---------------------------------------------------------------------- diff --git a/c_glib/example/go/write-batch.go b/c_glib/example/go/write-batch.go new file mode 100644 index 0000000..f5e946a --- /dev/null +++ b/c_glib/example/go/write-batch.go @@ -0,0 +1,162 @@ +// 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" +) + +import "gir/arrow-1.0" + +func BuildUInt8Array() *arrow.Array { + builder := arrow.NewUInt8ArrayBuilder() + for _, value := range []uint8{1, 2, 4, 8} { + builder.Append(value) + } + return builder.Finish() +} + +func BuildUInt16Array() *arrow.Array { + builder := arrow.NewUInt16ArrayBuilder() + for _, value := range []uint16{1, 2, 4, 8} { + builder.Append(value) + } + return builder.Finish() +} + +func BuildUInt32Array() *arrow.Array { + builder := arrow.NewUInt32ArrayBuilder() + for _, value := range []uint32{1, 2, 4, 8} { + builder.Append(value) + } + return builder.Finish() +} + +func BuildUInt64Array() *arrow.Array { + builder := arrow.NewUInt64ArrayBuilder() + for _, value := range []uint64{1, 2, 4, 8} { + builder.Append(value) + } + return builder.Finish() +} + +func BuildInt8Array() *arrow.Array { + builder := arrow.NewInt8ArrayBuilder() + for _, value := range []int8{1, -2, 4, -8} { + builder.Append(value) + } + return builder.Finish() +} + +func BuildInt16Array() *arrow.Array { + builder := arrow.NewInt16ArrayBuilder() + for _, value := range []int16{1, -2, 4, -8} { + builder.Append(value) + } + return builder.Finish() +} + +func BuildInt32Array() *arrow.Array { + builder := arrow.NewInt32ArrayBuilder() + for _, value := range []int32{1, -2, 4, -8} { + builder.Append(value) + } + return builder.Finish() +} + +func BuildInt64Array() *arrow.Array { + builder := arrow.NewInt64ArrayBuilder() + for _, value := range []int64{1, -2, 4, -8} { + builder.Append(value) + } + return builder.Finish() +} + +func BuildFloatArray() *arrow.Array { + builder := arrow.NewFloatArrayBuilder() + for _, value := range []float32{1.1, -2.2, 4.4, -8.8} { + builder.Append(value) + } + return builder.Finish() +} + +func BuildDoubleArray() *arrow.Array { + builder := arrow.NewDoubleArrayBuilder() + for _, value := range []float64{1.1, -2.2, 4.4, -8.8} { + builder.Append(value) + } + return builder.Finish() +} + +func main() { + var output_path string + if len(os.Args) < 2 { + output_path = "/tmp/batch.arrow" + } else { + output_path = os.Args[1] + } + + fields := []*arrow.Field{ + arrow.NewField("uint8", arrow.NewUInt8DataType()), + arrow.NewField("uint16", arrow.NewUInt16DataType()), + arrow.NewField("uint32", arrow.NewUInt32DataType()), + arrow.NewField("uint64", arrow.NewUInt64DataType()), + arrow.NewField("int8", arrow.NewInt8DataType()), + arrow.NewField("int16", arrow.NewInt16DataType()), + arrow.NewField("int32", arrow.NewInt32DataType()), + arrow.NewField("int64", arrow.NewInt64DataType()), + arrow.NewField("float", arrow.NewFloatDataType()), + arrow.NewField("double", arrow.NewDoubleDataType()), + } + schema := arrow.NewSchema(fields) + + output, err := arrow.NewFileOutputStream(output_path, false) + if err != nil { + log.Fatalf("Failed to open path: <%s>: %v", output_path, err) + } + writer, err := arrow.NewFileWriter(output, schema) + if err != nil { + log.Fatalf("Failed to create writer: %v", err) + } + + columns := []*arrow.Array{ + BuildUInt8Array(), + BuildUInt16Array(), + BuildUInt32Array(), + BuildUInt64Array(), + BuildInt8Array(), + BuildInt16Array(), + BuildInt32Array(), + BuildInt64Array(), + BuildFloatArray(), + BuildDoubleArray(), + } + + recordBatch := arrow.NewRecordBatch(schema, 4, columns) + writer.WriteRecordBatch(recordBatch) + + slicedColumns := make([]*arrow.Array, len(columns)) + for i, column := range columns { + slicedColumns[i] = column.Slice(1, 3) + } + recordBatch = arrow.NewRecordBatch(schema, 3, slicedColumns) + writer.WriteRecordBatch(recordBatch) + + writer.Close() +} http://git-wip-us.apache.org/repos/asf/arrow/blob/74ad4a82/c_glib/example/go/write-stream.go ---------------------------------------------------------------------- diff --git a/c_glib/example/go/write-stream.go b/c_glib/example/go/write-stream.go new file mode 100644 index 0000000..80ebd2b --- /dev/null +++ b/c_glib/example/go/write-stream.go @@ -0,0 +1,162 @@ +// 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" +) + +import "gir/arrow-1.0" + +func BuildUInt8Array() *arrow.Array { + builder := arrow.NewUInt8ArrayBuilder() + for _, value := range []uint8{1, 2, 4, 8} { + builder.Append(value) + } + return builder.Finish() +} + +func BuildUInt16Array() *arrow.Array { + builder := arrow.NewUInt16ArrayBuilder() + for _, value := range []uint16{1, 2, 4, 8} { + builder.Append(value) + } + return builder.Finish() +} + +func BuildUInt32Array() *arrow.Array { + builder := arrow.NewUInt32ArrayBuilder() + for _, value := range []uint32{1, 2, 4, 8} { + builder.Append(value) + } + return builder.Finish() +} + +func BuildUInt64Array() *arrow.Array { + builder := arrow.NewUInt64ArrayBuilder() + for _, value := range []uint64{1, 2, 4, 8} { + builder.Append(value) + } + return builder.Finish() +} + +func BuildInt8Array() *arrow.Array { + builder := arrow.NewInt8ArrayBuilder() + for _, value := range []int8{1, -2, 4, -8} { + builder.Append(value) + } + return builder.Finish() +} + +func BuildInt16Array() *arrow.Array { + builder := arrow.NewInt16ArrayBuilder() + for _, value := range []int16{1, -2, 4, -8} { + builder.Append(value) + } + return builder.Finish() +} + +func BuildInt32Array() *arrow.Array { + builder := arrow.NewInt32ArrayBuilder() + for _, value := range []int32{1, -2, 4, -8} { + builder.Append(value) + } + return builder.Finish() +} + +func BuildInt64Array() *arrow.Array { + builder := arrow.NewInt64ArrayBuilder() + for _, value := range []int64{1, -2, 4, -8} { + builder.Append(value) + } + return builder.Finish() +} + +func BuildFloatArray() *arrow.Array { + builder := arrow.NewFloatArrayBuilder() + for _, value := range []float32{1.1, -2.2, 4.4, -8.8} { + builder.Append(value) + } + return builder.Finish() +} + +func BuildDoubleArray() *arrow.Array { + builder := arrow.NewDoubleArrayBuilder() + for _, value := range []float64{1.1, -2.2, 4.4, -8.8} { + builder.Append(value) + } + return builder.Finish() +} + +func main() { + var output_path string + if len(os.Args) < 2 { + output_path = "/tmp/stream.arrow" + } else { + output_path = os.Args[1] + } + + fields := []*arrow.Field{ + arrow.NewField("uint8", arrow.NewUInt8DataType()), + arrow.NewField("uint16", arrow.NewUInt16DataType()), + arrow.NewField("uint32", arrow.NewUInt32DataType()), + arrow.NewField("uint64", arrow.NewUInt64DataType()), + arrow.NewField("int8", arrow.NewInt8DataType()), + arrow.NewField("int16", arrow.NewInt16DataType()), + arrow.NewField("int32", arrow.NewInt32DataType()), + arrow.NewField("int64", arrow.NewInt64DataType()), + arrow.NewField("float", arrow.NewFloatDataType()), + arrow.NewField("double", arrow.NewDoubleDataType()), + } + schema := arrow.NewSchema(fields) + + output, err := arrow.NewFileOutputStream(output_path, false) + if err != nil { + log.Fatalf("Failed to open path: <%s>: %v", output_path, err) + } + writer, err := arrow.NewStreamWriter(output, schema) + if err != nil { + log.Fatalf("Failed to create writer: %v", err) + } + + columns := []*arrow.Array{ + BuildUInt8Array(), + BuildUInt16Array(), + BuildUInt32Array(), + BuildUInt64Array(), + BuildInt8Array(), + BuildInt16Array(), + BuildInt32Array(), + BuildInt64Array(), + BuildFloatArray(), + BuildDoubleArray(), + } + + recordBatch := arrow.NewRecordBatch(schema, 4, columns) + writer.WriteRecordBatch(recordBatch) + + slicedColumns := make([]*arrow.Array, len(columns)) + for i, column := range columns { + slicedColumns[i] = column.Slice(1, 3) + } + recordBatch = arrow.NewRecordBatch(schema, 3, slicedColumns) + writer.WriteRecordBatch(recordBatch) + + writer.Close() +}