Skip to content

Commit 74ad4a8

Browse files
kouwesm
authored andcommitted
ARROW-984: [GLib] Add Go examples
Author: Kouhei Sutou <[email protected]> Closes #661 from kou/glib-example-go and squashes the following commits: 22e35cb [Kouhei Sutou] [GLib] Add Go examples
1 parent af0c21e commit 74ad4a8

File tree

9 files changed

+702
-0
lines changed

9 files changed

+702
-0
lines changed

c_glib/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,8 @@ Makefile.in
4141
/example/build
4242
/example/read-batch
4343
/example/read-stream
44+
!/example/go/Makefile
45+
/example/go/read-batch
46+
/example/go/read-stream
47+
/example/go/write-batch
48+
/example/go/write-stream

c_glib/example/go/Makefile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License. See accompanying LICENSE file.
12+
13+
PROGRAMS = \
14+
read-batch \
15+
read-stream \
16+
write-batch \
17+
write-stream
18+
19+
all: build
20+
21+
generate:
22+
$$GOPATH/bin/gir-generator \
23+
-o $$GOPATH/src/gir/arrow-1.0 \
24+
-config arrow-1.0/config.json \
25+
arrow-1.0/arrow.go.in
26+
27+
build: $(PROGRAMS)
28+
29+
clean:
30+
rm -f $(PROGRAMS)
31+
32+
.SUFFIXES: .go
33+
34+
.go:
35+
go build -o $@ $<

