/
hadoop.go
195 lines (182 loc) · 5.06 KB
/
hadoop.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package compression
import (
"fmt"
"github.com/colinmarc/hdfs"
"io"
"log"
"os"
"oss"
"regexp"
"time"
)
type OssWrite struct {
hdfsDir string
ossDir string
data string
}
type HadoopFile struct {
dirPrefix string
fileInfo os.FileInfo
}
func (h *HadoopFile) GetDirPrefix() string {
return h.dirPrefix
}
func (h *HadoopFile) GetFileInfo() os.FileInfo {
return h.fileInfo
}
func listAllDirFiles(hdfsDir string, hdfsClient *hdfs.Client) ([]HadoopFile, error) {
hdfsFileInfos, err := RecurseInfos(hdfsClient, hdfsDir)
if err != nil {
return nil, err
}
return hdfsFileInfos, nil
}
func RecurseInfos(hdfsClient *hdfs.Client, hdfsDir string) ([]HadoopFile, error) {
hdfsFiles := make([]HadoopFile, 0)
dirs, err := hdfsClient.ReadDir(hdfsDir)
if err != nil {
return nil, err
}
for _, fileInfo := range dirs {
if fileInfo.IsDir() {
recurseInfos, err := RecurseInfos(hdfsClient, hdfsDir+fileInfo.Name()+"/")
if err != nil {
return nil, err
}
for _, f := range recurseInfos {
hdfsFiles = append(hdfsFiles, f)
}
} else {
f := HadoopFile{dirPrefix: hdfsDir, fileInfo: fileInfo}
hdfsFiles = append(hdfsFiles, f)
}
}
return hdfsFiles, nil
}
func ReadHdfsWriteOSS(hdfsDir string, ossDir string, bucket string,
hdfsClient *hdfs.Client, ossClient *oss.Client, cm bool) (int, error) {
if ossDir[len(ossDir)-1:] != "/" {
return 0, fmt.Errorf("ossDir:%s parameter have to be / in the end!", ossDir)
}
if hdfsDir[len(hdfsDir)-1:] != "/" {
return 0, fmt.Errorf("hdfsDir:%s parameter have to be / in the end!", hdfsDir)
}
hadoopFiles, err := listAllDirFiles(hdfsDir, hdfsClient)
log.Printf("%d num hadoop files should be copy to !", len(hadoopFiles))
if err != nil {
return 0, nil
}
countNum := 0
for _, h := range hadoopFiles {
//if use gzip
if cm {
err := gzWriteToOss(h, hdfsClient, ossClient, ossDir, bucket, false)
if err != nil {
return 0, err
}
} else {
err := writeToOss(h, ossDir, hdfsClient, ossClient, bucket, false)
if err != nil {
return 0, err
}
}
countNum++
}
return countNum, nil
}
func gzWriteToOss(h HadoopFile, hdfsClient *hdfs.Client, ossClient *oss.Client, ossDir string, bucket string, b bool) error {
f, err := CreateGZ("/tmp/" + h.fileInfo.Name() + ".gz")
n, err := HadoopReadWriteGzipFile(f, h, hdfsClient)
log.Printf("HadoopReadWrite %d num bytes \n", n)
log.Printf("err:!!!!%s \n", err)
if err != nil && err != io.EOF {
log.Printf("HadoopReadWriteGzipFile failed !%s \n", err)
}
//force commit write
CloseGZ(f)
err = LocalFileReadWriteOssFile(f, h, ossClient, bucket, ossDir, b)
if err != nil {
return err
}
//every thing is ok, now we need to delete this file
err = os.Remove(f.f.Name())
if err != nil {
log.Printf("Failed to remove file, fileName is %s, errmessage:%s ~ \n", f.f.Name(), err)
}
return nil
}
func ReadHdfsWriteForMigrate(hdfsDir string, ossDir string, bucket string,
hdfsClient *hdfs.Client, ossClient *oss.Client, cm bool) (int, error) {
if ossDir[len(ossDir)-1:] != "/" {
return 0, fmt.Errorf("ossDir:%s parameter have to be / in the end!", ossDir)
}
if hdfsDir[len(hdfsDir)-1:] != "/" {
return 0, fmt.Errorf("hdfsDir:%s parameter have to be / in the end!", hdfsDir)
}
hadoopFiles, err := listAllDirFiles(hdfsDir, hdfsClient)
log.Printf("%d num hadoop files should be copy to !", len(hadoopFiles))
if err != nil {
return 0, nil
}
countNum := 0
for _, h := range hadoopFiles {
//if use gzip
if cm {
err := gzWriteToOss(h, hdfsClient, ossClient, ossDir, bucket, true)
if err != nil {
return 0, err
}
} else {
err := writeToOss(h, ossDir, hdfsClient, ossClient, bucket, true)
if err != nil {
return 0, err
}
}
countNum++
}
return countNum, nil
}
func makeTimestamp() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
func getDateStamp(hdfsPath string) (string, error) {
regex := `([a-zA-Z1-9]+)/hourly/(20\d{2})/(\d*)/(\d*)`
r := regexp.MustCompile(regex)
matchStr := r.FindStringSubmatch(hdfsPath)
if len(matchStr) != 5 {
return "", nil
} else {
return matchStr[1] + "/" + matchStr[2] + matchStr[3] + matchStr[4], nil
}
}
func writeToOss(h HadoopFile, ossDir string, hdfsClient *hdfs.Client,
ossClient *oss.Client, bucket string, b bool) error {
log.Printf("Start to sync %s to ossDir %s\n", h.dirPrefix+h.fileInfo.Name(), ossDir)
hdfsPath := h.dirPrefix + h.fileInfo.Name()
timeStamp := fmt.Sprintf("%d", makeTimestamp())
ossPath := ossDir + timeStamp + h.fileInfo.Name()
if b {
dateStr, err := getDateStamp(hdfsPath)
if err != nil {
log.Printf("get date info from hdfsPath %s failed!", hdfsPath)
return err
}
ossPath = dateStr + "/" + timeStamp + h.fileInfo.Name()
if err != nil {
return err
}
}
reader, err := hdfsClient.Open(hdfsPath)
if err != nil {
log.Printf("hdfsClient opend failed! err message: %s\n", err)
return err
}
defer reader.Close()
err = ossClient.PutObject(bucket, ossPath, reader, nil)
if err != nil {
log.Printf("Oss Append Object failed %s!\n", err)
return err
}
log.Printf("Finished sync %s to ossDir \n", h.dirPrefix+h.fileInfo.Name(), ossDir)
return nil
}