This is a creation in Article, where the information may have evolved or changed.
Golang Driver for clickhouse column database
Key Features
- Using native Clickhouse TCP client-server protocol
- Compatible with Database/sql libraries
- The load balance of rotation algorithm is realized.
Dsn
SSL/TLS parameters
- Secure-establishes a secure connection, false by default
- Skip_verify-Skip Security Authentication by default is True
Supported data types
- UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64
- Float32, Float64
- String
- Fixedstring (N)
- Date
- Datetime
- Enum
- Uuid
- Nullable (T)
- Array (T) (one-dimensional) Godoc
Install
go get -u github.com/kshvakov/clickhouse
Example
Package Mainimport ("Database/sql" "FMT" "Log" "Time" "Github.com/kshvakov/clickhouse") func Main () {Co Nnect, err: = SQL. Open ("Clickhouse", "tcp://127.0.0.1:9000?debug=true") if err! = Nil {log. Fatal (ERR)} If err: = Connect. Ping (); Err! = Nil {If exception, OK: = Err. ( *clickhouse. Exception); OK {fmt. Printf ("[%d]%s \n%s\n", exception. Code, exception. Message, exception. StackTrace)} else {fmt. PRINTLN (Err)} return} _, err = connect. Exec (' CREATE TABLE IF not ' EXISTS example (Country_code fixedstring (2), os_id UInt8, browser_id UInt8, Categories Array (Int16), Action_day Date, Action_time DateTime) Engine=memory ') if err! = Nil {log. Fatal (Err)} var (tx, _ = Connect. Begin () stmt, _ = Tx. Prepare ("INSERT into Example" (Country_code, os_id, browser_id, categories, ACTion_day, Action_time) VALUES (?,?,?,?,?,?))) For I: = 0; I < 100; i++ {If _, err: = stmt. Exec ("RU", 10+i, 100+i, Clickhouse. Array ([]int16{1, 2, 3}), time. Now (), time. Now (),); Err! = Nil {log. Fatal (ERR)}} If Err: = Tx.commit (); Err! = Nil {log. Fatal (Err)} rows, err: = Connect. Query ("Select Country_code, os_id, browser_id, Categories, Action_day, Action_time from Example") if err! = Nil { Log. Fatal (ERR)} for rows. Next () {var (country string OS, browser uint8 categories []int16 Actionday, Actiontime time. Time) If err: = rows. Scan (&country, &os, &browser, &categories, &actionday, &actiontime); Err! = Nil {log. Fatal (Err)} log. Printf ("Country:%s, OS:%d, browser:%d, categories:%v,Action_day:%s, Action_time:%s ", country, OS, browser, categories, Actionday, Actiontime)} If _, err: = Connect. Exec ("DROP TABLE example"); Err! = Nil {log. Fatal (ERR)}}
Use SQLX
Supports Golang database/sql extensions.
package mainimport ( "log" "time" "github.com/jmoiron/sqlx" _ "github.com/kshvakov/clickhouse")func main() { connect, err := sqlx.Open("clickhouse", "tcp://127.0.0.1:9000?debug=true") if err != nil { log.Fatal(err) } var items []struct { CountryCode string `db:"country_code"` OsID uint8 `db:"os_id"` BrowserID uint8 `db:"browser_id"` Categories []int16 `db:"categories"` ActionTime time.Time `db:"action_time"` } if err := connect.Select(&items, "SELECT country_code, os_id, browser_id, categories, action_time FROM example"); err != nil { log.Fatal(err) } for _, item := range items { log.Printf("country: %s, os: %d, browser: %d, categories: %v, action_time: %s", item.CountryCode, item.OsID, item.BrowserID, item.Categories, item.ActionTime) }}
Summarize
Basically the driver is compatible with the Database/sql library, so a meaningful job is to let Gorm this ORM support Clickhouse or write an ORM yourself.