# ClickHouse [![Build Status](https://travis-ci.org/kshvakov/clickhouse.svg?branch=master)](https://travis-ci.org/kshvakov/clickhouse) [![Go Report Card](https://goreportcard.com/badge/github.com/kshvakov/clickhouse)](https://goreportcard.com/report/github.com/kshvakov/clickhouse) [![codecov](https://codecov.io/gh/kshvakov/clickhouse/branch/master/graph/badge.svg)](https://codecov.io/gh/kshvakov/clickhouse)
Golang SQL database driver for [Yandex ClickHouse](https://clickhouse.yandex/)
## Key features
* Uses native ClickHouse tcp client-server protocol
* Compatibility with `database/sql`
* Round Robin load-balancing
* Bulk write support : `begin->prepare->(in loop exec)->commit`
## 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 are larger then the data will be split into several blocks to send them to the server
* debug - enable debug output (boolean value)
SSL/TLS parameters:
* secure - establish secure connection (default is false)
* skip_verify - skip certificate verification (default is true)
example:
```
tcp://host1:9000?username=user&password=qwerty&database=clicks&read_timeout=10&write_timeout=20&alt_hosts=host2:9000,host3:9000
```
## 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)](https://clickhouse.yandex/reference_en.html#Array(T)) [godoc](https://godoc.org/github.com/kshvakov/clickhouse#Array)
## TODO
* Compression
## Install
```
go get -u github.com/kshvakov/clickhouse
```
## Example
```go
package main
import (
"database/sql"
"fmt"
"log"
"time"
"github.com/kshvakov/clickhouse"
)
func main() {
connect, 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 (?, ?, ?, ?, ?, ?)")
)
defer stmt.Close()
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)
}
defer rows.Close()
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](https://github.com/jmoiron/sqlx)
```go
package main
import (
"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)
}
}
```
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【项目资源】: 包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。 包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python、web、C#、EDA、proteus、RTOS等项目的源码。 【项目质量】: 所有源码都经过严格测试,可以直接运行。 功能在确认正常工作后才上传。 【适用人群】: 适用于希望学习不同技术领域的小白或进阶学习者。 可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。 【附加价值】: 项目具有较高的学习借鉴价值,也可直接拿来修改复刻。 对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。 【沟通交流】: 有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 鼓励下载和使用,并欢迎大家互相学习,共同进步。
资源推荐
资源详情
资源评论
收起资源包目录
易观第二届OLAP漏斗算法大赛 开源组第一名开源代码.zip (131个子文件)
.gitignore 40B
feature_reflect_struct_decoder.go 25KB
feature_reflect.go 20KB
feature_reflect_native.go 17KB
feature_reflect_extension.go 12KB
feature_config.go 8KB
feature_stream_string.go 8KB
feature_iter_float.go 7KB
import.go 7KB
feature_reflect_map.go 7KB
clickhouse.go 7KB
feature_stream.go 7KB
feature_any_object.go 7KB
feature_stream_int.go 7KB
feature_iter.go 7KB
feature_iter_int.go 6KB
block.go 6KB
feature_any.go 5KB
feature_reflect_object.go 5KB
feature_any_array.go 5KB
feature_iter_string.go 5KB
feature_iter_object.go 5KB
bootstrap.go 5KB
stmt.go 4KB
feature_reflect_slice.go 4KB
feature_adapter.go 4KB
rows.go 4KB
array.go 3KB
connect.go 3KB
feature_iter_skip.go 3KB
uuid.go 3KB
decoder.go 3KB
feature_iter_skip_sloppy.go 3KB
uuid.go 3KB
enum.go 3KB
feature_any_string.go 3KB
column.go 3KB
encoder.go 3KB
feature_reflect_array.go 3KB
helpers.go 2KB
buffer.go 2KB
block_write_column.go 2KB
array.go 2KB
feature_stream_float.go 2KB
feature_any_number.go 2KB
value_converter.go 2KB
feature_any_bool.go 2KB
feature_iter_skip_strict.go 2KB
clickhouse_write_block.go 2KB
nullable.go 2KB
datetime.go 2KB
fixed_string.go 1KB
feature_any_invalid.go 1KB
ip.go 1KB
server_info.go 1KB
write_column.go 1KB
clickhouse_send_query.go 1KB
feature_any_float.go 1KB
clickhouse_read_meta.go 1KB
common.go 1KB
feature_iter_array.go 1KB
feature_any_uint32.go 1KB
feature_any_uint64.go 1KB
feature_any_int32.go 1KB
feature_any_int64.go 1KB
date.go 1KB
feature_pool.go 1KB
string.go 953B
feature_any_nil.go 916B
jsoniter.go 884B
clickhouse_exception.go 866B
clickhouse_profile_info.go 849B
client_info.go 643B
uint64.go 641B
int8.go 616B
word_matcher.go 598B
int64.go 584B
uint8.go 576B
protocol.go 558B
uint16.go 550B
uint32.go 550B
int32.go 539B
int16.go 539B
clickhouse_ping.go 531B
float64.go 523B
float32.go 523B
string.go 497B
array.go 478B
clickhouse_progress.go 439B
clickhouse_read_block.go 372B
feature_json_number.go 268B
result.go 265B
vendor.json 2KB
LICENSE 1KB
LICENSE 1KB
Makefile 148B
README.md 5KB
README.md 3KB
README.md 2KB
fuzzy_mode_convert_table.md 927B
共 131 条
- 1
- 2
资源评论
妄北y
- 粉丝: 1w+
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功