Modern Ruby database driver for ClickHouse
gem install click_houseA 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
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.
…Alternative, you can assign configuration parameters via a hash
ClickHouse.config.assign(logger: Logger.new(STDOUT))Now you are able to communicate with ClickHouse:
ClickHouse.connection.ping #=> trueYou can easily build a new raw connection and override any configuration parameter (such as database name, connection address)
@connection = ClickHouse::Connection.new(ClickHouse::Config.new(logger: Rails.logger))
@connection.ping…Select all type-casted result set
…Select value returns exactly one type-casted value
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)") #=> nilReturns a record hash with the column names as keys and column values as values.
ClickHouse.connection.select_one('SELECT date, SUM(id) AS sum FROM visits GROUP BY date')
#=> {"date"=>#, "sum"=>1}By default, gem provides parser for JSON and CSV response formats. Type conversion
available for the JSON.
# 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"When column names and values are transferred separately, data sends to the server
using JSONCompactEachRow format by default.
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
CREATE TABLE assets(visible Boolean, tags Array(Nullable(String))) ENGINE Memory…`
You can clear the data table before each test with RSpec
RSpec.configure do |config|
config.before(:each, truncate_click_house: true) do
ClickHouse.connection.truncate_tables
end
endRSpec.describe Api::MetricsCountroller, truncate_click_house: true do
it { }
it { }
endmake dockerize
rspec
rubocopNo open issues yet, or sync has not completed.