1
|
|
|
package files |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"io" |
5
|
|
|
"os" |
6
|
|
|
|
7
|
|
|
"github.com/colinmarc/hdfs" |
8
|
|
|
) |
9
|
|
|
|
10
|
|
|
// FileSystemClient interface is an abstraction around requests to an hdfs file system. |
11
|
|
|
type FileSystemClient interface { |
12
|
|
|
Close() error |
13
|
|
|
Name() string |
14
|
|
|
Upload(source, destination string) error |
15
|
|
|
Download(source, destination string) error |
16
|
|
|
Move(oldpath, newpath string) error |
17
|
|
|
MakeDir(path string, permission os.FileMode) error |
18
|
|
|
List(path string) ([]HdfsFileInfo, error) |
19
|
|
|
Delete(path string) error |
20
|
|
|
Read(path string) (*io.Reader, error) |
21
|
|
|
Write(path string, append bool) (*io.Writer, error) |
22
|
|
|
Status(path string) (HdfsFileInfo, error) |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// CreateHDFSFileSystemClient wraps an hdfs.Client in order to produce the FileSystemClient interface. |
26
|
|
|
func CreateHDFSFileSystemClient(name string, client *hdfs.Client) FileSystemClient { |
27
|
|
|
return &hdfsFileSystemClient{name, client} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
type hdfsFileSystemClient struct { |
31
|
|
|
name string |
32
|
|
|
client *hdfs.Client |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
func (f *hdfsFileSystemClient) Close() error { |
36
|
|
|
return f.client.Close() |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
func (f *hdfsFileSystemClient) Name() string { |
40
|
|
|
return f.name |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
func (f *hdfsFileSystemClient) Upload(source, destination string) error { |
44
|
|
|
return f.client.CopyToRemote(source, destination) |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
func (f *hdfsFileSystemClient) Download(source, destination string) error { |
48
|
|
|
return f.client.CopyToLocal(source, destination) |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
func (f *hdfsFileSystemClient) Move(oldpath, newpath string) error { |
52
|
|
|
return f.client.Rename(oldpath, newpath) |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
func (f *hdfsFileSystemClient) MakeDir(path string, permission os.FileMode) error { |
56
|
|
|
return f.client.MkdirAll(path, permission) |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
func (f *hdfsFileSystemClient) List(path string) ([]HdfsFileInfo, error) { |
60
|
|
|
files, err := f.client.ReadDir(path) |
61
|
|
|
if err == nil { |
62
|
|
|
hdfsFiles := make([]HdfsFileInfo, len(files)) |
63
|
|
|
|
64
|
|
|
for idx, value := range files { |
65
|
|
|
hdfsFiles[idx] = &hdfsFileInfo{value} |
66
|
|
|
} |
67
|
|
|
return hdfsFiles, nil |
68
|
|
|
} |
69
|
|
|
return nil, err |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
func (f *hdfsFileSystemClient) Delete(path string) error { |
73
|
|
|
return f.client.Remove(path) |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
func (f *hdfsFileSystemClient) Read(path string) (*io.Reader, error) { |
77
|
|
|
return nil, nil |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
func (f *hdfsFileSystemClient) Write(path string, append bool) (*io.Writer, error) { |
81
|
|
|
if !append { |
82
|
|
|
f.Delete(path) |
83
|
|
|
} |
84
|
|
|
return nil, nil |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
func (f *hdfsFileSystemClient) Status(path string) (HdfsFileInfo, error) { |
88
|
|
|
file, err := f.client.Stat(path) |
89
|
|
|
return &hdfsFileInfo{file}, err |
90
|
|
|
} |
91
|
|
|
|