forked from vladimirvivien/gowfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsio.go
215 lines (183 loc) · 5.59 KB
/
fsio.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package webhdfs
import "fmt"
import "os"
import "io"
import "strconv"
import "strings"
import "net/url"
import "net/http"
// Creates a new file and stores its content in HDFS.
// See HDFS FileSystem.create()
// For detail, http://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-hdfs/WebHDFS.html#Create_and_Write_to_a_File
// See NOTE section on that page for impl detail.
func (fs *FileSystem) Create(
data io.Reader,
p Path,
overwrite bool,
blocksize uint64,
replication uint16,
permission os.FileMode,
buffersize uint) (bool, error) {
params := map[string]string{"op": OP_CREATE}
params["overwrite"] = strconv.FormatBool(overwrite)
if blocksize == 0 {
params["blocksize"] = "134217728" // from hdfs-default.xml (ver 2)
} else {
params["blocksize"] = strconv.FormatInt(int64(blocksize), 10)
}
if replication == 0 {
params["replication"] = "3"
} else {
params["replication"] = strconv.FormatInt(int64(replication), 10)
}
if permission <= 0 || permission > 1777 {
params["permission"] = "0700"
} else {
params["permission"] = strconv.FormatInt(int64(permission), 8)
}
if buffersize == 0 {
params["buffersize"] = "4096"
} else {
params["buffersize"] = strconv.FormatInt(int64(buffersize), 10)
}
u, err := buildRequestUrl(fs.Config, &p, ¶ms)
if err != nil {
return false, err
}
// take over default transport to avoid redirect
tr := &http.Transport{}
req, _ := http.NewRequest("PUT", u.String(), nil)
rsp, err := tr.RoundTrip(req)
if err != nil {
return false, err
}
// extract returned url in header.
loc := rsp.Header.Get("Location")
u, err = url.ParseRequestURI(loc)
if err != nil {
return false, fmt.Errorf("FileSystem.Create(%s) - invalid redirect URL from server: %s", u, err.Error())
}
req, _ = http.NewRequest("PUT", u.String(), data)
req.Header.Add("Content-Type", "application/octet-stream")
rsp, err = fs.client.Do(req)
if err != nil {
fmt.Errorf("FileSystem.Create(%s) - bad url: %s", loc, err.Error())
return false, err
}
if rsp.StatusCode != http.StatusCreated {
defer rsp.Body.Close()
_, err = responseToHdfsData(rsp)
if err != nil {
return false, err
}
return false, fmt.Errorf("FileSystem.Create(%s) - File not created. Server returned status %v", loc, rsp.StatusCode)
}
return true, nil
}
//Opens the specificed Path and returns its content to be accessed locally.
//See HDFS WebHdfsFileSystem.open()
// See http://hadoop.apache.org/docs/r2.2.0/hadoop-project-dist/hadoop-hdfs/WebHDFS.html#HTTP_Query_Parameter_Dictionary
func (fs *FileSystem) Open(p Path, offset, length int64, buffSize int) (io.ReadCloser, error) {
params := map[string]string{"op": OP_OPEN}
if offset < 0 {
params["offset"] = "0"
} else {
params["offset"] = strconv.FormatInt(offset, 10)
}
if length > 0 {
params["length"] = strconv.FormatInt(length, 10)
}
if buffSize <= 0 {
params["buffersize"] = "4096"
} else {
params["buffersize"] = strconv.Itoa(buffSize)
}
u, err := buildRequestUrl(fs.Config, &p, ¶ms)
if err != nil {
return nil, err
}
req, _ := http.NewRequest("GET", u.String(), nil)
rsp, err := fs.client.Do(req)
if err != nil {
return nil, err
}
// possible error
if rsp.StatusCode != http.StatusOK {
defer rsp.Body.Close()
_, err = responseToHdfsData(rsp)
if err != nil {
return nil, err
}
}
return rsp.Body, nil
}
// Appends specified data to an existing file.
// See HDFS FileSystem.append()
// See http://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-hdfs/WebHDFS.html#Append_to_a_File
// NOTE: Append() is known to have issues - see https://issues.apache.org/jira/browse/HDFS-4600
func (fs *FileSystem) Append(data io.Reader, p Path, buffersize int) (bool, error) {
params := map[string]string{"op": OP_APPEND}
if buffersize == 0 {
params["buffersize"] = "4096"
} else {
params["buffersize"] = strconv.FormatInt(int64(buffersize), 10)
}
u, err := buildRequestUrl(fs.Config, &p, ¶ms)
if err != nil {
return false, err
}
// take over default transport to avoid redirect
tr := &http.Transport{}
req, _ := http.NewRequest("POST", u.String(), nil)
rsp, err := tr.RoundTrip(req)
if err != nil {
return false, err
}
// extract returned url in header.
loc := rsp.Header.Get("Location")
u, err = url.ParseRequestURI(loc)
if err != nil {
return false, fmt.Errorf("Append(%s) - did not receive a valid URL from server.", loc)
}
req, _ = http.NewRequest("POST", u.String(), data)
rsp, err = fs.client.Do(req)
if err != nil {
return false, err
}
if rsp.StatusCode != http.StatusOK {
defer rsp.Body.Close()
_, err = responseToHdfsData(rsp)
if err != nil {
return false, err
}
return false, fmt.Errorf("Append(%s) - File not created. Server returned status %v", loc, rsp.StatusCode)
}
return true, nil
}
// Concatenate (on the server) a list of given files paths to a new file.
// See HDFS FileSystem.concat()
func (fs *FileSystem) Concat(target Path, sources []string) (bool, error) {
if (target == Path{}) {
return false, fmt.Errorf("Concat() - The target path must be provided.")
}
params := map[string]string{"op": OP_CONCAT}
params["sources"] = strings.Join(sources, ",")
u, err := buildRequestUrl(fs.Config, &target, ¶ms)
if err != nil {
return false, err
}
req, _ := http.NewRequest("POST", u.String(), nil)
rsp, err := fs.client.Do(req)
if err != nil {
return false, err
}
if rsp.StatusCode != http.StatusOK {
defer rsp.Body.Close()
_, err = responseToHdfsData(rsp)
if err != nil {
return false, err
}
return false, fmt.Errorf("Concat(%s) - File not concatenated. Server returned status %v", u.String(), rsp.StatusCode)
}
return true, nil
}