package main import ( "context" "fmt" "github.com/ClickHouse/clickhouse-go/v2" "github.com/ClickHouse/clickhouse-go/v2/lib/driver" "time" ) // 创建clickhouse连接 func InitClickHouse(addr []string, size int, database, username, password string) (driver.Conn, error) { var ( ctx = context.Background() conn, err = clickhouse.Open(&clickhouse.Options{ //Addr: []string{"cc-2ze9tv451wov14w9e.clickhouse.ads.aliyuncs.com:9000"}, //内网 //Addr: []string{"cc-2ze9tv451wov14w9e.public.clickhouse.ads.aliyuncs.com:9000"}, //外网 Addr: addr, DialTimeout: 10 * time.Second, MaxIdleConns: 3, MaxOpenConns: size, Auth: clickhouse.Auth{ //Database: "information", //Username: "biservice", //Password: "Bi_top95215#", Database: database, Username: username, Password: password, }, Debugf: func(format string, v ...interface{}) { fmt.Printf(format, v) }, }) ) if err != nil { return nil, err } if err := conn.Ping(ctx); err != nil { if exception, ok := err.(*clickhouse.Exception); ok { fmt.Printf("Exception [%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace) } return nil, err } return conn, nil }