clickhouse-golisted
Install: claude install-skill omeyang/Thoth
# Go ClickHouse 专家
使用 Go clickhouse-go 开发 ClickHouse 功能:$ARGUMENTS
---
## 1. 连接管理
### 创建连接
```go
func NewClickHouse(ctx context.Context, dsn string) (driver.Conn, error)
```
- 解析 DSN,配置连接池(MaxOpenConns=10, MaxIdleConns=5)
- 设置超时(DialTimeout=5s, ReadTimeout=30s)
- 验证 Ping
### 包装器模式
```go
type ClickHouse struct { conn driver.Conn }
func New(conn driver.Conn) *ClickHouse
func (c *ClickHouse) Conn() driver.Conn
func (c *ClickHouse) Health(ctx context.Context) error
func (c *ClickHouse) Close() error
```
> 完整实现见 [references/examples.md](references/examples.md#1-连接管理)
---
## 2. 表引擎选择
| 引擎 | 适用场景 | 特点 |
|------|---------|------|
| MergeTree | 通用 OLAP、时序、日志 | 最常用,支持分区/TTL/索引 |
| ReplacingMergeTree | 有重复数据 | 按版本号去重,查询用 FINAL |
| AggregatingMergeTree | 实时统计、仪表盘 | 配合物化视图预聚合 |
| SummingMergeTree | 计数器、累加统计 | 自动求和指定列 |
> 完整 DDL 示例见 [references/examples.md](references/examples.md#2-表引擎选择)
---
## 3. 查询操作
### 基本查询
```go
func (c *ClickHouse) Query(ctx context.Context, query string, args ...any) ([]map[string]any, error)
```
### 结构体查询
```go
type Event struct {
Date time.Time `ch:"event_date"`
Time time.Time `ch:"event_time"`
UserID uint64 `ch:"user_id"`
EventType string `ch:"event_type"`
}
func (c *ClickHouse) QueryEvents(ctx context.Context, userID uint64, limit int) ([]Event, error) {
var events []Event
err := c.conn.Select(ctx, &events, `
SELECT event_date, event_time, user_id, event_type
FROM events WHERE user_i