forked from chrislusf/gleam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vfs_hdfs.go
102 lines (81 loc) · 2.34 KB
/
vfs_hdfs.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
package filesystem
import (
"fmt"
"log"
"os"
"strings"
"github.com/colinmarc/hdfs"
)
/*
Get the namenode from
1) the hdfs://namenode/... string
2) Or from env HADOOP_NAMENODE.
3) Or based on env HADOOP_CONF_DIR or HADOOP_HOME
to locate hdfs-site.xml and core-site.xml
*/
type HdfsFileSystem struct {
}
func (fs *HdfsFileSystem) Accept(fl *FileLocation) bool {
return strings.HasPrefix(fl.Location, "hdfs://")
}
func (fs *HdfsFileSystem) Open(fl *FileLocation) (VirtualFile, error) {
namenode, path, err := splitLocationToParts(fl.Location)
if err != nil {
return nil, err
}
if namenode == "" {
namenode = os.Getenv("HADOOP_NAMENODE")
}
client, err := hdfs.New(namenode)
if err != nil {
log.Fatalf("failed to create client to %s:%v\n", namenode, err)
}
file, err := client.Open(path)
return file, err
}
// List generates a full list of file locations under the given
// location, which should have a prefix of hdfs://
func (fs *HdfsFileSystem) List(fl *FileLocation) (fileLocations []*FileLocation, err error) {
namenode, path, err := splitLocationToParts(fl.Location)
if err != nil {
return
}
client, err := hdfs.New(namenode)
if err != nil {
return nil, fmt.Errorf("failed to create client to %s:%v\n", namenode, err)
}
fileInfos, err := client.ReadDir("/" + path)
if err != nil {
return nil, fmt.Errorf("failed to list files under /%s:%v\n", path, err)
}
for _, fi := range fileInfos {
fileLocations = append(fileLocations, &FileLocation{fl.Location + "/" + fi.Name()})
}
return
}
func (fs *HdfsFileSystem) IsDir(fl *FileLocation) bool {
namenode, path, err := splitLocationToParts(fl.Location)
if err != nil {
log.Fatalf("failed to create client to %s:%v\n", namenode, err)
return false
}
client, err := hdfs.New(namenode)
if err != nil {
log.Fatalf("failed to create client to %s:%v\n", namenode, err)
}
file, err := client.Open(path)
if err != nil {
log.Fatalf("failed to open file %s:%v\n", fl.Location, err)
}
defer file.Close()
fi := file.Stat()
return fi.IsDir()
}
func splitLocationToParts(location string) (namenode, path string, err error) {
hdfsPrefix := "hdfs://"
if !strings.HasPrefix(location, hdfsPrefix) {
return "", "", fmt.Errorf("parameter %s should start with hdfs://", location)
}
parts := strings.SplitN(location[len(hdfsPrefix):], "/", 2)
return parts[0], "/" + parts[1], nil
}