goo_clickhouse

package
v1.1.183 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

初始化

goo_clickhouse.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 := goo_clickhouse.DB().Exec(sqlstr); err != nil {
    log.Fatal(err)
}

添加数据

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

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

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

查询数据

rows, err := DB().Query("SELECT name, gender FROM user")
if err != nil {
    goo_log.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 {
    goo_log.Fatal(err)
}

删除表

if _, err := DB().Exec("DROP TABLE user"); err != nil {
	goo_log.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)

func New

func New(conf Config) (cli *client, err 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"`
	ReadTimeout  int32  `yaml:"read_timeout" json:"read_timeout"`
	WriteTimeout int32  `yaml:"write_timeout" json:"write_timeout"`
	AltHosts     string `yaml:"alt_hosts" json:"alt_hosts"`
	AutoPing     bool   `yaml:"auto_ping" json:"auto_ping"`
	Debug        bool   `yaml:"debug" json:"debug"`
}

Jump to

Keyboard shortcuts

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