Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

click_house

> 数据库
Open source

Modern Ruby database driver for ClickHouse

193 stars0 likes1 views
WebsiteGitHub

About

Modern Ruby database driver for ClickHouse

ClickHouse Ruby driver

bash
gem install click_house

A modern Ruby database driver for ClickHouse. ClickHouse is a high-performance column-oriented database management system developed by Yandex which operates Russia's most popular search engine.

This development was inspired by currently unmaintainable alternative but rewritten and well tested

Why use the HTTP interface and not the TCP interface?

Well, the developers of ClickHouse themselves discourage using the TCP interface.

TCP transport is more specific, we don't want to expose details. Despite we have full compatibility of protocol of different versions of client and server, we want to keep the ability to "break" it for very old clients. And that protocol is not too clean to make a specification.

Yandex uses HTTP interface for working from Java and Perl, Python and Go as well as shell scripts.

TOC

  • Configuration
  • Usage
  • Queries
  • Insert
  • Create a table
  • Alter table
  • Type casting
  • Using with a connection pool
  • Using with Rails
  • Using with ActiveRecord
  • Using with RSpec
  • Development

Configuration

…

Alternative, you can assign configuration parameters via a hash

ruby
ClickHouse.config.assign(logger: Logger.new(STDOUT))

Now you are able to communicate with ClickHouse:

ruby
ClickHouse.connection.ping #=> true

You can easily build a new raw connection and override any configuration parameter (such as database name, connection address)

ruby
@connection = ClickHouse::Connection.new(ClickHouse::Config.new(logger: Rails.logger))
@connection.ping

Usage

…

Queries

Select All

Select all type-casted result set

…

Select Value

Select value returns exactly one type-casted value

ruby
ClickHouse.connection.select_value('SELECT COUNT(*) from visits') #=> 0
ClickHouse.connection.select_value("SELECT toDate('2019-01-01')") #=> #
ClickHouse.connection.select_value("SELECT toDateOrZero(NULL)") #=> nil

Select One

Returns a record hash with the column names as keys and column values as values.

ruby
ClickHouse.connection.select_one('SELECT date, SUM(id) AS sum FROM visits GROUP BY date')
#=> {"date"=>#, "sum"=>1}

Execute Raw SQL

By default, gem provides parser for JSON and CSV response formats. Type conversion available for the JSON.

ruby
# format not specified
response = ClickHouse.connection.execute  "2\n"

# JSON
response = ClickHouse.connection.execute  {"meta"=>[{"name"=>"counter", "type"=>"UInt64"}], "data"=>[{"counter"=>"2"}], "rows"=>1, "statistics"=>{"elapsed"=>0.0002412, "rows_read"=>2, "bytes_read"=>4}}

# CSV
response = ClickHouse.connection.execute  [["2"]]

# You may use any format supported by ClickHouse
response = ClickHouse.connection.execute  "\u0002\u0000\u0000\u0000\u0000\u0000\u0000\u0000"

Insert

When column names and values are transferred separately, data sends to the server using JSONCompactEachRow format by default.

ruby
ClickHouse.connection.insert('table', columns: %i[id name]) do |buffer|
  buffer  {}
ClickHouse.types.default #=> #

Type casting works automatically when fetching data, when inserting data, you must serialize the types yourself

sql
CREATE TABLE assets(visible Boolean, tags Array(Nullable(String))) ENGINE Memory
…

`

Using with RSpec

You can clear the data table before each test with RSpec

ruby
RSpec.configure do |config|
  config.before(:each, truncate_click_house: true) do
    ClickHouse.connection.truncate_tables
  end
end
ruby
RSpec.describe Api::MetricsCountroller, truncate_click_house: true do
  it { }
  it { }
end

Development

bash
make dockerize
rspec
rubocop

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyclickhousegemruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库