/
clickhouse.go
93 lines (79 loc) · 2.04 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
package utils
import (
"database/sql"
"fmt"
"github.com/kshvakov/clickhouse"
"log"
)
var db = &sql.DB{}
func init() {
var url = "tcp://192.168.0.23:9000?debug=true"
db, _ = sql.Open("clickhouse", url)
}
func CreateTable(){
var url = "tcp://192.168.0.23:9000?debug=true&database=db_hystrix"
connect, err := sql.Open("clickhouse", url)
if err != nil {
log.Fatal(err)
}
if err := connect.Ping(); err != nil {
if exception, ok := err.(*clickhouse.Exception); ok {
fmt.Printf("[%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
} else {
fmt.Println(err)
}
return
}
_, err = connect.Exec(`
CREATE TABLE IF NOT EXISTS hystrix (
uuid String,
appName String,
interfaceName String,
shortCircuited String,
rollingCountTimeout Float64,
rollingCountFailure Float64,
th99 Float64,
note String,
insertDate String
) engine=Memory
`)
if err != nil {
log.Fatal(err)
}
}
func InsertData(uuid string, appName string, interfaceName string, shortCircuited string, rollingCountTimeout float64, rollingCountFailure float64, th99 float64, note string, insertDate string) {
var url= "tcp://192.168.0.23:9000?database=db_hystrix"
connect, err := sql.Open("clickhouse", url)
if err != nil {
log.Fatal(err)
}
if err := connect.Ping(); err != nil {
if exception, ok := err.(*clickhouse.Exception); ok {
fmt.Printf("[%d] %s \n%s\n", exception.Code, exception.Message, exception.StackTrace)
} else {
fmt.Println(err)
}
return
}
var (
tx, _= connect.Begin()
stmt, _= tx.Prepare("INSERT INTO hystrix (uuid, appName, interfaceName, shortCircuited, rollingCountTimeout, rollingCountFailure, th99, note, insertDate) VALUES (?,?,?,?,?,?,?,?,?)")
)
defer stmt.Close()
if _, err := stmt.Exec(
uuid,
appName,
interfaceName,
shortCircuited,
rollingCountTimeout,
rollingCountFailure,
th99,
note,
insertDate,
); err != nil {
log.Fatal(err)
}
if err := tx.Commit(); err != nil {
log.Fatal(err)
}
}