# clickhouse **Repository Path**: d_zt/clickhouse ## Basic Information - **Project Name**: clickhouse - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-04-24 - **Last Updated**: 2022-04-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ClickHouse [![run-tests](https://github.com/ClickHouse/clickhouse-go/actions/workflows/run-tests.yml/badge.svg?branch=v2)](https://github.com/ClickHouse/clickhouse-go/actions/workflows/run-tests.yml) [![Go Reference](https://pkg.go.dev/badge/github.com/ClickHouse/clickhouse-go/v2.svg)](https://pkg.go.dev/github.com/ClickHouse/clickhouse-go/v2) Golang SQL database driver for [Yandex ClickHouse](https://clickhouse.yandex/). Supported by [Kinescope](https://kinescope.io). ## Key features * Uses native ClickHouse TCP client-server protocol * Compatibility with [`database/sql`](#std-databasesql-interface) ([slower](#benchmark) than [native interface](#native-interface)!) * Marshal rows into structs ([ScanStruct](tests/scan_struct_test.go), [Select](examples/native/scan_struct/main.go)) * Unmarshal struct to row ([AppendStruct](benchmark/v2/write-native-struct/main.go)) * Connection pool * Failover and load balancing * [Bulk write support](examples/native/batch/main.go) (for `database/sql` [use](examples/std/batch/main.go) `begin->prepare->(in loop exec)->commit`) * [AsyncInsert](benchmark/v2/write-async/main.go) * Named and numeric placeholders support * LZ4 compression support * External data Support for the ClickHouse protocol advanced features using `Context`: * Query ID * Quota Key * Settings * OpenTelemetry * Execution events: * Logs * Progress * Profile info * Profile events # `database/sql` interface ## OpenDB ```go conn := clickhouse.OpenDB(&clickhouse.Options{ Addr: []string{"127.0.0.1:9999"}, Auth: clickhouse.Auth{ Database: "default", Username: "default", Password: "", }, TLS: &tls.Config{ InsecureSkipVerify: true, }, Settings: clickhouse.Settings{ "max_execution_time": 60, }, DialTimeout: 5 * time.Second, Compression: &clickhouse.Compression{ clickhouse.CompressionLZ4, }, Debug: true, }) conn.SetMaxIdleConns(5) conn.SetMaxOpenConns(10) conn.SetConnMaxLifetime(time.Hour) ``` ## DSN * hosts - comma-separated list of single address hosts for load-balancing and failover * username/password - auth credentials * database - select the current default database * dial_timeout - a duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix such as "300ms", "1s". Valid time units are "ms", "s", "m". * connection_open_strategy - random/in_order (default random). * round-robin - choose a round-robin server from the set * in_order - first live server is chosen in specified order * debug - enable debug output (boolean value) * compress - enable lz4 compression (boolean value) SSL/TLS parameters: * secure - establish secure connection (default is false) * skip_verify - skip certificate verification (default is false) Example: ```sh clickhouse://username:password@host1:9000,host2:9000/database?dial_timeout=200ms&max_execution_time=60 ``` ## Benchmark | [V1 (READ)](benchmark/v1/read/main.go) | [V2 (READ) std](benchmark/v2/read/main.go) | [V2 (READ) native](benchmark/v2/read-native/main.go) | | -------------------------------------- | ------------------------------------------ | ---------------------------------------------------- | | 1.218s | 924.390ms | 675.721ms | | [V1 (WRITE)](benchmark/v1/write/main.go) | [V2 (WRITE) std](benchmark/v2/write/main.go) | [V2 (WRITE) native](benchmark/v2/write-native/main.go) | [V2 (WRITE) by column](benchmark/v2/write-native-columnar/main.go) | | ---------------------------------------- | -------------------------------------------- | ------------------------------------------------------ | ------------------------------------------------------------------ | | 1.899s | 1.177s | 699.203ms | 661.973ms | ## Install ```sh go get -u github.com/ClickHouse/clickhouse-go/v2 ``` ## Examples ### native interface ```go package main import ( "context" "fmt" "log" "time" "github.com/ClickHouse/clickhouse-go/v2" ) func example() error { conn, err := clickhouse.Open(&clickhouse.Options{ Addr: []string{"127.0.0.1:9000"}, Auth: clickhouse.Auth{ Database: "default", Username: "default", Password: "", }, //Debug: true, DialTimeout: time.Second, MaxOpenConns: 10, MaxIdleConns: 5, ConnMaxLifetime: time.Hour, Compression: &clickhouse.Compression{ Method: clickhouse.CompressionLZ4, }, Settings: clickhouse.Settings{ "max_execution_time": 60, }, }) if err != nil { return err } ctx := clickhouse.Context(context.Background(), clickhouse.WithSettings(clickhouse.Settings{ "max_block_size": 10, }), clickhouse.WithProgress(func(p *clickhouse.Progress) { fmt.Println("progress: ", p) })) if err := conn.Ping(ctx); err != nil { if exception, ok := err.(*clickhouse.Exception); ok { fmt.Printf("Catch exception [%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace) } return err } if err := conn.Exec(ctx, `DROP TABLE IF EXISTS example`); err != nil { return err } err = conn.Exec(ctx, ` CREATE TABLE IF NOT EXISTS example ( Col1 UInt8, Col2 String, Col3 DateTime ) engine=Memory `) if err != nil { return err } batch, err := conn.PrepareBatch(ctx, "INSERT INTO example (Col1, Col2, Col3)") if err != nil { return err } for i := 0; i < 10; i++ { if err := batch.Append(uint8(i), fmt.Sprintf("value_%d", i), time.Now()); err != nil { return err } } if err := batch.Send(); err != nil { return err } rows, err := conn.Query(ctx, "SELECT Col1, Col2, Col3 FROM example WHERE Col1 >= $1 AND Col2 <> $2 AND Col3 <= $3", 0, "xxx", time.Now()) if err != nil { return err } for rows.Next() { var ( col1 uint8 col2 string col3 time.Time ) if err := rows.Scan(&col1, &col2, &col3); err != nil { return err } fmt.Printf("row: col1=%d, col2=%s, col3=%s\n", col1, col2, col3) } rows.Close() return rows.Err() } func main() { if err := example(); err != nil { log.Fatal(err) } } ``` ### std `database/sql` interface ```go package main import ( "context" "database/sql" "fmt" "log" "time" "github.com/ClickHouse/clickhouse-go/v2" ) func example() error { conn, err := sql.Open("clickhouse", "clickhouse://127.0.0.1:9000?dial_timeout=1s&compress=true&max_execution_time=60") if err != nil { return err } conn.SetMaxIdleConns(5) conn.SetMaxOpenConns(10) conn.SetConnMaxLifetime(time.Hour) ctx := clickhouse.Context(context.Background(), clickhouse.WithSettings(clickhouse.Settings{ "max_block_size": 10, }), clickhouse.WithProgress(func(p *clickhouse.Progress) { fmt.Println("progress: ", p) })) if err := conn.PingContext(ctx); err != nil { if exception, ok := err.(*clickhouse.Exception); ok { fmt.Printf("Catch exception [%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace) } return err } if _, err := conn.ExecContext(ctx, `DROP TABLE IF EXISTS example`); err != nil { return err } _, err = conn.ExecContext(ctx, ` CREATE TABLE IF NOT EXISTS example ( Col1 UInt8, Col2 String, Col3 DateTime ) engine=Memory `) if err != nil { return err } scope, err := conn.Begin() if err != nil { return err } { batch, err := scope.PrepareContext(ctx, "INSERT INTO example (Col1, Col2, Col3)") if err != nil { return err } for i := 0; i < 10; i++ { if _, err := batch.Exec(uint8(i), fmt.Sprintf("value_%d", i), time.Now()); err != nil { return err } } } if err := scope.Commit(); err != nil { return err } rows, err := conn.QueryContext(ctx, "SELECT Col1, Col2, Col3 FROM example WHERE Col1 >= $1 AND Col2 <> $2", 0, "xxx") if err != nil { return err } for rows.Next() { var ( col1 uint8 col2 string col3 time.Time ) if err := rows.Scan(&col1, &col2, &col3); err != nil { return err } fmt.Printf("row: col1=%d, col2=%s, col3=%s\n", col1, col2, col3) } rows.Close() return rows.Err() } func main() { if err := example(); err != nil { log.Fatal(err) } } ```