go get github.com/ClickHouse/clickhouse-go/v2
conn, err := clickhouse.Open(&clickhouse.Options{
Addr: []string{"<host>:9440"},
Auth: clickhouse.Auth{
Database: "default",
Username: "default",
Password: "<password>",
},
TLS: &tls.Config{},
})
conn, err := clickhouse.Open(&clickhouse.Options{
Addr: []string{"<host>:8443"},
Auth: clickhouse.Auth{
Database: "default",
Username: "default",
Password: "<password>",
},
TLS: &tls.Config{},
Protocol: clickhouse.HTTP,
})
err := conn.Exec(context.Background(), `
CREATE TABLE IF NOT EXISTS my_table (Col1 UInt8, Col2 String)
`)
if err != nil {
return err
}
batch, err := conn.PrepareBatch(ctx, "INSERT INTO my_table")
if err != nil {
return err
}
if err := batch.Append(uint8(42), "ClickHouse")); err != nil {
return err
}
if err := batch.Send(); err != nil {
return err
}
rows, err := conn.Query(ctx, "SELECT Col1, Col2 FROM my_table")
if err != nil {
return err
}
for rows.Next() {
var (
col1 uint8
col2 string
)
if err := rows.Scan(&col1, &col2); err != nil {
return err
}
fmt.Printf("row: col1=%d, col2=%s\n", col1, col2)
}
rows.Close()
return rows.Err()
conn, err := sql.Open("clickhouse", "clickhouse://<host>:9440?username=default&password=<password>/<database>")
conn, err := sql.Open("clickhouse", "http://<host>:8443?username=default&password=<password>/<database>")
err := conn.ExecContext(context.Background(), `
CREATE TABLE IF NOT EXISTS my_table (Col1 UInt8, Col2 String)
`)
if err != nil {
return err
}
stmt, err := conn.PrepareContext(ctx, "INSERT INTO my_table")
if err != nil {
return err
}
if err := stmt.Exec(uint8(42), "ClickHouse")); err != nil {
return err
}
if err := stmt.Commit(); err != nil {
return err
}
rows, err := conn.QueryContext(ctx, "SELECT Col1, Col2 FROM my_table")
if err != nil {
return err
}
for rows.Next() {
var (
col1 uint8
col2 string
)
if err := rows.Scan(&col1, &col2); err != nil {
return err
}
fmt.Printf("row: col1=%d, col2=%s\n", col1, col2)
}
rows.Close()
return rows.Err()