Giter VIP home page Giter VIP logo

ch's Introduction

Minimal HTTP ClickHouse client.

tl;dr

iex> Mix.install([{:ch, github: "ruslandoga/ch"}])

# https://clickhouse.com/docs/en/quick-start#step-3-create-a-database-and-table
iex> {:ok, conn} = Ch.start_link(scheme: "http", hostname: "localhost", port: 8123)
iex> {:ok, _} = Ch.query(conn, "CREATE DATABASE IF NOT EXISTS helloworld")
iex> {:ok, _} = Ch.query(conn, """
CREATE TABLE helloworld.my_first_table
(
    user_id UInt32,
    message String,
    timestamp DateTime,
    metric Float32
)
ENGINE = MergeTree()
PRIMARY KEY (user_id, timestamp)
""")

iex> {:ok, %{num_rows: 1}} = Ch.query(conn, "INSERT INTO helloworld.my_first_table VALUES (0, {$0:String}, now(), {$1:Float32})", ["test", -1.0])
iex> {:ok, _} = Ch.query(conn, "DELETE FROM helloworld.my_first_table WHERE user_id = 0", [], settings: [allow_experimental_lightweight_delete: 1])

iex> types = [:u32, :string, :datetime, :f32]
iex> rows = [
  [101, "Hello, ClickHouse!", ~N[2023-01-06 03:50:38], -1.0],
  [102, "Insert a lot of rows per batch", ~N[2023-01-05 00:00:00], 1.41421],
  [102, "Sort your data based on your commonly-used queries", ~N[2023-01-06 00:00:00], 2.718],
  [101, "Granules are the smallest chunks of data read", ~N[2023-01-06 03:55:38], 3.14159]
]
iex> stream = Stream.map(rows, fn row -> Ch.RowBinary.encode_row(row, types) end)

iex> {:ok, %{num_rows: 4}} = Ch.query(conn, "INSERT INTO helloworld.my_first_table(user_id, message, timestamp, metric) FORMAT RowBinary", stream)

iex> {:ok, %{rows: rows}} = Ch.query(conn, "SELECT * FROM helloworld.my_first_table")
iex> rows
[
  [101, "Hello, ClickHouse!", ~N[2023-01-06 03:50:38], -1.0],
  [101, "Granules are the smallest chunks of data read", ~N[2023-01-06 03:55:38], 3.141590118408203],
  [102, "Insert a lot of rows per batch", ~N[2023-01-05 00:00:00], 1.4142099618911743],
  [102, "Sort your data based on your commonly-used queries", ~N[2023-01-06 00:00:00], 2.7179999351501465]
]

iex> {:ok, %{rows: rows}} = Ch.query(conn, "SELECT * FROM helloworld.my_first_table WHERE user_id = {user_id:Int8}", %{"user_id" => 101})
iex> rows
[
  [101, "Hello, ClickHouse!", ~N[2023-01-06 03:50:38], -1.0],
  [101, "Granules are the smallest chunks of data read", ~N[2023-01-06 03:55:38], 3.141590118408203]
]

iex> {:ok, %{rows: csv}} = Ch.query(conn, "SELECT * FROM helloworld.my_first_table", [], format: "CSV")
iex> IO.puts(csv)
101,"Hello, ClickHouse!","2023-01-06 03:50:38",-1
101,"Granules are the smallest chunks of data read","2023-01-06 03:55:38",3.14159
102,"Insert a lot of rows per batch","2023-01-05 00:00:00",1.41421
102,"Sort your data based on your commonly-used queries","2023-01-06 00:00:00",2.718

Examples

  • Queries with parameters
# https://clickhouse.com/docs/en/interfaces/http/#cli-queries-with-parameters
statement = "SELECT {a:Array(UInt8)}, {b:UInt8}, {c:String}, {d:DateTime}"
params = %{a: [1,2], b: 123, c: "123", d: ~N[2023-01-06 03:06:32]}
{:ok, %{num_rows: 1, rows: [[[1, 2], 123, "123", ~N[2023-01-06 03:06:32]]]}} = Ch.query(conn, statement, params)
  • CSV inserts
{:ok, _} = Ch.query(conn, "CREATE TABLE example(a UInt32, b String) ENGINE=Memory")
csv = """
1,a
2,b
3,c\
"""

File.write!("example.csv", csv)
{:ok, _} = Ch.query(conn, "INSERT INTO example(a, b) FORMAT CSV", File.stream!("example.csv"))
  • CSV with headers inserts
csv = """
a,b
1,a
2,b
3,c\
"""

File.write!("example.csv", csv)
{:ok, _} = Ch.query(conn, "INSERT INTO example FORMAT CSVWithNames", File.stream!("example.csv"))
iex> Ch.query(conn, "SHOW SETTINGS LIKE 'async_insert'", [], settings: [async_insert: 1])
{:ok, %{num_rows: 1, rows: [["async_insert", "Bool", "1"]]}}

iex> Ch.query(conn, "CREATE TABLE example3(a UInt8) ENGINE=Memory", [], settings: [readonly: 1])
{:error,
 %Ch.Error{
   code: 164,
   message: "Code: 164. DB::Exception: default: Cannot execute query in readonly mode. (READONLY) (version 22.10.1.1175 (official build))\n"
 }}

ch's People

Contributors

ruslandoga avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.