-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (117 loc) · 2.69 KB
/
main.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"bufio"
"flag"
"flinkCDC_sql_creator/domain"
"flinkCDC_sql_creator/mysql"
"flinkCDC_sql_creator/oracle"
"fmt"
"gopkg.in/yaml.v3"
"io/ioutil"
"log"
"os"
"runtime"
"runtime/debug"
"sync"
)
var configFile = flag.String("f", "conf/config.yaml", "the config file")
func main() {
//从文件获取配置
var wg sync.WaitGroup
wg.Add(1)
var c domain.Config
yamlFile, err := ioutil.ReadFile(*configFile)
err = yaml.Unmarshal(yamlFile, &c)
if err != nil {
log.Println("配置文件解析失败!")
return
}
c.SrcDb.Trans()
c.SinkDb.Trans()
mysql.InitMap()
oracle.InitMap()
var src []string
var sink []string
go func() {
defer wg.Done()
switch c.SrcDb.Type {
case "mysql":
src = mysql.MysqlSrcCreator(&c)
case "oracle":
for _, rule := range c.TableRule {
src = append(src, oracle.OracleSrcCreator(&c, rule.Src.Database)...)
}
default:
fmt.Println("未匹配的数据库类型,等待支持。")
}
}()
switch c.SinkDb.Type {
case "mysql":
sink = mysql.MysqlSinkCreator(&c)
case "oracle":
fmt.Println("flink-jdbc不支持oracle相关操作!")
default:
fmt.Println("未匹配的数据库类型,等待支持。")
}
filePath := c.OutputDir + "/create.sql"
os.MkdirAll(c.OutputDir, os.ModeDir)
file, err := os.OpenFile(filePath, os.O_TRUNC|os.O_CREATE, 0666)
if err != nil {
fmt.Printf("open file error=%v\n", err)
return
}
defer file.Close()
wg.Wait()
bufWriter := bufio.NewWriter(file)
limitChan := make(chan struct{}, runtime.GOMAXPROCS(runtime.NumCPU())) // 最大并发协程数
var mutex sync.Mutex
for _, s1 := range src {
limitChan <- struct{}{}
wg.Add(1)
s1 := s1
go func() {
defer func() {
if e := recover(); e != nil {
fmt.Printf("WriteData panic: %v,stack: %s\n", e, debug.Stack())
// return
}
wg.Done()
<-limitChan
}()
mutex.Lock() // 要加锁/解锁,否则 bufWriter.WriteString 写入数据有问题
_, err := bufWriter.WriteString(s1)
if err != nil {
fmt.Printf("WriteDataToTxt WriteString err: %v\n", err)
return
}
mutex.Unlock()
}()
}
for _, s1 := range sink {
limitChan <- struct{}{}
wg.Add(1)
s1 := s1
go func() {
defer func() {
if e := recover(); e != nil {
fmt.Printf("WriteData panic: %v,stack: %s\n", e, debug.Stack())
// return
}
wg.Done()
<-limitChan
}()
mutex.Lock() // 要加锁/解锁,否则 bufWriter.WriteString 写入数据有问题
_, err := bufWriter.WriteString(s1)
if err != nil {
fmt.Printf("WriteData WriteString err: %v\n", err)
return
}
mutex.Unlock()
}()
}
wg.Wait()
err = bufWriter.Flush()
if err != nil {
fmt.Printf("写文件失败: %v\n", err)
}
}