Skip to content

Instantly share code, notes, and snippets.

@joshnuss
Last active June 24, 2021 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshnuss/e96ae827a454fb6de9da8282f6a37f9c to your computer and use it in GitHub Desktop.
Save joshnuss/e96ae827a454fb6de9da8282f6a37f9c to your computer and use it in GitHub Desktop.
Clickhouse+Elixir driver
# only connection packets work at the moment
{:ok, socket} = :gen_tcp.connect('localhost', 9000, [:binary, nodelay: true, active: true])
#:inet.setopts(socket, [active: :once])
hello_packet = 0
client_name = "Elixir client"
client_major_version = 1
client_minor_version = 0
protocol_version = 54449
database = ""
user = "default"
password = ""
packet = <<hello_packet, String.length(client_name)>> <> client_name <> <<client_major_version::unsigned-integer, client_minor_version::unsigned-integer, <<0xb0, 0xa9, 0x03>>, String.length(database)>> <> database <> <<String.length(user)>> <> user <> <<String.length(password)>> <> password
:gen_tcp.send(socket, packet)
# Receive HELLO ack packet
receive do
{:tcp, ^socket, packet = <<^hello_packet, length, rest::binary>>} ->
<<server_name::binary-size(length), major_version, minor_version, _, _, _, length, rest::binary>> = rest
<<timezone::binary-size(length), length, rest::binary()>> = rest
<<display_name::binary-size(length), patch_number>> = rest
IO.inspect({server_name, {major_version, minor_version, patch_number}, timezone, display_name})
IO.puts "Connected."
end
:gen_tcp.close(socket)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment