clickhouse

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: Apache-2.0 Imports: 13 Imported by: 1

README

framework-clickhouse

Go framework package for clickhouse.

Installation

go get github.com/go-anyway/framework-clickhouse@v1.0.0

Usage

See documentation for usage examples.

License

Apache License 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClickHouseClient

type ClickHouseClient struct {
	EnableTrace bool // 是否启用追踪
	// contains filtered or unexported fields
}

ClickHouseClient ClickHouse 客户端接口,包含连接

func NewClickHouse

func NewClickHouse(opts *Options) (*ClickHouseClient, error)

NewClickHouse 根据给定的选项创建一个新的 ClickHouse 客户端实例

func (*ClickHouseClient) Close

func (c *ClickHouseClient) Close() error

Close 关闭 ClickHouse 客户端连接

func (*ClickHouseClient) Exec

func (c *ClickHouseClient) Exec(ctx context.Context, query string, args ...interface{}) error

Exec 执行 SQL 语句(不返回结果集,自动处理追踪)

func (*ClickHouseClient) GetConn

func (c *ClickHouseClient) GetConn() driver.Conn

GetConn 获取原生连接(用于高级操作)

func (*ClickHouseClient) GetDB

func (c *ClickHouseClient) GetDB() *sql.DB

GetDB 获取 sql.DB 连接(用于兼容 database/sql 接口)

func (*ClickHouseClient) Insert

func (c *ClickHouseClient) Insert(ctx context.Context, table string, columns []string, rows [][]interface{}) error

Insert 插入数据(自动处理追踪)

func (*ClickHouseClient) Ping

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

Ping 测试连接

func (*ClickHouseClient) Query

func (c *ClickHouseClient) Query(ctx context.Context, query string, args ...interface{}) (driver.Rows, error)

Query 执行查询并返回结果(自动处理追踪)

func (*ClickHouseClient) QueryRow

func (c *ClickHouseClient) QueryRow(ctx context.Context, query string, args ...interface{}) driver.Row

QueryRow 执行查询并返回单行结果

type Config

type Config struct {
	Enabled         bool               `yaml:"enabled" env:"CLICKHOUSE_ENABLED" default:"true"`
	Host            string             `yaml:"host" env:"CLICKHOUSE_HOST" default:"localhost"`
	Port            int                `yaml:"port" env:"CLICKHOUSE_PORT" default:"9000"`
	Database        string             `yaml:"database" env:"CLICKHOUSE_DATABASE" required:"true"`
	Username        string             `yaml:"username" env:"CLICKHOUSE_USERNAME" required:"true"`
	Password        string             `yaml:"password" env:"CLICKHOUSE_PASSWORD" required:"true"`
	EnableTLS       bool               `yaml:"enable_tls" env:"CLICKHOUSE_ENABLE_TLS" default:"false"`
	DialTimeout     pkgConfig.Duration `yaml:"dial_timeout" env:"CLICKHOUSE_DIAL_TIMEOUT" default:"30s"`
	ReadTimeout     pkgConfig.Duration `yaml:"read_timeout" env:"CLICKHOUSE_READ_TIMEOUT" default:"30s"`
	WriteTimeout    pkgConfig.Duration `yaml:"write_timeout" env:"CLICKHOUSE_WRITE_TIMEOUT" default:"30s"`
	MaxOpenConns    int                `yaml:"max_open_conns" env:"CLICKHOUSE_MAX_OPEN_CONNS" default:"100"`
	MaxIdleConns    int                `yaml:"max_idle_conns" env:"CLICKHOUSE_MAX_IDLE_CONNS" default:"10"`
	ConnMaxLifetime pkgConfig.Duration `yaml:"conn_max_lifetime" env:"CLICKHOUSE_CONN_MAX_LIFETIME" default:"1h"`
	EnableTrace     bool               `yaml:"enable_trace" env:"CLICKHOUSE_ENABLE_TRACE" default:"true"`
}

Config ClickHouse 配置结构体(用于从配置文件创建)

func (*Config) ConnMaxLifetimeDuration

func (c *Config) ConnMaxLifetimeDuration() time.Duration

ConnMaxLifetimeDuration 返回 time.Duration 类型的 ConnMaxLifetime

func (*Config) DialTimeoutDuration

func (c *Config) DialTimeoutDuration() time.Duration

DialTimeoutDuration 返回 time.Duration 类型的 DialTimeout

func (*Config) ReadTimeoutDuration

func (c *Config) ReadTimeoutDuration() time.Duration

ReadTimeoutDuration 返回 time.Duration 类型的 ReadTimeout

func (*Config) ToOptions

func (c *Config) ToOptions() (*Options, error)

ToOptions 转换为 Options

func (*Config) Validate

func (c *Config) Validate() error

Validate 验证 ClickHouse 配置

func (*Config) WriteTimeoutDuration

func (c *Config) WriteTimeoutDuration() time.Duration

WriteTimeoutDuration 返回 time.Duration 类型的 WriteTimeout

type Options

type Options struct {
	Host            string        // ClickHouse 主机地址
	Port            int           // ClickHouse 端口
	Database        string        // 数据库名
	Username        string        // 用户名
	Password        string        // 密码
	DialTimeout     time.Duration // 连接超时
	ReadTimeout     time.Duration // 读取超时
	WriteTimeout    time.Duration // 写入超时
	MaxOpenConns    int           // 最大打开连接数
	MaxIdleConns    int           // 最大空闲连接数
	ConnMaxLifetime time.Duration // 连接最大生存时间
	EnableTLS       bool          // 是否启用 TLS
	EnableTrace     bool          // 是否启用查询追踪,用于记录查询执行时间
}

Options 结构体定义了 ClickHouse 连接器的配置选项(内部使用)

Jump to

Keyboard shortcuts

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