Flow acquisition and Analysis---Clickhorse's Golang drive

Source: Internet
Author: User
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

    • Username/password-auth credentials
    • Database-select the current default database
    • Read_timeout/write_timeout-timeout in second
    • No_delay-disable/enable the Nagle algorithm for TCP socket (default
      Is ' true '-disable)
    • Alt_hosts-comma separated list of single address host for
      Load-balancing
    • Connection_open_strategy-random/in_order (default random).

      • Random-choose random server from set
      • In_order-first Live Server is choosen in specified order
    • Block_size-maximum rows in block (default is 1000000). If the rows
      is larger then the data would be is split into several blocks to send
      them to the server
    • Debug-enable Debug Output (boolean value)

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.