clickhouse

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2020 License: GPL-3.0 Imports: 16 Imported by: 4

README

Golang Clickhouse connector

Create new connection

package main

import (
	ch "github.com/leprosus/golang-clickhouse"
)

var (
    user = "clickhouse.user"
    pass = "clickhouse.pass"
    host = "clickhouse.host"
    port = 8123
)

func init(){
    conn := ch.New(host, port, user, pass)
    
    // Also you can preset maximum memory usage limit to execute one query
    conn.MaxMemoryUsage(4 * clickhouse.GigaByte)
}

Query rows

conn := ch.New(host, port, user, pass)

iter, err := conn.Fetch("SELECT `database`, `name`, `engine` FROM system.tables")
if err != nil {
    panic(err)
}

for iter.Next() {
    result := iter.Result

    database, _ := result.String("database")
    name, _ := result.String("name")
    engine, _ := result.String("engine")

    println(database, country, engine)
}

Execute insert

conn := ch.New(host, port, user, pass)

query := fmt.Sprintf("INSERT INTO db.table (SomeFiled) VALUES ('%s')", "Some value")
conn.Exec(query)

Values escaping

value := "Here	is tab. This is line comment --"
escaped := clickhouse.Escape(value)
fmt.Print(escaped) //Here\tis tab. This is line comment \-\-

List all methods

Connection
  • clickhouse.New(host, port, user, pass) - creates connection
  • clickhouse.Debug(func(message string)) - sets custom logger for debug
  • clickhouse.Info(func(message string)) - sets custom logger for info
  • clickhouse.Warn(func(message string)) - sets custom logger for warn
  • clickhouse.Error(func(message string)) - sets custom logger for error
  • clickhouse.Fatal(func(message string)) - sets custom logger for fatal
  • conn.Attempts(attempts, wait) - sets amount of attempts and time awaiting after fail request (wait in seconds)
  • conn.MaxMemoryUsage(limit) - sets maximum memory usage per query (limit in bytes)
  • conn.MaxRequests(limit) - sets maximum requests at the same time
  • conn.ConnectTimeout(timeout) - sets connection timeout (timeout in seconds)
  • conn.SendTimeout(timeout) - sets send timeout (timeout in seconds)
  • conn.ReceiveTimeout(timeout) - sets receive timeout (timeout in seconds)
  • conn.Compression(flag) - sets response compression
Fetching
  • conn.Fetch(query) - executes, fetches query and returns iterator and error
  • conn.ForcedFetch(query) - executes, fetches query and returns iterator and error without requests limits
  • conn.FetchOne(query) - executes, fetches query and returns first result and error
  • conn.ForcedFetchOne(query) - executes, fetches query and returns first result and error without requests limits
  • conn.Exec(query) - executes query and returns error
  • conn.ForcedExec(query) - executes query and returns error without requests limits
Iterator
  • iter.Next() - checks if has more data
  • iter.Err() - returns error if exist or nil
  • iter.Result() - returns result
  • iter.Close() - closes data stream
Result
  • result.Columns() - returns columns list
  • result.Exist("FieldName") - returns true if field is exist or false
  • result.String("FieldName") - returns string value and error
  • result.Bytes("FieldName") - returns bytes slice value and error
  • result.Bool("FieldName") - returns boolean value and error
  • result.UInt8("FieldName") - returns unsigned int8 value and error
  • result.UInt16("FieldName") - returns unsigned int16 value and error
  • result.UInt32("FieldName") - returns unsigned int32 value and error
  • result.UInt64("FieldName") - returns unsigned int64 value and error
  • result.Int8("FieldName") - returns int8 value and error
  • result.Int16("FieldName") - returns int16 value and error
  • result.Int32("FieldName") - returns int32 value and error
  • result.Int64("FieldName") - returns int64 value and error
  • result.Float32("FieldName") - returns float32 value and error
  • result.Float64("FieldName") - returns float64 value and error
  • result.Date("FieldName") - parses data YYYY-MM-DD and returns time value and error
  • result.DateTime("FieldName") - parses data YYYY-MM-DD HH:MM:SS and returns time value and error
Escaping
  • clickhouse.Escape("ValueToEscape") - escapes special symbols
  • clickhouse.Unescape("ValueToUndoEscaping") - undoes escaping of special symbols

Documentation

Index

Constants

View Source
const (
	MegaByte = 1024 * 1024
	GigaByte = 1024 * MegaByte
)

Variables

This section is empty.

Functions

func Debug

func Debug(callback func(message string))

Debug sets logger for debug

func Error

func Error(callback func(message string))

Error sets logger for error

func Escape

func Escape(line string) string

