Jelajahi Sumber

DB: clickhouse

- simple wrapper
Alexey Kim 1 tahun lalu
induk
melakukan
97145db3a3
3 mengubah file dengan 107 tambahan dan 0 penghapusan
  1. 103 0
      clickhouse/clickhouse.go
  2. 2 0
      go.mod
  3. 2 0
      go.sum

+ 103 - 0
clickhouse/clickhouse.go

@@ -1 +1,104 @@
 package clickhouse
+
+import (
+	"context"
+	"fmt"
+	"git.beejay.kim/tool/service"
+	"github.com/ClickHouse/clickhouse-go/v2"
+	"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
+	"github.com/google/uuid"
+	"github.com/rs/zerolog/log"
+	"strings"
+	"sync"
+	"time"
+)
+
+type _ch struct {
+	config *Config
+	cli    clickhouse.Conn
+	debug  bool
+}
+
+func New(config Config, debug bool) (service.Clickhouse, error) {
+	var err error
+	ch := &_ch{
+		config: &config,
+		debug:  debug,
+	}
+
+	if ch.cli, err = clickhouse.Open(&clickhouse.Options{
+		Protocol: clickhouse.Native,
+		Addr: []string{
+			fmt.Sprintf("%s:%d", config.Host, config.Port),
+		},
+		Auth: clickhouse.Auth{
+			Database: config.Database,
+			Username: config.Username,
+			Password: config.Password,
+		},
+		Debug: debug,
+		Compression: &clickhouse.Compression{
+			Method: clickhouse.CompressionLZ4,
+		},
+		DialTimeout: time.Duration(config.Params.Timeout) * time.Second,
+		ReadTimeout: time.Duration(config.Params.ReadTimeout) * time.Second,
+	}); err != nil {
+		return nil, err
+	}
+
+	if err = ch.Ping(context.TODO()); err != nil {
+		return nil, err
+	}
+
+	return ch, nil
+}
+
+func (ch *_ch) ID() uuid.UUID {
+	return uuid.NewSHA1(uuid.NameSpaceDNS, []byte("db.clickhouse"))
+}
+
+func (ch *_ch) Close() error {
+	var wg sync.WaitGroup
+	wg.Add(1)
+
+	go func() {
+		defer wg.Done()
+
+		if ch.cli != nil {
+			_ = ch.cli.Close()
+		}
+
+		log.Debug().
+			Str("clickhouse", "stop session").
+			Send()
+	}()
+
+	wg.Wait()
+	return nil
+}
+
+func (ch *_ch) Ping(ctx context.Context) error {
+	return ch.cli.Ping(ctx)
+}
+
+func (ch *_ch) Query(ctx context.Context, query string, args ...any) (driver.Rows, error) {
+	return ch.cli.Query(ctx, query, args...)
+}
+
+//goland:noinspection ALL
+func (ch *_ch) Batch(ctx context.Context, tblName string, release bool) (driver.Batch, error) {
+	if tblName = strings.TrimSpace(tblName); tblName == "" {
+		return nil, fmt.Errorf("illegal argumets: empty table name")
+	}
+
+	var (
+		query = fmt.Sprintf("INSERT INTO `%s`", tblName)
+		opts  []driver.PrepareBatchOption
+	)
+
+	if release {
+		opts = append(opts, driver.WithReleaseConnection())
+	}
+
+	return ch.cli.PrepareBatch(ctx, query, opts...)
+}

+ 2 - 0
go.mod

@@ -14,6 +14,7 @@ require (
 
 require (
 	github.com/ClickHouse/ch-go v0.58.2 // indirect
+	github.com/andybalholm/brotli v1.0.6 // indirect
 	github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
 	github.com/go-faster/city v1.0.1 // indirect
 	github.com/go-faster/errors v0.6.1 // indirect
@@ -24,6 +25,7 @@ require (
 	github.com/mattn/go-isatty v0.0.19 // indirect
 	github.com/paulmach/orb v0.10.0 // indirect
 	github.com/pierrec/lz4/v4 v4.1.18 // indirect
+	github.com/pkg/errors v0.9.1 // indirect
 	github.com/russross/blackfriday/v2 v2.1.0 // indirect
 	github.com/segmentio/asm v1.2.0 // indirect
 	github.com/shopspring/decimal v1.3.1 // indirect

+ 2 - 0
go.sum

@@ -10,6 +10,8 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc
 github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
 github.com/Microsoft/hcsshim v0.11.1 h1:hJ3s7GbWlGK4YVV92sO88BQSyF4ZLVy7/awqOlPxFbA=
 github.com/Microsoft/hcsshim v0.11.1/go.mod h1:nFJmaO4Zr5Y7eADdFOpYswDDlNVbvcIJJNJLECr5JQg=
+github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI=
+github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
 github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
 github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
 github.com/confluentinc/confluent-kafka-go/v2 v2.3.0 h1:icCHutJouWlQREayFwCc7lxDAhws08td+W3/gdqgZts=