goclickhouse

package
v1.2.29 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2024 License: Apache-2.0 Imports: 6 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 DB

func DB() *sql.DB

func Init

func Init(conf Config) error

全局

Types

type Config

type Config struct {
	Driver             string `yaml:"Driver" json:"Driver"`
	Addr               string `yaml:"Addr" json:"Addr"`
	User               string `yaml:"User" json:"User"`
	Password           string `yaml:"Password" json:"Password"`
	Database           string `yaml:"Database" json:"Database"`
	DialTimeout        int32  `yaml:"DialTimeout" json:"DialTimeout"`               // default 30 second
	MaxIdleConn        int    `yaml:"MaxIdleConn" json:"MaxIdleConn"`               // default 5 second
	MaxOpenConn        int    `yaml:"MaxOpenConn" json:"MaxOpenConn"`               // default 10 second
	MaxExecutionTime   int    `yaml:"max_execution_time" json:"MaxExecutionTime"`   // default 60 second
	ConnMaxLifetime    int    `yaml:"ConnMaxLifetime" json:"ConnMaxLifetime"`       //seconds 60 * 60(1hour)
	Tls                bool   `yaml:"Tls" json:"Tls"`                               // tls true 时都会启用https 否则http
	InsecureSkipVerify bool   `yaml:"InsecureSkipVerify" json:"InsecureSkipVerify"` // tls true 才会生效
	AutoPing           bool   `yaml:"AutoPing" json:"AutoPing"`
	Debug              bool   `yaml:"Debug" json:"Debug"`
}

type GoClickHouse

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

func CreateConnection

func CreateConnection(conf Config) (*GoClickHouse, error)

创建一个连接

func New

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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