Skip to content

Instantly share code, notes, and snippets.

@kavirajk
Created August 3, 2026 15:19
Show Gist options
  • Select an option

  • Save kavirajk/1d20a967914ecf66cc5a24ceaa4f605c to your computer and use it in GitHub Desktop.

Select an option

Save kavirajk/1d20a967914ecf66cc5a24ceaa4f605c to your computer and use it in GitHub Desktop.
go_input_ch_sql
package main
import (
"fmt"
"log"
"time"
"github.com/ClickHouse/clickhouse-go/v2"
)
const (
createQuery = `CREATE OR REPLACE TABLE events
(
user_id UInt64,
event_date Date,
event_name LowCardinality(String)
)
ENGINE = MergeTree
ORDER BY (event_date, user_id)`
insertQuery = `INSERT INTO events
SELECT
id,
toDate(parseDateTimeBestEffort(ts)),
lowerUTF8(trim(name))
FROM input('id UInt64, ts String, name String')
FORMAT CSV
42,"2026-08-03 14:22:01"," PageView"
43,"2026-08-03T14:23:11Z","Click"`
)
func main() {
// ctx := context.Background()
// Malformed percent-encoding in DSN parameter
conn := clickhouse.OpenDB(&clickhouse.Options{
Addr: []string{"localhost:8123"},
Protocol: clickhouse.HTTP,
Auth: clickhouse.Auth{
Database: "default",
Username: "default",
Password: "",
},
})
_, err := conn.Exec(createQuery)
if err != nil {
panic(err)
}
_, err = conn.Exec(insertQuery)
if err != nil {
panic(err)
}
row := conn.QueryRow("SELECT * from events LIMIT 1")
var (
id uint64
ts time.Time
name string
)
if err := row.Scan(&id, &ts, &name); err != nil {
log.Fatal(err)
}
fmt.Printf("id: %d \nevent_time: %s \nevent_name: %s\n", id, ts, name)
}
@kavirajk

kavirajk commented Aug 3, 2026

Copy link
Copy Markdown
Author
$ go run simple.go
id: 42
event_time: 2026-08-03 00:00:00 +0000 UTC
event_name: pageview

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment