Ruby¶
Red ADBC is the Ruby binding for ADBC, built on top of ADBC GLib. It is distributed as the red-adbc gem on RubyGems.
Installation¶
red-adbc depends on the native Arrow GLib and ADBC GLib libraries. The
rubygems-requirements-system plugin installs
these system dependencies automatically using your system package manager, so
install it first.
With gem:
gem install rubygems-requirements-system
gem install red-adbc
Or, with Bundler, add the plugin and the gem to your Gemfile:
source "https://rubygems.org"
plugin "rubygems-requirements-system"
gem "red-adbc"
Installing the GLib libraries manually
The rubygems-requirements-system plugin installs the native Arrow GLib
and ADBC GLib libraries automatically. If it can’t (for example, on an
unsupported package manager), install them yourself before installing
red-adbc:
macOS with Homebrew:
brew install apache-arrow-glib apache-arrow-adbc-glib
Debian/Ubuntu:
sudo apt install libarrow-glib-dev libadbc-glib-dev
RHEL-compatible distributions:
sudo dnf install arrow-glib-devel adbc-glib-devel
Windows with RubyInstaller/MSYS2 UCRT64:
pacman -S --needed mingw-w64-ucrt-x86_64-arrow mingw-w64-ucrt-x86_64-arrow-adbc-glib
Installing Drivers¶
You also need a driver for the database you want to connect to. See Installing Drivers for instructions. For the example below, you could install dbc and then install the PostgreSQL driver with:
dbc install postgresql
Basic Example¶
This example demonstrates connecting to PostgreSQL, executing a query, and reading results.
require "adbc"
database = ADBC::Database.new
begin
database.set_option("driver", "postgresql")
database.set_option("uri", "postgresql://user:password@localhost:5432/database")
database.set_load_flags(ADBC::LoadFlags::DEFAULT)
database.init
database.connect do |connection|
table, = connection.query("SELECT * FROM my_table;")
puts table
end
ensure
database.release
end
Working with Results¶
ADBC returns results as Arrow tables, which you can process using the Arrow Ruby library:
require "adbc"
database = ADBC::Database.new
begin
database.set_option("driver", "postgresql")
database.set_option("uri", "postgresql://user:password@localhost:5432/database")
database.set_load_flags(ADBC::LoadFlags::DEFAULT)
database.init
database.connect do |connection|
table, = connection.query("SELECT * FROM my_table;")
# Access columns
puts "Columns: #{table.schema.fields.map(&:name)}"
# Iterate over rows
table.each_record_batch do |batch|
batch.each do |row|
puts row
end
end
end
ensure
database.release
end
Next Steps¶
See the drivers to see which databases are supported
Explore more examples in the adbc-quickstarts repository
Explore the Ruby source code for additional examples