-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced database/sql driver for clickhouse
- Loading branch information
0 parents
commit 1241723
Showing
28 changed files
with
2,193 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea/ | ||
_vendor/ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
sudo: required | ||
notifications: | ||
email: false | ||
language: go | ||
go: | ||
- 1.7 | ||
- 1.8 | ||
|
||
services: | ||
- docker | ||
|
||
before_install: | ||
- travis_retry docker pull yandex/clickhouse-server | ||
- docker run -d -p 127.0.0.1:8123:8123 --name dbr-clickhouse-server yandex/clickhouse-server | ||
|
||
install: | ||
- travis_retry go get -u github.com/golang/lint/golint | ||
- travis_retry go get github.com/mattn/goveralls | ||
- travis_retry go get golang.org/x/tools/cmd/cover | ||
- travis_retry go get github.com/stretchr/testify | ||
|
||
before_script: | ||
- export TEST_CLICKHOUSE_DSN="http://localhost:8123/default" | ||
|
||
script: | ||
- test -z "$(golint ./... | tee /dev/stderr)" | ||
- go vet ./... | ||
- test -z "$(gofmt -d -s . | tee /dev/stderr)" | ||
- go test -v -race -covermode=count -coverprofile=coverage.out . | ||
- goveralls -coverprofile=coverage.out -service=travis-ci |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Mail.Ru Group | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
# ClickHouse [![Build Status](https://travis-ci.org/mailru/go-clickhouse.svg?branch=master)](https://travis-ci.org/mailru/go-clickhouse) [![Go Report Card](https://goreportcard.com/badge/github.com/mailru/go-clickhouse)](https://goreportcard.com/report/github.com/mailru/go-clickhouse) [![Coverage Status](https://coveralls.io/repos/github/mailru/go-clickhouse/badge.svg?branch=master)](https://coveralls.io/github/mailru/go-clickhouse?branch=master) | ||
|
||
Yet another Golang SQL database driver for [Yandex ClickHouse](https://clickhouse.yandex/) | ||
|
||
## Key features | ||
|
||
* Uses official http interface | ||
* Compatibility with [dbr](https://github.com/mailru/dbr) | ||
|
||
## DSN | ||
* timeout - is the maximum amount of time a dial will wait for a connect to complete | ||
* idle_timeout - is the maximum amount of time an idle (keep-alive) connection will remain idle before closing itself. | ||
* read_timeout - specifies the amount of time to wait for a server's response | ||
* location - timezone to parse Date and DateTime | ||
* debug - enables debug logging | ||
* other clickhouse options can be specified as well (except default_format) | ||
|
||
example: | ||
``` | ||
http://user:password@host:8123/clicks?read_timeout=10&write_timeout=20 | ||
``` | ||
|
||
## Supported data types | ||
|
||
* UInt8, UInt16, UInt32, UInt64, Int8, Int16, Int32, Int64 | ||
* Float32, Float64 | ||
* String | ||
* FixedString(N) | ||
* Date | ||
* DateTime | ||
* Enum | ||
* [Array(T) (one-dimensional)](https://clickhouse.yandex/reference_en.html#Array(T)) | ||
|
||
|
||
## Install | ||
``` | ||
go get -u github.com/mailru/go-clickhouse | ||
``` | ||
|
||
## Example | ||
```go | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"log" | ||
"time" | ||
|
||
"github.com/mailru/go-clickhouse" | ||
) | ||
|
||
func main() { | ||
connect, err := sql.Open("clickhouse", "http://127.0.0.1:8123/default") | ||
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 < 100; 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) | ||
} | ||
} | ||
``` | ||
|
||
Use [dbr](https://github.com/mailru/dbr) | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
_ "github.com/mailru/go-clickhouse" | ||
"github.com/mailru/dbr" | ||
) | ||
|
||
func main() { | ||
connect, err := dbr.Open("clickhouse", "http://127.0.0.1:8123/default", nil) | ||
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"` | ||
} | ||
sess := connect.NewSession(nil) | ||
query := sess.Select("country_code", "os_id", "browser_id", "categories", "action_time").From("example") | ||
query.Where(dbr.Eq("country_code", "RU")) | ||
if _, err := query.Load(&items); 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) | ||
} | ||
} | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package clickhouse | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
) | ||
|
||
func init() { | ||
sql.Register("clickhouse", new(chDriver)) | ||
} | ||
|
||
// chDriver implements sql.Driver interface | ||
type chDriver struct { | ||
} | ||
|
||
// Open returns new db connection | ||
func (d *chDriver) Open(dsn string) (driver.Conn, error) { | ||
cfg, err := ParseDSN(dsn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return newConn(cfg), nil | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package clickhouse | ||
|
||
import ( | ||
"database/sql" | ||
"os" | ||
"reflect" | ||
"sync" | ||
"time" | ||
|
||
"database/sql/driver" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
var ( | ||
_ driver.Driver = new(chDriver) | ||
) | ||
|
||
var ddls = []string{ | ||
`DROP TABLE IF EXISTS data`, | ||
`CREATE TABLE data ( | ||
i64 Int64, | ||
u64 UInt64, | ||
f64 Float64, | ||
s String, | ||
a Array(Int16), | ||
d Date, | ||
t DateTime, | ||
e Enum8('one'=1, 'two'=2, 'three'=3) | ||
) ENGINE = Memory`, | ||
`INSERT INTO data VALUES | ||
(-1, 1, 1.0, '1', [1], '2011-03-06', '2011-03-06 06:20:00', 'one'), | ||
(-2, 2, 2.0, '2', [2], '2012-05-31', '2012-05-31 11:20:00', 'two'), | ||
(-3, 3, 3.0, '3', [3], '2016-04-04', '2016-04-04 11:30:00', 'three') | ||
`, | ||
} | ||
|
||
var initialzer = new(dbInit) | ||
|
||
type dbInit struct { | ||
mu sync.Mutex | ||
done bool | ||
} | ||
|
||
type chSuite struct { | ||
suite.Suite | ||
conn *sql.DB | ||
} | ||
|
||
func (s *chSuite) SetupSuite() { | ||
dsn := os.Getenv("TEST_CLICKHOUSE_DSN") | ||
if len(dsn) == 0 { | ||
dsn = "http://localhost:8123/test" | ||
} | ||
conn, err := sql.Open("clickhouse", dsn) | ||
s.Require().NoError(err) | ||
s.Require().NoError(initialzer.Do(conn)) | ||
s.conn = conn | ||
} | ||
|
||
func (s *chSuite) TearDownSuite() { | ||
s.conn.Close() | ||
_, err := s.conn.Query("SELECT 1") | ||
s.EqualError(err, "sql: database is closed") | ||
} | ||
|
||
func (d *dbInit) Do(conn *sql.DB) error { | ||
if d.done { | ||
return nil | ||
} | ||
d.mu.Lock() | ||
defer d.mu.Unlock() | ||
|
||
if d.done { | ||
return nil | ||
} | ||
for _, ddl := range ddls { | ||
if _, err := conn.Exec(ddl); err != nil { | ||
return err | ||
} | ||
} | ||
d.done = true | ||
return nil | ||
} | ||
|
||
func scanValues(rows *sql.Rows, template []interface{}) (interface{}, error) { | ||
result := [][]interface{}{} | ||
types := make([]reflect.Type, len(template)) | ||
for i, v := range template { | ||
types[i] = reflect.TypeOf(v) | ||
} | ||
ptrs := make([]interface{}, len(types)) | ||
var err error | ||
for rows.Next() { | ||
if err = rows.Err(); err != nil { | ||
return nil, err | ||
} | ||
for i, t := range types { | ||
ptrs[i] = reflect.New(t).Interface() | ||
} | ||
err = rows.Scan(ptrs...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
values := make([]interface{}, len(types)) | ||
for i, p := range ptrs { | ||
values[i] = reflect.ValueOf(p).Elem().Interface() | ||
} | ||
result = append(result, values) | ||
} | ||
return result, nil | ||
} | ||
|
||
func parseTime(layout, s string) time.Time { | ||
t, err := time.Parse(layout, s) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return t | ||
} | ||
|
||
func parseDate(s string) time.Time { | ||
return parseTime(dateFormat, s) | ||
} | ||
|
||
func parseDateTime(s string) time.Time { | ||
return parseTime(timeFormat, s) | ||
} |
Oops, something went wrong.