百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

click_house

> 数据库
开源

适用于 ClickHouse 的现代 Ruby 数据库驱动程序

193 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

适用于 ClickHouse 的现代 Ruby 数据库驱动程序

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 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyclickhousegemruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类数据库
定价开源

> 相关工具

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