clickhouse

package module
v1.3.26 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

README

ClickHouse Handler Library

This library provides a utility for interacting with a ClickHouse database. It includes functionality for loading configuration, establishing a connection, executing queries, and managing the database session.

Features

  • Load ClickHouse configuration from environment variables.
  • Establish a secure connection to the ClickHouse database.
  • Execute queries and statements.
  • Manage the database session lifecycle.

Configuration

The library expects the following environment variables to be set:

  • CLICKHOUSE_HOSTNAME: The hostname of the ClickHouse server.
  • CLICKHOUSE_USERNAME: The username for authentication.
  • CLICKHOUSE_PASSWORD: The password for authentication.
  • CLICKHOUSE_DATABASE: The name of the database to connect to.
  • CLICKHOUSE_PORT: The port number of the ClickHouse server.
  • CLICKHOUSE_SKIP_VERIFY: Whether to skip TLS verification (true or false).

Usage

Loading Configuration

Use the ClickhouseLoadConfig function to load the configuration from environment variables:

config, err := ClickhouseLoadConfig()
if err != nil {
    log.Fatalf("Failed to load ClickHouse configuration: %v", err.Error())
}
Creating a ClickHouse Session

Use the NewClickhouseSession function to create a new session:

ctx := context.Background()
session, err := NewClickhouseSession(config, ctx)
if err != nil {
    log.Fatalf("Failed to create ClickHouse session: %v", err.Error())
}
defer session.Close()
Executing Queries
Querying Data

Use the Query method to execute a query and retrieve rows:

rows, err := session.Query(ctx, "SELECT * FROM my_table")
if err != nil {
    log.Fatalf("Query failed: %v", err.Error())
}
defer rows.Close()
Executing Statements

Use the Exec method to execute a statement:

err := session.Exec(ctx, "INSERT INTO my_table (id, name) VALUES (1, 'example')")
if err != nil {
    log.Fatalf("Execution failed: %v", err.Error())
}
Closing the Session

Always close the session when done:

err := session.Close()
if err != nil {
    log.Fatalf("Failed to close ClickHouse session: %v", err.Error())
}

Error Handling

Ensure proper error handling when loading configuration, creating a session, or executing queries, as invalid credentials or connection issues will result in errors.

Dependencies

This library uses the following dependencies:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClickhouseValidateConfig

func ClickhouseValidateConfig(clickhouseConfig *ClickhouseConfig) error

validateConfig validates the loaded configuration.

Types

type ClickhouseConfig

type ClickhouseConfig struct {
	ChHostname   string `mapstructure:"chhostname"`
	ChUsername   string `mapstructure:"chusername"`
	ChPassword   string `mapstructure:"chpassword"`
	ChDatabase   string `mapstructure:"chdatabase"`
	ChSkipVerify string `mapstructure:"chskipverify"`
	ChPort       string `mapstructure:"chport"`
}

func ClickhouseLoadConfig

func ClickhouseLoadConfig() (*ClickhouseConfig, error)

LoadConfig loads the configuration from environment variables using Viper.

type ClickhouseSession

type ClickhouseSession struct {
	// contains filtered or unexported fields
}

ClickhouseSession implements ClickhouseSessionInterface

func NewClickhouseSession

func NewClickhouseSession(ch *ClickhouseConfig, context context.Context) (*ClickhouseSession, error)

NewClickhouseSession creates a new Clickhouse session.

func (*ClickhouseSession) Close

func (ch *ClickhouseSession) Close() error

Close ClickHouse connection

func (*ClickhouseSession) Conn

func (ch *ClickhouseSession) Conn() driver.Conn

return ClickHouse connection

func (*ClickhouseSession) Connect

func (chsession *ClickhouseSession) Connect(ch *ClickhouseConfig, context context.Context) error

Connect to ClickHouse database

func (*ClickhouseSession) Exec

func (ch *ClickhouseSession) Exec(ctx context.Context, stmt string) error

Exec ClickHouse query

func (*ClickhouseSession) Query

func (ch *ClickhouseSession) Query(ctx context.Context, query string) (driver.Rows, error)

Query ClickHouse database

type ClickhouseSessionInterface added in v1.3.26

type ClickhouseSessionInterface interface {
	Connect(ch *ClickhouseConfig, context context.Context) error
	Query(ctx context.Context, query string) (driver.Rows, error)
	Exec(ctx context.Context, stmt string) error
	Close() error
	Conn() driver.Conn
}

ClickhouseSessionInterface defines the interface for ClickHouse session operations

type ConfigLoaderInterface added in v1.3.26

type ConfigLoaderInterface interface {
	LoadConfig() (*ClickhouseConfig, error)
}

ConfigLoaderInterface defines the interface for configuration loading

type ConfigValidatorInterface added in v1.3.26

type ConfigValidatorInterface interface {
	ValidateConfig(config *ClickhouseConfig) error
}

ConfigValidatorInterface defines the interface for configuration validation

type DefaultConfigLoader added in v1.3.26

type DefaultConfigLoader struct{}

DefaultConfigLoader implements ConfigLoaderInterface

func (*DefaultConfigLoader) LoadConfig added in v1.3.26

func (c *DefaultConfigLoader) LoadConfig() (*ClickhouseConfig, error)

LoadConfig implements ConfigLoaderInterface

type DefaultConfigValidator added in v1.3.26

type DefaultConfigValidator struct{}

DefaultConfigValidator implements ConfigValidatorInterface

func (*DefaultConfigValidator) ValidateConfig added in v1.3.26

func (v *DefaultConfigValidator) ValidateConfig(config *ClickhouseConfig) error

ValidateConfig implements ConfigValidatorInterface

type DefaultSessionFactory added in v1.3.26

type DefaultSessionFactory struct{}

DefaultSessionFactory implements SessionFactoryInterface

func (*DefaultSessionFactory) NewSession added in v1.3.26

NewSession implements SessionFactoryInterface

type SessionFactoryInterface added in v1.3.26

type SessionFactoryInterface interface {
	NewSession(ch *ClickhouseConfig, context context.Context) (ClickhouseSessionInterface, error)
}

SessionFactoryInterface defines the interface for creating ClickHouse sessions

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL