Clickhouse
一个使用 https://github.com/ClickHouse/clickhouse-go 的 Clickhouse 存储驱动程序。
目录
签名
func New(config ...Config) (*Storage, error)
func (s *Storage) Get(key string) ([]byte, error)
func (s *Storage) Set(key string, val []byte, exp time.Duration) error
func (s *Storage) Delete(key string) error
func (s *Storage) Reset() error
func (s *Storage) Close() error
func (s *Storage) Conn() *Session
安装
Clickhouse 支持最新两个版本的 Go
安装 clickhouse 实现
go get github.com/gofiber/storage/clickhouse
运行测试
此模块使用 Testcontainers for Go 运行集成测试,它将在后台启动一个 Clickhouse 本地实例作为 Docker 容器。要运行测试,您的机器上必须安装 Docker(或与 Docker API 100% 兼容的其他容器运行时)。
本地开发
在运行此实现之前,您必须确保 Clickhouse 集群可用。对于本地开发,我们建议使用 Clickhouse Docker 镜像;它包含了客户端正确运行所需的一切。
要使用 Docker 启动 Clickhouse,请执行以下命令
docker run -d -p 9000:9000 --name some-clickhouse-server --ulimit nofile=262144:262144 clickhouse/clickhouse-server
运行此命令后,您就可以开始使用此存储驱动程序并连接到数据库了。
示例
您可以使用以下选项创建 clickhouse 存储驱动程序
import "github.com/gofiber/storage/clickhouse"
// Initialize default config, to connect to localhost:9000 using the memory engine and with a clean table.
store, err := clickhouse.New(clickhouse.Config{
Host: "localhost",
Port: 9000,
Clean: true,
})
// Initialize custom config to connect to a different host/port and use custom engine and with clean table.
store, err := clickhouse.New(clickhouse.Config{
Host: "some-ip-address",
Port: 9000,
Engine: clickhouse.MergeTree,
Clean: true,
})
// Initialize to connect with TLS enabled with your own tls.Config and with clean table.
tlsConfig := config := &tls.Config{...}
store, err := clickhouse.New(clickhouse.Config{
Host: "some-ip-address",
Port: 9000,
Clean: true,
TLSConfig: tlsConfig,
})
配置
// Config defines configuration options for Clickhouse connection.
type Config struct {
// The host of the database. Ex: 127.0.0.1
Host string
// The port where the database is supposed to listen to. Ex: 9000
Port int
// The database that the connection should authenticate from
Database string
// The username to be used in the authentication
Username string
// The password to be used in the authentication
Password string
// The name of the table that will store the data
Table string
// The engine that should be used in the table
Engine string
// Should start a clean table, default false
Clean bool
// TLS configuration, default nil
TLSConfig *tls.Config
// Should the connection be in debug mode, default false
Debug bool
// The function to use with the debug config, default print function. It only works when debug is true
Debugf func(format string, v ...any)
}
默认配置
var DefaultConfig = Config{
Host: "localhost",
Port: 9000,
Engine: "Memory",
Clean: false,
}