goclickhouse

package
v1.2.121 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

go-clickhouse clickhouse/v2版本

尽支持 http/https database/sql interface ,即将支持 native interface

初始化

goclickhouse.Init(clickhouse.Config{
    Driver:   "clickhouse",
    Addr:     "192.168.1.100:9000",
    User:     "root",
    Password: "123456",
    Database: "test",
})

创建数据库

CREATE DATABASE IF NOT EXISTS test;

创建表

sqlstr := `
    CREATE TABLE IF NOT EXISTS user
    (
        name String,
        gender String,
        birthday Date
    )
    ENGINE = MergeTree()
    ORDER BY (name, gender)
    PARTITION BY toYYYYMM(birthday)
`
if _, err := goclickhouse.DB().Exec(sqlstr); err != nil {
    log.Fatal(err)
}

添加数据

func insert() {
	var (
		tx, _   = goclickhouse.DB().Begin()
		stmt, _ = tx.Prepare(`INSERT INTO user(name, gender) VALUES(?, ?)`)
	)

    data := []interface{}{"", ""}
    if _, err := stmt.Exec(data...); err != nil {
        golog.Error(err)
        return
    }

    if err := tx.Commit(); err != nil {
        golog.Error(err)
	}
}

查询数据

rows, err := DB().Query("SELECT name, gender FROM user")
if err != nil {
    golog.Fatal(err)
}
defer rows.Close()

for rows.Next() {
    var (
        name string
        gender string
    )
    if err := rows.Scan(&name, &gender); err != nil {
        log.Fatal(err)
    }
    fmt.Println(name, gender)
}

if err := rows.Err(); err != nil {
    golog.Fatal(err)
}

删除表

if _, err := DB().Exec("DROP TABLE user"); err != nil {
	golog.Fatal(err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DelClient added in v1.2.108

func DelClient(names ...string)

func Init

func Init(configs ...Config) (err error)

可以一次初始化多个Redis实例或者 多次调用初始化多个实例

Types

type Config

type Config struct {
	Name               string `yaml:"Name" json:"name,optional"`
	Driver             string `yaml:"Driver" json:"driver,optional"`
	Addr               string `yaml:"Addr" json:"addr,optional"`
	User               string `yaml:"User" json:"user,optional"`
	Password           string `yaml:"Password" json:"password,optional"`
	Database           string `yaml:"Database" json:"database,optional"`
	DialTimeout        int32  `yaml:"DialTimeout" json:"dialTimeout,optional"`               // default 30 second
	MaxIdleConn        int    `yaml:"MaxIdleConn" json:"maxIdleConn,optional"`               // default 5 second
	MaxOpenConn        int    `yaml:"MaxOpenConn" json:"maxOpenConn,optional"`               // default 10 second
	MaxExecutionTime   int    `yaml:"MaxExecutionTime" json:"maxExecutionTime,optional"`     // default 60 second
	ConnMaxLifetime    int    `yaml:"ConnMaxLifetime" json:"connMaxLifetime,optional"`       //seconds 60 * 60(1hour)
	TLS                bool   `yaml:"TLS" json:"tls,optional"`                               // tls true 时都会启用https 否则http
	InsecureSkipVerify bool   `yaml:"InsecureSkipVerify" json:"insecureSkipVerify,optional"` // tls true 才会生效
	AutoPing           bool   `yaml:"AutoPing" json:"autoPing,optional"`
	Debug              bool   `yaml:"Debug" json:"debug,optional"`
}

type GoClickHouse

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

func Default added in v1.2.83

func Default() *GoClickHouse

func GetClient added in v1.2.83

func GetClient(names ...string) *GoClickHouse

func New

func New(conf Config) (cli *GoClickHouse, err error)

func (*GoClickHouse) DB added in v1.2.83

func (cli *GoClickHouse) DB() *sql.DB

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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