]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/ruby/red-arrow/example/write-stream.rb
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / ruby / red-arrow / example / write-stream.rb
CommitLineData
1d09f67e
TL
1#!/usr/bin/env ruby
2#
3# Licensed to the Apache Software Foundation (ASF) under one
4# or more contributor license agreements. See the NOTICE file
5# distributed with this work for additional information
6# regarding copyright ownership. The ASF licenses this file
7# to you under the Apache License, Version 2.0 (the
8# "License"); you may not use this file except in compliance
9# with the License. You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing,
14# software distributed under the License is distributed on an
15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16# KIND, either express or implied. See the License for the
17# specific language governing permissions and limitations
18# under the License.
19
20require "arrow"
21
22fields = [
23 Arrow::Field.new("uint8", :uint8),
24 Arrow::Field.new("uint16", :uint16),
25 Arrow::Field.new("uint32", :uint32),
26 Arrow::Field.new("uint64", :uint64),
27 Arrow::Field.new("int8", :int8),
28 Arrow::Field.new("int16", :int16),
29 Arrow::Field.new("int32", :int32),
30 Arrow::Field.new("int64", :int64),
31 Arrow::Field.new("float", :float),
32 Arrow::Field.new("double", :double),
33]
34schema = Arrow::Schema.new(fields)
35
36Arrow::FileOutputStream.open("/tmp/stream.arrow", false) do |output|
37 Arrow::RecordBatchStreamWriter.open(output, schema) do |writer|
38 uints = [1, 2, 4, 8]
39 ints = [1, -2, 4, -8]
40 floats = [1.1, -2.2, 4.4, -8.8]
41 columns = [
42 Arrow::UInt8Array.new(uints),
43 Arrow::UInt16Array.new(uints),
44 Arrow::UInt32Array.new(uints),
45 Arrow::UInt64Array.new(uints),
46 Arrow::Int8Array.new(ints),
47 Arrow::Int16Array.new(ints),
48 Arrow::Int32Array.new(ints),
49 Arrow::Int64Array.new(ints),
50 Arrow::FloatArray.new(floats),
51 Arrow::DoubleArray.new(floats),
52 ]
53
54 record_batch = Arrow::RecordBatch.new(schema, 4, columns)
55 writer.write_record_batch(record_batch)
56
57 sliced_columns = columns.collect do |column|
58 column.slice(1, 3)
59 end
60 record_batch = Arrow::RecordBatch.new(schema, 3, sliced_columns)
61 writer.write_record_batch(record_batch)
62 end
63end