Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple Ruby client example #19

Merged
merged 12 commits into from
Mar 14, 2024
1 change: 1 addition & 0 deletions http/get_simple/ruby/client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Gemfile.lock
amoeba marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 20 additions & 0 deletions http/get_simple/ruby/client/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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.

source "https://rubygems.org"

gem "red-arrow"
37 changes: 37 additions & 0 deletions http/get_simple/ruby/client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!---
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.
-->

# HTTP GET Arrow Data: Simple Ruby Client Example

This directory contains a minimal example of an HTTP client implemented in Ruby.

The client:

1. Sends an HTTP GET request to a server.
2. Receives an HTTP 200 response from the server, converts the response to a RecordBatchReader, and consumes it.
3. Sends a second HTTP GET request to a server.
with the response body containing an Arrow IPC stream of record batches.
4. Streams the response, creating and printing record batches as it goes.
amoeba marked this conversation as resolved.
Show resolved Hide resolved

To run this example, first start one of the server examples in the parent directory, then:

```sh
bundle install
bundle exec ruby client.rb
```
61 changes: 61 additions & 0 deletions http/get_simple/ruby/client/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# 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.

require "open-uri"
require "arrow"

host = "localhost"
port = 8008
url = URI("http://#{host}:#{port}")

# Non-streaming
url.open do |input|
input = Arrow::BufferInputStream.new(input.read)
reader = Arrow::RecordBatchStreamReader.new(input)
p reader.schema
p reader.read_all
end
amoeba marked this conversation as resolved.
Show resolved Hide resolved

# Streaming

nrows = 0
batches = []

Net::HTTP.start(host, port) do |http|
req = Net::HTTP::Get.new(url)

http.request(req) do |res|
StringIO.open(res.read_body) do |stringio_input|
Gio::RubyInputStream.open(stringio_input) do |gio_input|
Arrow::GIOInputStream.open(gio_input) do |arrow_input|
reader = Arrow::RecordBatchStreamReader.new(arrow_input)

p reader.schema

reader.each do |batch|
puts "Got batch of #{batch.length} rows"

nrows += batch.length
ianmcook marked this conversation as resolved.
Show resolved Hide resolved
batches << batch
end
end
end
end
end
end

puts "Streamed a total of #{batches.length} batches and #{nrows} total rows"