-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclickhouse.go
117 lines (105 loc) · 2.88 KB
/
clickhouse.go
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"database/sql"
"log"
"time"
"github.com/kshvakov/clickhouse"
"github.com/patrickmn/go-cache"
)
func connectClickDB() {
var err error
log.Println("Connecting to Clickhouse by connString", config.Clickhouse.ConnString)
clickHouseDB, err = sql.Open("clickhouse", config.Clickhouse.ConnString)
if err != nil {
log.Fatalln("Clickhouse connection error: ", err, config.Clickhouse.ConnString)
}
log.Println("Connected to Clickhouse, pinging")
if err := clickHouseDB.Ping(); err != nil {
if exception, ok := err.(*clickhouse.Exception); ok {
log.Fatalf("[%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
} else {
log.Fatalln("Clickhouse ping error: ", err, config.Clickhouse.ConnString)
}
}
log.Println("Ping to Clickhouse - OK")
createClickDB()
tables := getTablesList()
for _, tbl := range tables {
cached.Set(tbl, 1, cache.NoExpiration)
}
}
func createClickDB() {
_, err := clickHouseDB.Exec(`CREATE DATABASE IF NOT EXISTS ` + config.Clickhouse.DBName)
if err != nil {
log.Fatalln("Creating database error:", err)
}
_, err = clickHouseDB.Exec("USE " + config.Clickhouse.DBName)
if err != nil {
log.Fatalln("Selecting db for session error:", err)
}
}
func getTablesList() []string {
rows, err := clickHouseDB.Query("SHOW TABLES")
if err != nil {
log.Fatalln("Error on SHOW TABLES:", err)
}
var tables []string
for rows.Next() {
var tableRow string
if err := rows.Scan(&tableRow); err != nil {
log.Fatalln("Error on scanning SHOW TABLES:", err)
}
tables = append(tables, tableRow)
}
return tables
}
func createCollectdTable(tableName string) {
_, err := clickHouseDB.Exec(`
CREATE TABLE IF NOT EXISTS ` + tableName + ` (
EventDate DEFAULT toDate(EventTime),
EventTime DateTime DEFAULT now(),
Hostname String,
ParamName String,
ParamValue Float64
) ENGINE = MergeTree(EventDate, (Hostname, EventTime), 8192)
`)
if err != nil {
log.Fatalln("Creating table error:", err)
}
cached.Set(tableName, 1, cache.NoExpiration)
}
func checkAndCreateTableIfNeed(table string) {
_, foundTable := cached.Get(table)
if foundTable != true {
createCollectdTable(table)
}
}
type cdElementData struct {
EventDateTime time.Time
Hostname string
Plugin string
ParamName string
ParamValue float64
}
func insertCollectDToCH(insData []cdElementData) {
if len(insData) < 1 {
return
}
for _, element := range insData {
checkAndCreateTableIfNeed(element.Plugin)
var tx, _ = clickHouseDB.Begin()
var stmt, _ = tx.Prepare(`INSERT INTO ` + element.Plugin + ` (EventTime, Hostname, ParamName, ParamValue) VALUES (?, ?, ?, ?)`)
_, err := stmt.Exec(
element.EventDateTime,
element.Hostname,
element.ParamName,
element.ParamValue,
)
if err != nil {
log.Fatalln("Error on EXEC Statement:", err)
}
if err := tx.Commit(); err != nil {
log.Fatalln("Error on Commit Statement:", err)
}
}
}