This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
hdfs_backend.go
111 lines (90 loc) · 2.19 KB
/
hdfs_backend.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
package backend
import (
"fmt"
"io"
"path"
"strings"
"github.com/colinmarc/hdfs"
)
type HdfsBackend struct {
client *hdfs.Client
namenode string
path string
}
func NewHdfsBackend(client *hdfs.Client, namenode string, hdfsPath string) *HdfsBackend {
return &HdfsBackend{
client: client,
namenode: namenode,
path: path.Clean(hdfsPath),
}
}
func (h *HdfsBackend) ListDBs() ([]string, error) {
files, err := h.client.ReadDir(h.path)
if err != nil {
return nil, err
}
var res []string
for _, f := range files {
if !f.IsDir() {
continue
}
res = append(res, f.Name())
}
return res, nil
}
func (h *HdfsBackend) ListVersions(db, after string, checkForSuccess bool) ([]string, error) {
files, err := h.client.ReadDir(path.Join(h.path, db))
if err != nil {
return nil, err
}
var res []string
for _, f := range files {
if !f.IsDir() {
continue
}
name := f.Name()
fullPath := path.Join(h.path, db, name)
if name > after && (!checkForSuccess || h.checkForSuccessFile(fullPath)) {
res = append(res, name)
}
}
return res, nil
}
func (h *HdfsBackend) ListFiles(db, version string) ([]string, error) {
infos, err := h.client.ReadDir(path.Join(h.path, db, version))
if err != nil {
return nil, err
}
var res []string
for _, info := range infos {
name := info.Name()
if !info.IsDir() && !strings.HasPrefix(name, "_") && !strings.HasPrefix(name, ".") {
res = append(res, path.Base(info.Name()))
}
}
return res, nil
}
func (h *HdfsBackend) Open(db, version, file string) (io.ReadCloser, error) {
src := path.Join(h.path, db, version, file)
f, err := h.client.Open(src)
if err != nil {
return nil, err
}
return f, nil
}
func (h *HdfsBackend) DisplayPath(parts ...string) string {
allParts := append([]string{h.path}, parts...)
return h.displayURL(allParts...)
}
func (h *HdfsBackend) displayURL(parts ...string) string {
p := strings.TrimPrefix(path.Join(parts...), "/")
return fmt.Sprintf("hdfs://%s/%s", h.namenode, p)
}
func (h *HdfsBackend) checkForSuccessFile(versionPath string) bool {
successPath := path.Join(versionPath, "_SUCCESS")
if _, err := h.client.Stat(successPath); err == nil {
return true
} else {
return false
}
}