Created
August 3, 2026 15:19
-
-
Save kavirajk/1d20a967914ecf66cc5a24ceaa4f605c to your computer and use it in GitHub Desktop.
go_input_ch_sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
commented
Aug 3, 2026
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment