Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions c_glib/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 35 additions & 0 deletions c_glib/example/go/Makefile
Original file line number Diff line number Diff line change
@@ -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 $@ $<
78 changes: 78 additions & 0 deletions c_glib/example/go/README.md
Original file line number Diff line number Diff line change
@@ -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.
38 changes: 38 additions & 0 deletions c_glib/example/go/arrow-1.0/arrow.go.in
Original file line number Diff line number Diff line change
@@ -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>]
21 changes: 21 additions & 0 deletions c_glib/example/go/arrow-1.0/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
100 changes: 100 additions & 0 deletions c_glib/example/go/read-batch.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
101 changes: 101 additions & 0 deletions c_glib/example/go/read-stream.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading