You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by we...@apache.org on 2017/04/11 14:17:48 UTC

arrow git commit: ARROW-802: [GLib] Add read examples

Repository: arrow
Updated Branches:
  refs/heads/master 06d92bbab -> 85b870e72


ARROW-802: [GLib] Add read examples

Author: Kouhei Sutou <ko...@clear-code.com>

Closes #522 from kou/glib-add-read-examples and squashes the following commits:

3fd5a2f [Kouhei Sutou] [GLib] Add read examples


Project: http://git-wip-us.apache.org/repos/asf/arrow/repo
Commit: http://git-wip-us.apache.org/repos/asf/arrow/commit/85b870e7
Tree: http://git-wip-us.apache.org/repos/asf/arrow/tree/85b870e7
Diff: http://git-wip-us.apache.org/repos/asf/arrow/diff/85b870e7

Branch: refs/heads/master
Commit: 85b870e72803641568ff260af2306d9fc993a6d4
Parents: 06d92bb
Author: Kouhei Sutou <ko...@clear-code.com>
Authored: Tue Apr 11 10:17:43 2017 -0400
Committer: Wes McKinney <we...@twosigma.com>
Committed: Tue Apr 11 10:17:43 2017 -0400

----------------------------------------------------------------------
 c_glib/.gitignore            |   2 +
 c_glib/configure.ac          |   2 +
 c_glib/example/Makefile.am   |  15 +++-
 c_glib/example/README.md     |  42 +++++++++++
 c_glib/example/read-batch.c  | 144 ++++++++++++++++++++++++++++++++++++++
 c_glib/example/read-stream.c | 143 +++++++++++++++++++++++++++++++++++++
 6 files changed, 347 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/arrow/blob/85b870e7/c_glib/.gitignore
