chx

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package chx is a thin ClickHouse client for analytics / long-term stores.

The write path uses the native clickhouse-go/v2 connection (driver.Conn), which speaks the columnar native protocol and supports PrepareBatch for large bulk inserts. Schema is applied via skit/migrate (goose, ClickHouse dialect) from a caller-supplied migrations fs.FS over a separate database/sql handle, because goose needs database/sql rather than the native conn.

ClickHouse suits analytics / long-term storage: finished, structured data in large batches, never on a hot ingest path. This client is an optional building block — importing it is what pulls in the clickhouse-go/v2 dependency; wire it in only when a ClickHouse endpoint is configured.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migrate

func Migrate(ctx context.Context, cfg Config, fsys fs.FS) error

Migrate applies the pending DDL migrations in fsys (goose SQL files, rooted at the directory that holds them) using goose's ClickHouse dialect over a dedicated database/sql handle. It is idempotent. The caller supplies its own migrations FS, e.g. fs.Sub(embedded, "migrations"). goose needs a database/sql connection (clickhouse.OpenDB), separate from the native conn.

Types

type Client

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

Client is a ClickHouse connection. It wraps the native driver.Conn; use Conn for direct access or the thin pass-throughs below.

func Open

func Open(ctx context.Context, cfg Config) (*Client, error)

Open dials ClickHouse and verifies connectivity with a ping. It does NOT apply migrations — call Migrate explicitly (usually once at startup) so schema changes stay an intentional operational step, not a side effect of connecting.

func (*Client) Close

func (c *Client) Close() error

Close releases the connection.

func (*Client) Conn

func (c *Client) Conn() driver.Conn

Conn returns the underlying native connection for direct use (e.g. PrepareBatch with column-level control, or Select into structs).

func (*Client) Exec

func (c *Client) Exec(ctx context.Context, query string, args ...any) error

Exec runs a statement with no result set (DDL/DML).

func (*Client) Name

func (c *Client) Name() string

Name identifies this store for lifecycle logging.

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping verifies the connection is alive.

func (*Client) PrepareBatch

func (c *Client) PrepareBatch(ctx context.Context, query string) (driver.Batch, error)

PrepareBatch opens a client-side batch for a bulk INSERT — the primary write path (CH loves large batches).

func (*Client) Query

func (c *Client) Query(ctx context.Context, query string, args ...any) (driver.Rows, error)

Query runs a query returning rows for manual iteration.

func (*Client) Select

func (c *Client) Select(ctx context.Context, dest any, query string, args ...any) error

Select scans a full result set into dest (a pointer to a slice of structs).

type Config

type Config struct {
	Addrs        []string
	Database     string
	Username     string
	Password     string
	DialTimeout  time.Duration
	MaxOpenConns int
	Compression  bool
}

Config describes how to reach ClickHouse. Addrs is a list of host:port native endpoints (default native port 9000). Compression enables LZ4 wire compression, worthwhile for the large batches CH ingests.

Jump to

Keyboard shortcuts

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