Class: ClickHouse::Client::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/click_house/client/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



68
69
70
71
72
73
74
75
# File 'lib/click_house/client/configuration.rb', line 68

def initialize
  @databases = {}
  @typecasters = {}
  @http_post_proc = nil
  @json_parser = JSON
  @logger = ::Logger.new($stdout)
  @log_proc = ->(query) { query.to_sql }
end

Instance Attribute Details

#databasesObject (readonly)

Returns the value of attribute databases.



66
67
68
# File 'lib/click_house/client/configuration.rb', line 66

def databases
  @databases
end

#http_post_procObject

Configuration options:

register_database (method): registers a database, the following arguments are required:

- database: database name
- url: URL and port to the HTTP interface
- username
- password
- variables (optional): configuration for the client

http_post_proc: A callable object for invoking the HTTP request. The object must handle the following parameters: url, headers, body and return a ClickHouse::Client::Response object.

json_parser: object for parsing JSON strings, it should respond to the "parse" method

logger: object for receiving logger commands. Default $stdout log_proc: any output (e.g. structure) to wrap around the query for every statement

register_typecaster (method): registers a custom typecaster for a column type, taking precedence over the built-in typecasters. Arguments:

- type: column type as a String (exact match) or a Regexp
- typecaster: a callable object receiving the column metadata Hash
(e.g. { 'name' => 'title', 'type' => 'String' }) and the raw value

Example:

ClickHouse::Client.configure do |c| c.register_database(:main, database: 'gitlab_clickhouse_test', url: 'http://localhost:8123', username: 'default', password: 'clickhouse', variables: { join_use_nulls: 1 # treat JOINs as per SQL standard } )

c.logger = MyLogger.new
c.log_proc = ->(query) do
{ query_body: query.to_redacted_sql }
end

c.http_post_proc = lambda do |url, headers, body|
options = {
  headers: headers,
  body: body,
  allow_local_requests: false
}

response = Gitlab::HTTP.post(url, options)
ClickHouse::Client::Response.new(response.body, response.code)
end

c.json_parser = JSON

c.register_typecaster('String', ->(meta, value) do
meta['name'].match?(/\w+_json\z/) ? JSON.parse(value) : value
end)

end



65
66
67
# File 'lib/click_house/client/configuration.rb', line 65

def http_post_proc
  @http_post_proc
end

#json_parserObject

Configuration options:

register_database (method): registers a database, the following arguments are required:

- database: database name
- url: URL and port to the HTTP interface
- username
- password
- variables (optional): configuration for the client

http_post_proc: A callable object for invoking the HTTP request. The object must handle the following parameters: url, headers, body and return a ClickHouse::Client::Response object.

json_parser: object for parsing JSON strings, it should respond to the "parse" method

logger: object for receiving logger commands. Default $stdout log_proc: any output (e.g. structure) to wrap around the query for every statement

register_typecaster (method): registers a custom typecaster for a column type, taking precedence over the built-in typecasters. Arguments:

- type: column type as a String (exact match) or a Regexp
- typecaster: a callable object receiving the column metadata Hash
(e.g. { 'name' => 'title', 'type' => 'String' }) and the raw value

Example:

ClickHouse::Client.configure do |c| c.register_database(:main, database: 'gitlab_clickhouse_test', url: 'http://localhost:8123', username: 'default', password: 'clickhouse', variables: { join_use_nulls: 1 # treat JOINs as per SQL standard } )

c.logger = MyLogger.new
c.log_proc = ->(query) do
{ query_body: query.to_redacted_sql }
end

c.http_post_proc = lambda do |url, headers, body|
options = {
  headers: headers,
  body: body,
  allow_local_requests: false
}

response = Gitlab::HTTP.post(url, options)
ClickHouse::Client::Response.new(response.body, response.code)
end

c.json_parser = JSON

c.register_typecaster('String', ->(meta, value) do
meta['name'].match?(/\w+_json\z/) ? JSON.parse(value) : value
end)

end



65
66
67
# File 'lib/click_house/client/configuration.rb', line 65

def json_parser
  @json_parser
end

#log_procObject

Configuration options:

register_database (method): registers a database, the following arguments are required:

- database: database name
- url: URL and port to the HTTP interface
- username
- password
- variables (optional): configuration for the client

http_post_proc: A callable object for invoking the HTTP request. The object must handle the following parameters: url, headers, body and return a ClickHouse::Client::Response object.

json_parser: object for parsing JSON strings, it should respond to the "parse" method

logger: object for receiving logger commands. Default $stdout log_proc: any output (e.g. structure) to wrap around the query for every statement

register_typecaster (method): registers a custom typecaster for a column type, taking precedence over the built-in typecasters. Arguments:

- type: column type as a String (exact match) or a Regexp
- typecaster: a callable object receiving the column metadata Hash
(e.g. { 'name' => 'title', 'type' => 'String' }) and the raw value

Example:

ClickHouse::Client.configure do |c| c.register_database(:main, database: 'gitlab_clickhouse_test', url: 'http://localhost:8123', username: 'default', password: 'clickhouse', variables: { join_use_nulls: 1 # treat JOINs as per SQL standard } )

c.logger = MyLogger.new
c.log_proc = ->(query) do
{ query_body: query.to_redacted_sql }
end

c.http_post_proc = lambda do |url, headers, body|
options = {
  headers: headers,
  body: body,
  allow_local_requests: false
}

response = Gitlab::HTTP.post(url, options)
ClickHouse::Client::Response.new(response.body, response.code)
end

c.json_parser = JSON

c.register_typecaster('String', ->(meta, value) do
meta['name'].match?(/\w+_json\z/) ? JSON.parse(value) : value
end)

end



65
66
67
# File 'lib/click_house/client/configuration.rb', line 65

def log_proc
  @log_proc
end

#loggerObject

Configuration options:

register_database (method): registers a database, the following arguments are required:

- database: database name
- url: URL and port to the HTTP interface
- username
- password
- variables (optional): configuration for the client

http_post_proc: A callable object for invoking the HTTP request. The object must handle the following parameters: url, headers, body and return a ClickHouse::Client::Response object.

json_parser: object for parsing JSON strings, it should respond to the "parse" method

logger: object for receiving logger commands. Default $stdout log_proc: any output (e.g. structure) to wrap around the query for every statement

register_typecaster (method): registers a custom typecaster for a column type, taking precedence over the built-in typecasters. Arguments:

- type: column type as a String (exact match) or a Regexp
- typecaster: a callable object receiving the column metadata Hash
(e.g. { 'name' => 'title', 'type' => 'String' }) and the raw value

Example:

ClickHouse::Client.configure do |c| c.register_database(:main, database: 'gitlab_clickhouse_test', url: 'http://localhost:8123', username: 'default', password: 'clickhouse', variables: { join_use_nulls: 1 # treat JOINs as per SQL standard } )

c.logger = MyLogger.new
c.log_proc = ->(query) do
{ query_body: query.to_redacted_sql }
end

c.http_post_proc = lambda do |url, headers, body|
options = {
  headers: headers,
  body: body,
  allow_local_requests: false
}

response = Gitlab::HTTP.post(url, options)
ClickHouse::Client::Response.new(response.body, response.code)
end

c.json_parser = JSON

c.register_typecaster('String', ->(meta, value) do
meta['name'].match?(/\w+_json\z/) ? JSON.parse(value) : value
end)

end



65
66
67
# File 'lib/click_house/client/configuration.rb', line 65

def logger
  @logger
end

#typecastersObject (readonly)

Returns the value of attribute typecasters.



66
67
68
# File 'lib/click_house/client/configuration.rb', line 66

def typecasters
  @typecasters
end

Instance Method Details

#register_database(name, **args) ⇒ Object

Raises:



77
78
79
80
81
# File 'lib/click_house/client/configuration.rb', line 77

def register_database(name, **args)
  raise ConfigurationError, "The database '#{name}' is already registered" if @databases.key?(name)

  @databases[name] = Database.new(**args)
end

#register_typecaster(type, typecaster) ⇒ Object

Raises:



83
84
85
86
87
# File 'lib/click_house/client/configuration.rb', line 83

def register_typecaster(type, typecaster)
  raise ConfigurationError, "The typecaster for '#{type}' is already registered" if @typecasters.key?(type)

  @typecasters[type] = typecaster
end

#validate!Object

Raises:



89
90
91
92
# File 'lib/click_house/client/configuration.rb', line 89

def validate!
  raise ConfigurationError, "The 'http_post_proc' option is not configured" unless @http_post_proc
  raise ConfigurationError, "The 'json_parser' option is not configured" unless @json_parser
end