----------------------------------------------------------------------
diff --git a/c_glib/.gitignore b/c_glib/.gitignore
index e57a059..8928158 100644
--- a/c_glib/.gitignore
+++ b/c_glib/.gitignore
@@ -43,3 +43,5 @@ Makefile.in
 /arrow-glib/stamp-*
 /arrow-glib/*.pc
 /example/build
+/example/read-batch
+/example/read-stream

http://git-wip-us.apache.org/repos/asf/arrow/blob/85b870e7/c_glib/configure.ac
----------------------------------------------------------------------
diff --git a/c_glib/configure.ac b/c_glib/configure.ac
index d63132e..f367192 100644
--- a/c_glib/configure.ac
+++ b/c_glib/configure.ac
@@ -87,6 +87,8 @@ else
   AC_SUBST(ARROW_LIBS)
 fi
 
+exampledir="\$(datadir)/arrow-glib/example"
+AC_SUBST(exampledir)
 
 AC_CONFIG_FILES([
   Makefile

http://git-wip-us.apache.org/repos/asf/arrow/blob/85b870e7/c_glib/example/Makefile.am
----------------------------------------------------------------------
diff --git a/c_glib/example/Makefile.am b/c_glib/example/Makefile.am
index 3d456d7..8bf3c15 100644
--- a/c_glib/example/Makefile.am
+++ b/c_glib/example/Makefile.am
@@ -28,7 +28,20 @@ AM_LDFLAGS =						\
 	$(builddir)/../arrow-glib/libarrow-glib.la
 
 noinst_PROGRAMS =				\
-	build
+	build					\
+	read-batch				\
+	read-stream
 
 build_SOURCES =					\
 	build.c
+
+read_batch_SOURCES =				\
+	read-batch.c
+
+read_stream_SOURCES =				\
+	read-stream.c
+
+example_DATA =					\
+	$(build_SOURCES)			\
+	$(read_batch_SOURCES)			\
+	$(read_stream_SOURCES)

http://git-wip-us.apache.org/repos/asf/arrow/blob/85b870e7/c_glib/example/README.md
----------------------------------------------------------------------
diff --git a/c_glib/example/README.md b/c_glib/example/README.md
new file mode 100644
index 0000000..b1ba259
--- /dev/null
+++ b/c_glib/example/README.md
@@ -0,0 +1,42 @@
+<!---
+  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 GLib example
+
+There are example codes in this directory.
+
+C example codes exist in this directory.
+
+## C example codes
+
+Here are example codes in this directory:
+
+  * `build.c`: It shows how to create an array by array builder.
+
+<!---
+  * `write-batch.c`: It shows how to write Arrow array to file in batch
+    mode.
+-->
+
+  * `read-batch.c`: It shows how to read Arrow array from file in batch
+    mode.
+
+<!---
+  * `write-stream.c`: It shows how to write Arrow array to file in
+    stream mode.
+-->
+
+  * `read-stream.c`: It shows how to read Arrow array from file in
+    stream mode.
+

http://git-wip-us.apache.org/repos/asf/arrow/blob/85b870e7/c_glib/example/read-batch.c
----------------------------------------------------------------------
diff --git a/c_glib/example/read-batch.c b/c_glib/example/read-batch.c
new file mode 100644
index 0000000..a55b085
--- /dev/null
+++ b/c_glib/example/read-batch.c
@@ -0,0 +1,144 @@
+/*
+ * 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.
+ */
+
+#include <stdlib.h>
+
+#include <arrow-glib/arrow-glib.h>
+
+static void
+print_array(GArrowArray *array)
+{
+  GArrowType value_type;
+  gint64 i, n;
+
+  value_type = garrow_array_get_value_type(array);
+
+  g_print("[");
+  n = garrow_array_get_length(array);
+
+#define ARRAY_CASE(type, Type, TYPE, format)                            \
+  case GARROW_TYPE_ ## TYPE:                                            \
+    {                                                                   \
+      GArrow ## Type ## Array *real_array;                              \
+      real_array = GARROW_ ## TYPE ## _ARRAY(array);                    \
+      for (i = 0; i < n; i++) {                                         \
+        if (i > 0) {                                                    \
+          g_print(", ");                                                \
+        }                                                               \
+        g_print(format,                                                 \
+                garrow_ ## type ## _array_get_value(real_array, i));    \
+      }                                                                 \
+    }                                                                   \
+    break
+
+  switch (value_type) {
+    ARRAY_CASE(uint8,  UInt8,  UINT8,  "%hhu");
+    ARRAY_CASE(uint16, UInt16, UINT16, "%" G_GUINT16_FORMAT);
+    ARRAY_CASE(uint32, UInt32, UINT32, "%" G_GUINT32_FORMAT);
+    ARRAY_CASE(uint64, UInt64, UINT64, "%" G_GUINT64_FORMAT);
+    ARRAY_CASE( int8,   Int8,   INT8,  "%hhd");
+    ARRAY_CASE( int16,  Int16,  INT16, "%" G_GINT16_FORMAT);
+    ARRAY_CASE( int32,  Int32,  INT32, "%" G_GINT32_FORMAT);
+    ARRAY_CASE( int64,  Int64,  INT64, "%" G_GINT64_FORMAT);
+    ARRAY_CASE( float,  Float,  FLOAT, "%g");
+    ARRAY_CASE(double, Double, DOUBLE, "%g");
+  default:
+    break;
+  }
+#undef ARRAY_CASE
+
+  g_print("]\n");
+}
+
+static void
+print_record_batch(GArrowRecordBatch *record_batch)
+{
+  guint nth_column, n_columns;
+
+  n_columns = garrow_record_batch_get_n_columns(record_batch);
+  for (nth_column = 0; nth_column < n_columns; nth_column++) {
+    GArrowArray *array;
+
+    g_print("columns[%u](%s): ",
+            nth_column,
+            garrow_record_batch_get_column_name(record_batch, nth_column));
+    array = garrow_record_batch_get_column(record_batch, nth_column);
+    print_array(array);
+  }
+}
+
+int
+main(int argc, char **argv)
+{
+  const char *input_path = "/tmp/batch.arrow";
+  GArrowIOMemoryMappedFile *input;
+  GError *error = NULL;
+
+  if (argc > 1)
+    input_path = argv[1];
+  input = garrow_io_memory_mapped_file_open(input_path,
+                                            GARROW_IO_FILE_MODE_READ,
+                                            &error);
+  if (!input) {
+    g_print("failed to open file: %s\n", error->message);
+    g_error_free(error);
+    return EXIT_FAILURE;
+  }
+
+  {
+    GArrowIPCFileReader *reader;
+
+    reader = garrow_ipc_file_reader_open(GARROW_IO_RANDOM_ACCESS_FILE(input),
+                                         &error);
+    if (!reader) {
+      g_print("failed to open file reader: %s\n", error->message);
+      g_error_free(error);
+      g_object_unref(input);
+      return EXIT_FAILURE;
+    }
+
+    {
+      guint i, n;
+
+      n = garrow_ipc_file_reader_get_n_record_batches(reader);
+      for (i = 0; i < n; i++) {
+        GArrowRecordBatch *record_batch;
+
+        record_batch =
+          garrow_ipc_file_reader_get_record_batch(reader, i, &error);
+        if (!record_batch) {
+          g_print("failed to open file reader: %s\n", error->message);
+          g_error_free(error);
+          g_object_unref(reader);
+          g_object_unref(input);
+          return EXIT_FAILURE;
+        }
+
+        print_record_batch(record_batch);
+        g_object_unref(record_batch);
+      }
+    }
+
+    g_object_unref(reader);
+  }
+
+  g_object_unref(input);
+
+  return EXIT_SUCCESS;
+}

http://git-wip-us.apache.org/repos/asf/arrow/blob/85b870e7/c_glib/example/read-stream.c
----------------------------------------------------------------------
diff --git a/c_glib/example/read-stream.c b/c_glib/example/read-stream.c
new file mode 100644
index 0000000..c56942c
--- /dev/null
+++ b/c_glib/example/read-stream.c
@@ -0,0 +1,143 @@
+/*
+ * 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.
+ */
+
+#include <stdlib.h>
+
+#include <arrow-glib/arrow-glib.h>
+
+static void
+print_array(GArrowArray *array)
+{
+  GArrowType value_type;
+  gint64 i, n;
+
+  value_type = garrow_array_get_value_type(array);
+
+  g_print("[");
+  n = garrow_array_get_length(array);
+
+#define ARRAY_CASE(type, Type, TYPE, format)                            \
+  case GARROW_TYPE_ ## TYPE:                                            \
+    {                                                                   \
+      GArrow ## Type ## Array *real_array;                              \
+      real_array = GARROW_ ## TYPE ## _ARRAY(array);                    \
+      for (i = 0; i < n; i++) {                                         \
+        if (i > 0) {                                                    \
+          g_print(", ");                                                \
+        }                                                               \
+        g_print(format,                                                 \
+                garrow_ ## type ## _array_get_value(real_array, i));    \
+      }                                                                 \
+    }                                                                   \
+    break
+
+  switch (value_type) {
+    ARRAY_CASE(uint8,  UInt8,  UINT8,  "%hhu");
+    ARRAY_CASE(uint16, UInt16, UINT16, "%" G_GUINT16_FORMAT);
+    ARRAY_CASE(uint32, UInt32, UINT32, "%" G_GUINT32_FORMAT);
+    ARRAY_CASE(uint64, UInt64, UINT64, "%" G_GUINT64_FORMAT);
+    ARRAY_CASE( int8,   Int8,   INT8,  "%hhd");
+    ARRAY_CASE( int16,  Int16,  INT16, "%" G_GINT16_FORMAT);
+    ARRAY_CASE( int32,  Int32,  INT32, "%" G_GINT32_FORMAT);
+    ARRAY_CASE( int64,  Int64,  INT64, "%" G_GINT64_FORMAT);
+    ARRAY_CASE( float,  Float,  FLOAT, "%g");
+    ARRAY_CASE(double, Double, DOUBLE, "%g");
+  default:
+    break;
+  }
+#undef ARRAY_CASE
+
+  g_print("]\n");
+}
+
+static void
+print_record_batch(GArrowRecordBatch *record_batch)
+{
+  guint nth_column, n_columns;
+
+  n_columns = garrow_record_batch_get_n_columns(record_batch);
+  for (nth_column = 0; nth_column < n_columns; nth_column++) {
+    GArrowArray *array;
+
+    g_print("columns[%u](%s): ",
+            nth_column,
+            garrow_record_batch_get_column_name(record_batch, nth_column));
+    array = garrow_record_batch_get_column(record_batch, nth_column);
+    print_array(array);
+  }
+}
+
+int
+main(int argc, char **argv)
+{
+  const char *input_path = "/tmp/stream.arrow";
+  GArrowIOMemoryMappedFile *input;
+  GError *error = NULL;
+
+  if (argc > 1)
+    input_path = argv[1];
+  input = garrow_io_memory_mapped_file_open(input_path,
+                                            GARROW_IO_FILE_MODE_READ,
+                                            &error);
+  if (!input) {
+    g_print("failed to open file: %s\n", error->message);
+    g_error_free(error);
+    return EXIT_FAILURE;
+  }
+
+  {
+    GArrowIPCStreamReader *reader;
+
+    reader = garrow_ipc_stream_reader_open(GARROW_IO_INPUT_STREAM(input),
+                                           &error);
+    if (!reader) {
+      g_print("failed to open stream reader: %s\n", error->message);
+      g_error_free(error);
+      g_object_unref(input);
+      return EXIT_FAILURE;
+    }
+
+    while (TRUE) {
+      GArrowRecordBatch *record_batch;
+
+      record_batch =
+        garrow_ipc_stream_reader_get_next_record_batch(reader, &error);
+      if (error) {
+        g_print("failed to get record batch: %s\n", error->message);
+        g_error_free(error);
+        g_object_unref(reader);
+        g_object_unref(input);
+        return EXIT_FAILURE;
+      }
+
+      if (!record_batch) {
+        break;
+      }
+
+      print_record_batch(record_batch);
+      g_object_unref(record_batch);
+    }
+
+    g_object_unref(reader);
+  }
+
+  g_object_unref(input);
+
+  return EXIT_SUCCESS;
+}