forked from danielkurniadi/clickhouse
-
-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OpenDB is not declared #74
Comments
It declared in "github.com/ClickHouse/clickhouse-go/v2" |
https://github.com/go-gorm/clickhouse |
Yeah, the advanced README example seems to be a bit messed up. This code worked for me: var dsn = "clickhouse://default:@127.0.0.1:9000/db?dial_timeout=200ms&max_execution_time=60"
if _, err = gorm.Open(clickhouse.Open(dsn), &gorm.Config{}); err != nil {
fmt.Printf("failed to connect database, got error %v", err)
os.Exit(1)
}
fmt.Printf("No error. Connectivity ok.\n") Assuming In the advanced example |
Here is an advanced example: package main
import (
"context"
"fmt"
"os"
"time"
chapi "github.com/ClickHouse/clickhouse-go/v2"
"github.com/tlalocweb/hulation/config"
"gorm.io/driver/clickhouse"
"gorm.io/gorm"
)
func main() {
var confpath string
if len(os.Args) > 1 {
confpath = os.Args[1]
}
if len(confpath) < 1 {
fmt.Printf("Error: config file path not provided.\n")
os.Exit(1)
}
myconf, err := config.LoadConfig(confpath)
if err != nil {
fmt.Printf("Error loading config: (%s) %s", confpath, err.Error())
os.Exit(1)
}
dsn := config.GetDSNFromConfig(myconf)
fmt.Printf("Connecting to %s\n", dsn)
// or ... whatver ...
// var dsn = "clickhouse://default:@127.0.0.1:9000/db?dial_timeout=200ms&max_execution_time=60"
fmt.Printf("testing clickhouse-go library...\n")
conn := chapi.OpenDB(&chapi.Options{
Addr: []string{fmt.Sprintf("%s:%d", myconf.DBConfig.Host, myconf.DBConfig.Port)},
Auth: chapi.Auth{
Database: myconf.DBConfig.DBName,
Username: myconf.DBConfig.Username,
Password: myconf.DBConfig.Password,
},
Settings: chapi.Settings{
"max_execution_time": 60,
},
DialTimeout: 5 * time.Second,
Compression: &chapi.Compression{
Method: chapi.CompressionLZ4,
},
})
conn.SetMaxIdleConns(5)
conn.SetMaxOpenConns(10)
conn.SetConnMaxLifetime(time.Hour)
ctx := chapi.Context(context.Background(), chapi.WithSettings(chapi.Settings{
"max_block_size": 10,
}), chapi.WithProgress(func(p *chapi.Progress) {
fmt.Println("progress: ", p)
}))
if err := conn.PingContext(ctx); err != nil {
if exception, ok := err.(*chapi.Exception); ok {
fmt.Printf("Catch exception [%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
} else {
fmt.Printf("Error: %s\n", err.Error())
}
os.Exit(1)
} else {
fmt.Println("Ping OK")
}
// conn.Close()
fmt.Printf("testing gorm w/ clickhouse driver...\n")
if _, err = gorm.Open(clickhouse.New(clickhouse.Config{
Conn: conn,
}), &gorm.Config{}); err != nil {
fmt.Printf("failed to connect database, got error %v", err)
os.Exit(1)
}
fmt.Printf("No error. Connectivity ok.\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
gorm.io/driver/clickhouse version is v0.5.0
The text was updated successfully, but these errors were encountered: