Created
August 7, 2017 19:07
-
-
Save romanitalian/6ad1d22ba1db816748a441bdfa133758 to your computer and use it in GitHub Desktop.
Code: 73, Message: Unknown format TSVWithNamesAndTypes
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 ( | |
"github.com/mailru/go-clickhouse" | |
"time" | |
"database/sql" | |
"log" | |
) | |
func main() { | |
connect, err := sql.Open("clickhouse", "http://127.0.0.1:9000") | |
if err != nil { | |
log.Fatal(err) | |
} | |
if err := connect.Ping(); err != nil { | |
log.Fatal(err) | |
} | |
_, 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) | |
} | |
tx, err := connect.Begin() | |
if err != nil { | |
log.Fatal(err) | |
} | |
stmt, err := tx.Prepare("INSERT INTO example (country_code, os_id, browser_id, categories, action_day, action_time) VALUES (?, ?, ?, ?, ?, ?)") | |
if err != nil { | |
log.Fatal(err) | |
} | |
for i := 0; i < 10; i++ { | |
if _, err := stmt.Exec( | |
"RU", | |
10+i, | |
100+i, | |
clickhouse.Array([]int16{1, 2, 3}), | |
clickhouse.Date(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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment