-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
97 lines (82 loc) · 2.13 KB
/
main.go
File metadata and controls
97 lines (82 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"context"
"fmt"
"time"
"clickhouse_postgres/domain"
repoclickhouse "clickhouse_postgres/repository/clickhouse"
repopostgres "clickhouse_postgres/repository/postgres"
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/go-pg/pg"
)
func main() {
//repo, ctx := initializeClickHouseRepository()
repo, ctx := initializePostgresRepository()
defer repo.Close()
// create
err := repo.InitializeAuthorTable(ctx)
if err != nil {
fmt.Println("failed to create table with : " + err.Error())
return
}
// insert
authorRob := &domain.Author{
Id: 1,
Name: "Rob",
Age: 42,
}
repo.Save(ctx, authorRob)
if err != nil {
fmt.Println("failed to insert with : " + err.Error())
return
}
// query
author, errQyery := repo.GetByID(ctx, authorRob.Id)
if errQyery != nil {
fmt.Println("failed to query with : " + errQyery.Error())
return
}
fmt.Println(author)
//drop
err = repo.DropAuthorTable(ctx)
if err != nil {
fmt.Println("failed to drop with : " + err.Error())
return
}
}
func initializeClickHouseRepository() (domain.AuthorRepository, context.Context) {
//docker run -d --name clickhouse-server --ulimit nofile=262144:262144 -p 9000:9000 yandex/clickhouse-server:lastest
conn, err := clickhouse.Open(&clickhouse.Options{
Addr: []string{"127.0.0.1:9000"},
Auth: clickhouse.Auth{
Database: "default",
Username: "default",
Password: "",
},
Settings: clickhouse.Settings{
"max_execution_time": 60,
},
DialTimeout: 5 * time.Second,
Compression: &clickhouse.Compression{
Method: clickhouse.CompressionLZ4,
},
})
if err != nil {
panic("failed to connect with : " + err.Error())
}
ctx := clickhouse.Context(context.Background(), clickhouse.WithSettings(clickhouse.Settings{
"max_block_size": 10,
}))
repo := repoclickhouse.NewAuthorRepository(conn)
return repo, ctx
}
func initializePostgresRepository() (domain.AuthorRepository, context.Context) {
db := pg.Connect(&pg.Options{
Addr: "127.0.0.1:5432",
User: "postgres",
Password: "123456",
Database: "postgres",
})
repo := repopostgres.NewAuthorRepository(db)
return repo, context.Background()
}