c_glib/example/go/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<!---
2+
Licensed under the Apache License, Version 2.0 (the "License");
3+
you may not use this file except in compliance with the License.
4+
You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License. See accompanying LICENSE file.
13+
-->
14+
15+
# Arrow Go example
16+
17+
There are Go example codes in this directory.
18+
19+
## How to run
20+
21+
All example codes use
22+
[go-gir-generator](https://github.com/linuxdeepin/go-gir-generator) to
23+
use Arrow GLib based bindings.
24+
25+
See [../../README.md](../../README.md) how to install Arrow GLib. You
26+
can use packages to install Arrow GLib. The following instructions
27+
assumes that you've installed Arrow GLib by package. Package name is
28+
`libarrow-glib-dev` on Debian GNU/Linux and Ubuntu, `arrow-glib-devel`
29+
on CentOS.
30+
31+
Here are command lines to install go-gir-generator on Debian GNU/Linux
32+
and Ubuntu:
33+
34+
```text
35+
% sudo apt install -V -y libarrow-glib-dev golang git libgirepository1.0-dev libgudev-1.0-dev
36+
% export GOPATH=$HOME
37+
% go get github.com/linuxdeepin/go-gir-generator
38+
% cd $GOPATH/src/github.com/linuxdeepin/go-gir-generator
39+
% make build copyfile
40+
% mkdir -p $GOPATH/bin/
41+
% cp -a out/gir-generator $GOPATH/bin/
42+
% cp -a out/src/gir/ $GOPATH/src/
43+
```
44+
45+
Now, you can generate Arrow bindings for Go:
46+
47+
```text
48+
% git clone https://github.com/apache/arrow.git ~/arrow
49+
% cd ~/arrow/c_glib/example/go
50+
% make generate
51+
```
52+
53+
Then you can build all example codes:
54+
55+
```text
56+
% cd ~/arrow/c_glib/example/go
57+
% make
58+
% ./write-batch # Write data in batch mode
59+
% ./read-batch # Read the written batch mode data
60+
% ./write-stream # Write data in stream mode
61+
% ./read-stream # Read the written stream mode data
62+
```
63+
64+
## Go example codes
65+
66+
Here are example codes in this directory:
67+
68+
* `write-batch.go`: It shows how to write Arrow array to file in
69+
batch mode.
70+
71+
* `read-batch.go`: It shows how to read Arrow array from file in
72+
batch mode.
73+
74+
* `write-stream.go`: It shows how to write Arrow array to file in
75+
stream mode.
76+
77+
* `read-stream.go`: It shows how to read Arrow array from file in
78+
stream mode.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package arrow
19+
20+
/*
21+
#include "arrow.gen.h"
22+
#cgo pkg-config: arrow-glib
23+
24+
[<.g_list_funcs>]
25+
[<.g_error_free>]
26+
[<.g_free>]
27+
*/
28+
import "C"
29+
import "unsafe"
30+
31+
import (
32+
"gir/glib-2.0"
33+
"gir/gobject-2.0"
34+
)
35+
36+
[<.go_utils>]
37+
38+
[<.go_bindings>]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
{
19+
"namespace": "Arrow",
20+
"version": "1.0"
21+
}

c_glib/example/go/read-batch.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package main
19+
20+
import (
21+
"os"
22+
"log"
23+
"fmt"
24+
"strings"
25+
)
26+
27+
import "gir/arrow-1.0"
28+
29+
func PrintColumnValue(column *arrow.Array, i int64) {
30+
valueType := column.GetValueType()
31+
switch valueType {
32+
case arrow.TypeUint8:
33+
fmt.Print(arrow.ToUInt8Array(column).GetValue(i))
34+
case arrow.TypeUint16:
35+
fmt.Print(arrow.ToUInt16Array(column).GetValue(i))
36+
case arrow.TypeUint32:
37+
fmt.Print(arrow.ToUInt32Array(column).GetValue(i))
38+
case arrow.TypeUint64:
39+
fmt.Print(arrow.ToUInt64Array(column).GetValue(i))
40+
case arrow.TypeInt8:
41+
fmt.Print(arrow.ToInt8Array(column).GetValue(i))
42+
case arrow.TypeInt16:
43+
fmt.Print(arrow.ToInt16Array(column).GetValue(i))
44+
case arrow.TypeInt32:
45+
fmt.Print(arrow.ToInt32Array(column).GetValue(i))
46+
case arrow.TypeInt64:
47+
fmt.Print(arrow.ToInt64Array(column).GetValue(i))
48+
case arrow.TypeFloat:
49+
fmt.Print(arrow.ToFloatArray(column).GetValue(i))
50+
case arrow.TypeDouble:
51+
fmt.Print(arrow.ToDoubleArray(column).GetValue(i))
52+
default:
53+
fmt.Printf("unknown(%s)", valueType)
54+
}
55+
}
56+
57+
func PrintRecordBatch(recordBatch *arrow.RecordBatch) {
58+
nColumns := recordBatch.GetNColumns()
59+
for i := uint32(0); i < nColumns; i++ {
60+
column := recordBatch.GetColumn(i)
61+
columnName := recordBatch.GetColumnName(i)
62+
fmt.Printf(" %s: [", columnName)
63+
nRows := recordBatch.GetNRows()
64+
for j := int64(0); j < nRows; j++ {
65+
if j > 0 {
66+
fmt.Print(", ")
67+
}
68+
PrintColumnValue(column, j)
69+
}
70+
fmt.Println("]")
71+
}
72+
}
73+
74+
func main() {
75+
var path string
76+
if len(os.Args) < 2 {
77+
path = "/tmp/batch.arrow"
78+
} else {
79+
path = os.Args[1]
80+
}
81+
input, err := arrow.NewMemoryMappedInputStream(path);
82+
if err != nil {
83+
log.Fatalf("Failed to open path: <%s>: %v", path, err)
84+
}
85+
reader, err := arrow.NewFileReader(input)
86+
if err != nil {
87+
log.Fatalf("Failed to parse data: %v", err)
88+
}
89+
var i uint32
90+
nRecordBatches := reader.GetNRecordBatches()
91+
for i = 0; i < nRecordBatches; i++ {
92+
recordBatch, err := reader.GetRecordBatch(i)
93+
if err != nil {
94+
log.Fatalf("Failed to get record batch[%d]: %v", i, err)
95+
}
96+
fmt.Println(strings.Repeat("=", 40))
97+
fmt.Printf("record-batch[%d]:\n", i)
98+
PrintRecordBatch(recordBatch)
99+
}
100+
}

c_glib/example/go/read-stream.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package main
19+
20+
import (
21+
"os"
22+
"log"
23+
"fmt"
24+
"strings"
25+
)
26+
27+
import "gir/arrow-1.0"
28+
29+
func PrintColumnValue(column *arrow.Array, i int64) {
30+
valueType := column.GetValueType()
31+
switch valueType {
32+
case arrow.TypeUint8:
33+
fmt.Print(arrow.ToUInt8Array(column).GetValue(i))
34+
case arrow.TypeUint16:
35+
fmt.Print(arrow.ToUInt16Array(column).GetValue(i))
36+
case arrow.TypeUint32:
37+
fmt.Print(arrow.ToUInt32Array(column).GetValue(i))
38+
case arrow.TypeUint64:
39+
fmt.Print(arrow.ToUInt64Array(column).GetValue(i))
40+
case arrow.TypeInt8:
41+
fmt.Print(arrow.ToInt8Array(column).GetValue(i))
42+
case arrow.TypeInt16:
43+
fmt.Print(arrow.ToInt16Array(column).GetValue(i))
44+
case arrow.TypeInt32:
45+
fmt.Print(arrow.ToInt32Array(column).GetValue(i))
46+
case arrow.TypeInt64:
47+
fmt.Print(arrow.ToInt64Array(column).GetValue(i))
48+
case arrow.TypeFloat:
49+
fmt.Print(arrow.ToFloatArray(column).GetValue(i))
50+
case arrow.TypeDouble:
51+
fmt.Print(arrow.ToDoubleArray(column).GetValue(i))
52+
default:
53+
fmt.Printf("unknown(%s)", valueType)
54+
}
55+
}
56+
57+
func PrintRecordBatch(recordBatch *arrow.RecordBatch) {
58+
nColumns := recordBatch.GetNColumns()
59+
for i := uint32(0); i < nColumns; i++ {
60+
column := recordBatch.GetColumn(i)
61+
columnName := recordBatch.GetColumnName(i)
62+
fmt.Printf(" %s: [", columnName)
63+
nRows := recordBatch.GetNRows()
64+
for j := int64(0); j < nRows; j++ {
65+
if j > 0 {
66+
fmt.Print(", ")
67+
}
68+
PrintColumnValue(column, j)
69+
}
70+
fmt.Println("]")
71+
}
72+
}
73+
74+
func main() {
75+
var path string
76+
if len(os.Args) < 2 {
77+
path = "/tmp/stream.arrow"
78+
} else {
79+
path = os.Args[1]
80+
}
81+
input, err := arrow.NewMemoryMappedInputStream(path);
82+
if err != nil {
83+
log.Fatalf("Failed to open path: <%s>: %v", path, err)
84+
}
85+
reader, err := arrow.NewStreamReader(input)
86+
if err != nil {
87+
log.Fatalf("Failed to parse data: %v", err)
88+
}
89+
for i := 0; true; i++ {
90+
recordBatch, err := reader.GetNextRecordBatch()
91+
if err != nil {
92+
log.Fatalf("Failed to get next record batch: %v", err)
93+
}
94+
if recordBatch == nil {
95+
break
96+
}
97+
fmt.Println(strings.Repeat("=", 40))
98+
fmt.Printf("record-batch[%d]:\n", i)
99+
PrintRecordBatch(recordBatch)
100+
}
101+
}

0 commit comments

Comments
 (0)