Escape escapes special symbols

func Fatal

func Fatal(callback func(message string))

Fatal sets logger for fatal

func Info

func Info(callback func(message string))

Info sets logger for into

func Unescape

func Unescape(line string) string

Unescape undoes escaping of special symbols

func Warn

func Warn(callback func(message string))

Warn sets logger for warning

Types

type Conn

type Conn struct {
	Limiter
	// contains filtered or unexported fields
}

func New

func New(host string, port int, user string, pass string) *Conn

func (*Conn) Attempts

func (conn *Conn) Attempts(amount int, wait int)

Attempts sets amount of attempt query execution

func (*Conn) Compression

func (conn *Conn) Compression(compression bool)

Compression sets new send timeout

func (*Conn) ConnectTimeout

func (conn *Conn) ConnectTimeout(timeout int)

ConnectTimeout sets new connection timeout

func (*Conn) Exec

func (conn *Conn) Exec(query string) error

Exec executes new query

func (*Conn) Fetch

func (conn *Conn) Fetch(query string) (Iter, error)

Fetch executes new query and fetches all data

func (*Conn) FetchOne

func (conn *Conn) FetchOne(query string) (Result, error)

FetchOne executes new query and fetches one row

func (*Conn) ForcedExec

func (conn *Conn) ForcedExec(query string) error

ForcedExec executes new query without requests limits

func (*Conn) ForcedFetch

func (conn *Conn) ForcedFetch(query string) (Iter, error)

ForcedFetch executes new query and fetches all data without requests limits

func (*Conn) ForcedFetchOne

func (conn *Conn) ForcedFetchOne(query string) (Result, error)

ForcedFetchOne executes new query and fetches one row without requests limits

func (*Conn) MaxMemoryUsage

func (conn *Conn) MaxMemoryUsage(limit int)

MaxMemoryUsage sets new maximum memory usage value

func (*Conn) ReceiveTimeout

func (conn *Conn) ReceiveTimeout(timeout int)

ReceiveTimeout sets new receive timeout

func (*Conn) SendTimeout

func (conn *Conn) SendTimeout(timeout int)

SendTimeout sets new send timeout

type Iter

type Iter struct {
	Result Result
	// contains filtered or unexported fields
}

func (Iter) Close

func (iter Iter) Close()

Close closes stream

func (Iter) Err

func (iter Iter) Err() error

Err returns error of iterator

func (*Iter) Next

func (iter *Iter) Next() bool

Next returns next row of data

type Limiter

type Limiter struct {
	// contains filtered or unexported fields
}

func (*Limiter) MaxRequests

func (lim *Limiter) MaxRequests(limit int)

MaxRequests sets requests limitation (zero is limitation off)

type Result

type Result struct {
	// contains filtered or unexported fields
}

func (Result) Bool

func (result Result) Bool(column string) (f bool, err error)

Bool returns value as bool

func (Result) Bytes

func (result Result) Bytes(column string) (bytes []byte, err error)

Bytes returns value of bytes

func (Result) Columns

func (result Result) Columns() (columns []string)

Columns returns columns list

func (Result) Date

func (result Result) Date(column string) (t time.Time, err error)

Date returns value as date

func (Result) DateTime

func (result Result) DateTime(column string) (t time.Time, err error)

DateTime returns value as datetime

func (Result) Exist

func (result Result) Exist(column string) bool

Exist returns true if field is exist or false

func (Result) Float32

func (result Result) Float32(column string) (f32 float32, err error)

Float32 returns value as float32

func (Result) Float64

func (result Result) Float64(column string) (f64 float64, err error)

Float64 returns value as float64

func (Result) Int16

func (result Result) Int16(column string) (i16 int16, err error)

Int16 returns value as int16

func (Result) Int32

func (result Result) Int32(column string) (i32 int32, err error)

Int32 returns value as int32

func (Result) Int64

func (result Result) Int64(column string) (i64 int64, err error)

Int64 returns value as int64

func (Result) Int8

func (result Result) Int8(column string) (i8 int8, err error)

Int8 returns value as int8

func (Result) String

func (result Result) String(column string) (value string, err error)

String returns value of string

func (Result) UInt16

func (result Result) UInt16(column string) (ui16 uint16, err error)

UInt16 returns value as uint16

func (Result) UInt32

func (result Result) UInt32(column string) (ui32 uint32, err error)

UInt32 returns value as uint32

func (Result) UInt64

func (result Result) UInt64(column string) (ui64 uint64, err error)

UInt64 returns value as uint64

func (Result) UInt8

func (result Result) UInt8(column string) (ui8 uint8, err error)

UInt8 returns value as uint8

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL