All Projects → mailru → Go Clickhouse

mailru / Go Clickhouse

Licence: mit
Golang SQL database driver for Yandex ClickHouse

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Go Clickhouse

Clickhouse Net
Yandex ClickHouse fully managed .NET client
Stars: ✭ 142 (-40.83%)
Mutual labels:  database, clickhouse
Datafuse
Datafuse is a free Cloud-Native Analytics DBMS(Inspired by ClickHouse) implemented in Rust
Stars: ✭ 327 (+36.25%)
Mutual labels:  database, clickhouse
Clickhouse Native Jdbc
ClickHouse Native Protocol JDBC implementation
Stars: ✭ 310 (+29.17%)
Mutual labels:  database, clickhouse
Gorose
GoRose(go orm), a mini database ORM for golang, which inspired by the famous php framwork laravle's eloquent. It will be friendly for php developer and python or ruby developer. Currently provides six major database drivers: mysql,sqlite3,postgres,oracle,mssql, Clickhouse.
Stars: ✭ 947 (+294.58%)
Mutual labels:  database, clickhouse
Clickhouse.client
.NET client for ClickHouse
Stars: ✭ 85 (-64.58%)
Mutual labels:  database, clickhouse
Clickhouse Go
Golang driver for ClickHouse
Stars: ✭ 1,234 (+414.17%)
Mutual labels:  database, clickhouse
Clickhouse Driver
ClickHouse Python Driver with native interface support
Stars: ✭ 562 (+134.17%)
Mutual labels:  database, clickhouse
Datax
DataX is an open source universal ETL tool that support Cassandra, ClickHouse, DBF, Hive, InfluxDB, Kudu, MySQL, Oracle, Presto(Trino), PostgreSQL, SQL Server
Stars: ✭ 116 (-51.67%)
Mutual labels:  database, clickhouse
Clickhouse Sqlalchemy
ClickHouse dialect for SQLAlchemy
Stars: ✭ 166 (-30.83%)
Mutual labels:  database, clickhouse
Wechat Database
破解微信数据库 获取想要的数据
Stars: ✭ 236 (-1.67%)
Mutual labels:  database
Granite
ORM Model with Adapters for mysql, pg, sqlite in the Crystal Language.
Stars: ✭ 238 (-0.83%)
Mutual labels:  database
Android Mvvm Architecture
This repository contains a detailed sample app that implements MVVM architecture using Dagger2, Room, RxJava2, FastAndroidNetworking and PlaceholderView
Stars: ✭ 2,720 (+1033.33%)
Mutual labels:  database
Yunodb
A portable, persistent, electron-embeddable fulltext search + document store database for node.js
Stars: ✭ 237 (-1.25%)
Mutual labels:  database
Aiida Core
The official repository for the AiiDA code
Stars: ✭ 238 (-0.83%)
Mutual labels:  database
Warehouse Inventory System
Open source inventory management system with php and mysql
Stars: ✭ 235 (-2.08%)
Mutual labels:  database
Sqlhelper
SQL Tools ( Dialect, Pagination, DDL dump, UrlParser, SqlStatementParser, WallFilter, BatchExecutor for Test) based Java. it is easy to integration into any ORM frameworks
Stars: ✭ 242 (+0.83%)
Mutual labels:  database
Filebase
A Simple but Powerful Flat File Database Storage.
Stars: ✭ 235 (-2.08%)
Mutual labels:  database
React Agent
Client and server-side state management library
Stars: ✭ 235 (-2.08%)
Mutual labels:  database
Neo4j Java Driver
Neo4j Bolt driver for Java
Stars: ✭ 241 (+0.42%)
Mutual labels:  database
Ravendb
ACID Document Database
Stars: ✭ 2,870 (+1095.83%)
Mutual labels:  database

ClickHouse Build Status Go Report Card Coverage Status

Yet another Golang SQL database driver for Yandex ClickHouse

Key features

  • Uses official http interface
  • Compatibility with dbr

DSN

schema://user:[email protected][:port]/database?param1=value1&...&paramN=valueN

parameters

  • 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
  • kill_query - enables killing query on the server side if we have error from transport
  • kill_query_timeout - timeout to kill query (default value is 1 second)
  • other clickhouse options can be specified as well (except default_format)

example:

http://user:[email protected]:8123/clicks?read_timeout=10&write_timeout=20

Supported data types

Notes: database/sql does not allow to use big uint64 values. It is recommended use type UInt64 which is provided by driver for such kind of values. type []byte are used as raw string (without quoting) for passing value of type []uint8 to driver as array - please use the wrapper clickhouse.Array for passing decimal value please use the wrappers clickhouse.Decimal*

Supported request params

Clickhouse supports setting query_id and quota_key for each query. The database driver provides ability to set these parameters as well.

There are constants QueryID and QuotaKey for correct setting these params.

quota_key could be set as empty string, but query_id - does not. Keep in mind, that setting same query_id could produce exception or replace already running query depending on current Clickhouse settings. See replace_running_query for details.

See Example section for use cases.

Install

go get -u github.com/mailru/go-clickhouse

Example

package main

import (
	"context"
	"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,
		)
	}

	ctx := context.Background()
	rows, err := connect.QueryContext(context.WithValue(ctx, clickhouse.QueryID, "dummy-query-id"), `
		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

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,
		)
	}
}

Go versions

Officially support last 3 golang releases

Development

You can check the effect of changes on Travis CI or run tests locally:

make init # dep ensure and install
make test

Remember that make init will add a few binaries used for testing (like golint and it's dependencies) into your GOPATH

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].