webhdfs 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -9,6 +9,10 @@ Rake::TestTask.new(:test) do |test|
9
9
  test.verbose = true
10
10
  end
11
11
 
12
+ task :doc do |t|
13
+ `bundle exec rdoc --markup=tomdoc --visibility=public --include=lib --exclude=test`
14
+ end
15
+
12
16
  task :coverage do |t|
13
17
  ENV['SIMPLE_COV'] = '1'
14
18
  Rake::Task["test"].invoke
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1
1
+ 0.2
@@ -5,13 +5,120 @@ module WebHDFS
5
5
  # This hash table holds command options.
6
6
  OPT_TABLE = {} # internal use only
7
7
 
8
+ # Those values hold NameNode location
8
9
  @fu_host = 'localhost'
9
10
  @fu_port = 50070
10
- def set_host(host, port)
11
+
12
+ # Public: Set hostname and port number of WebHDFS
13
+ #
14
+ # host - hostname
15
+ # port - port
16
+ #
17
+ # Examples
18
+ #
19
+ # FileUtils.set_server 'localhost', 50070
20
+ #
21
+ def set_server(host, port)
11
22
  @fu_host = host
12
23
  @fu_port = port
13
24
  end
25
+ module_function :set_server
26
+
27
+ # Public: Copy local file into HDFS
28
+ #
29
+ # file - local file path
30
+ # path - HDFS file path
31
+ # options - :overwrite, :blocksize, :replication, :mode, :buffersize, :verbose
32
+ #
33
+ # Examples
34
+ #
35
+ # FileUtils.copy_from_local 'local_file', 'remote_file'
36
+ #
37
+ def copy_from_local(file, path, options={})
38
+ fu_check_options options, OPT_TABLE['copy_from_local']
39
+ fu_log "copy_from_local local=#{file} hdfs=#{path}" if options[:verbose]
40
+ if mode = options[:mode]
41
+ mode = ('0%03o' % mode) if mode.is_a? Integer
42
+ else
43
+ mode = '0644'
44
+ end
45
+ options[:permission] = mode
46
+ options[:overwrite] ||= true
47
+ begin
48
+ fu_put(path, 'CREATE', options)
49
+ rescue RestClient::TemporaryRedirect => e
50
+ # must be redirected
51
+ raise e unless [301, 302, 307].include? e.response.code
52
+ # must have location
53
+ location = e.response.headers[:location]
54
+ raise e if location.nil? or location.empty?
55
+ # put contents
56
+ RestClient.put location, File.new(file, 'rb')
57
+ end
58
+ end
59
+ OPT_TABLE['copy_from_local'] = [:overwrite, :blocksize, :replication, :mode, :buffersize, :verbose]
60
+ module_function :copy_from_local
61
+
62
+ # Public: Copy remote HDFS file into local
63
+ #
64
+ # path - HDFS file path
65
+ # file - local file path
66
+ # options - :offset, :length, :buffersize, :verbose
67
+ #
68
+ # Examples
69
+ #
70
+ # FileUtils.copy_from_local 'remote_file', 'local_file'
71
+ #
72
+ def copy_to_local(path, file, options={})
73
+ fu_check_options options, OPT_TABLE['copy_to_local']
74
+ fu_log "copy_to_local hdfs=#{path} local=#{file}" if options[:verbose]
75
+ File.open(file, "wb") do |f|
76
+ ret = fu_get(path, 'OPEN', options)
77
+ f.write ret
78
+ end
79
+ end
80
+ OPT_TABLE['copy_to_local'] = [:offset, :length, :buffersize, :verbose]
81
+ module_function :copy_to_local
14
82
 
83
+ # Public: Append to HDFS file
84
+ #
85
+ # path - HDFS file path
86
+ # body - contents
87
+ # options - :buffersize, :verbose
88
+ #
89
+ # Examples
90
+ #
91
+ # FileUtils.copy_from_local 'local_file', 'remote_file'
92
+ #
93
+ def append(path, body, options={})
94
+ fu_check_options options, OPT_TABLE['append']
95
+ fu_log "append #{body.bytesize} bytes to #{path}" if options[:verbose]
96
+ begin
97
+ fu_post(path, 'APPEND', options)
98
+ rescue RestClient::TemporaryRedirect => e
99
+ # must be redirected
100
+ raise e unless [301, 302, 307].include? e.response.code
101
+ # must have location
102
+ location = e.response.headers[:location]
103
+ raise e if location.nil? or location.empty?
104
+ # put contents
105
+ RestClient.post location, body
106
+ end
107
+ end
108
+ OPT_TABLE['append'] = [:buffersize, :verbose]
109
+ module_function :append
110
+
111
+ # Public: Create one or more directories.
112
+ #
113
+ # list - directory name, or list of them
114
+ # options - :mode, :verbose
115
+ #
116
+ # Examples
117
+ #
118
+ # FileUtils.mkdir 'test'
119
+ # FileUtils.mkdir %w( tmp data )
120
+ # FileUtils.mkdir 'tmp', :mode => 0700
121
+ #
15
122
  def mkdir(list, options={})
16
123
  fu_check_options options, OPT_TABLE['mkdir']
17
124
  list = fu_list(list)
@@ -28,9 +135,31 @@ module WebHDFS
28
135
  OPT_TABLE['mkdir'] = [:mode, :verbose]
29
136
  module_function :mkdir
30
137
 
138
+ # Public: Create one or more directories recursively.
139
+ #
140
+ # list - directory name, or list of them
141
+ # options - :mode, :verbose
142
+ #
143
+ # Examples
144
+ #
145
+ # FileUtils.mkdir_p 'dir/subdir'
146
+ # FileUtils.mkdir_p %w( tmp data )
147
+ # FileUtils.mkdir_p 'dir/subdir', :mode => 0700
148
+ #
31
149
  alias mkdir_p mkdir
32
150
  module_function :mkdir_p
33
151
 
152
+ # Public: Remove one or more directories or files.
153
+ #
154
+ # list - directory name, or list of them
155
+ # options - :recursive, :verbose
156
+ #
157
+ # Examples
158
+ #
159
+ # FileUtils.rm 'dir'
160
+ # FileUtils.rm %w( tmp data )
161
+ # FileUtils.rm 'dir', :recursive => true
162
+ #
34
163
  def rm(list, options={})
35
164
  fu_check_options options, OPT_TABLE['rm']
36
165
  list = fu_list(list)
@@ -42,13 +171,34 @@ module WebHDFS
42
171
  OPT_TABLE['rm'] = [:verbose, :recursive]
43
172
  module_function :rm
44
173
 
174
+ # Public: Remove one or more directories/files recursively.
175
+ #
176
+ # list - directory name, or list of them
177
+ # options - :verbose
178
+ #
179
+ # Examples
180
+ #
181
+ # FileUtils.rmr 'dir'
182
+ # FileUtils.rmr %w( tmp data )
183
+ # FileUtils.rmr 'dir'
184
+ #
45
185
  def rmr(list, options={})
46
186
  fu_check_options options, OPT_TABLE['rmr']
47
187
  self.rm(list, options.merge({:recursive => true}))
48
188
  end
49
- OPT_TABLE['rmr'] = [:verbose, :recursive]
189
+ OPT_TABLE['rmr'] = [:verbose]
50
190
  module_function :rmr
51
191
 
192
+ # Public: Rename a file or directory.
193
+ #
194
+ # src - from
195
+ # dst - to
196
+ # options - :verbose
197
+ #
198
+ # Examples
199
+ #
200
+ # FileUtils.rename 'from', 'to'
201
+ #
52
202
  def rename(src, dst, options={})
53
203
  fu_check_options options, OPT_TABLE['rename']
54
204
  fu_log "rename #{src} #{dst}" if options[:verbose]
@@ -57,6 +207,17 @@ module WebHDFS
57
207
  OPT_TABLE['rename'] = [:verbose]
58
208
  module_function :rename
59
209
 
210
+ # Public: Change permission of one or more directories/files.
211
+ #
212
+ # mode - permission
213
+ # list - file/directory name or list of them.
214
+ # options - :verbose
215
+ #
216
+ # Examples
217
+ #
218
+ # FileUtils.chmod 0755, 'dir'
219
+ # FileUtils.chmod 0644, 'file'
220
+ #
60
221
  def chmod(mode, list, options={})
61
222
  fu_check_options options, OPT_TABLE['chmod']
62
223
  list = fu_list(list)
@@ -69,6 +230,18 @@ module WebHDFS
69
230
  OPT_TABLE['chmod'] = [:verbose]
70
231
  module_function :chmod
71
232
 
233
+ # Public: Change an ownership of one or more directories/files.
234
+ #
235
+ # user - username
236
+ # group - groupname
237
+ # list - file/directory name or list of them
238
+ # options - :verbose
239
+ #
240
+ # Examples
241
+ #
242
+ # FileUtils.chmod 0755, 'dir'
243
+ # FileUtils.chmod 0644, 'file'
244
+ #
72
245
  def chown(user, group, list, options={})
73
246
  fu_check_options options, OPT_TABLE['chown']
74
247
  list = fu_list(list)
@@ -82,6 +255,16 @@ module WebHDFS
82
255
  OPT_TABLE['chown'] = [:verbose]
83
256
  module_function :chown
84
257
 
258
+ # Public: Set a replication factor of files
259
+ #
260
+ # list - file/directory name or list of them
261
+ # num - replication factor
262
+ # options - :verbose
263
+ #
264
+ # Examples
265
+ #
266
+ # FileUtils.set_repl_factor 'file', 3
267
+ #
85
268
  def set_repl_factor(list, num, options={})
86
269
  fu_check_options options, OPT_TABLE['set_repl_factor']
87
270
  list = fu_list(list)
@@ -94,6 +277,16 @@ module WebHDFS
94
277
  OPT_TABLE['set_repl_factor'] = [:verbose]
95
278
  module_function :set_repl_factor
96
279
 
280
+ # Public: Set an access time of files
281
+ #
282
+ # list - file/directory name or list of them
283
+ # time - new access time
284
+ # options - :verbose
285
+ #
286
+ # Examples
287
+ #
288
+ # FileUtils.set_atime 'file', Time.now
289
+ #
97
290
  def set_atime(list, time, options={})
98
291
  fu_check_options options, OPT_TABLE['set_atime']
99
292
  list = fu_list(list)
@@ -101,11 +294,21 @@ module WebHDFS
101
294
  fu_log sprintf('set_atime %s %d', list.join(' '), time) if options[:verbose]
102
295
  list.each { |dir|
103
296
  fu_put(dir, 'SETTIMES', {:accesstime => time})
104
- }
297
+ }
105
298
  end
106
299
  OPT_TABLE['set_atime'] = [:verbose]
107
300
  module_function :set_atime
108
301
 
302
+ # Public: Set a modification time of files
303
+ #
304
+ # list - file/directory name or list of them
305
+ # time - new modification time
306
+ # options - :verbose
307
+ #
308
+ # Examples
309
+ #
310
+ # FileUtils.set_mtime 'file', Time.now
311
+ #
109
312
  def set_mtime(list, time, options={})
110
313
  fu_check_options options, OPT_TABLE['set_mtime']
111
314
  list = fu_list(list)
@@ -113,34 +316,52 @@ module WebHDFS
113
316
  fu_log sprintf('set_mtime %s %d', list.join(' '), time) if options[:verbose]
114
317
  list.each { |dir|
115
318
  fu_put(dir, 'SETTIMES', {:modificationtime => time})
116
- }
319
+ }
117
320
  end
118
321
  OPT_TABLE['set_mtime'] = [:verbose]
119
322
  module_function :set_mtime
120
323
 
121
- ##
324
+ # Internal: make functin private
122
325
  def self.private_module_function(name)
123
326
  module_function name
124
327
  private_class_method name
125
328
  end
126
329
 
330
+ # Internal: make list
127
331
  def fu_list(arg)
128
332
  [arg].flatten
129
333
  end
130
334
  private_module_function :fu_list
131
335
 
336
+ # Internal: HTTP GET
337
+ def fu_get(path, op, params={}, payload='')
338
+ url = "http://#{@fu_host}:#{@fu_port}/webhdfs/v1/#{path}"
339
+ RestClient.get url, :params => params.merge({:op => op})
340
+ end
341
+ private_module_function :fu_get
342
+
343
+ # Internal: HTTP PUT
132
344
  def fu_put(path, op, params={}, payload='')
133
345
  url = "http://#{@fu_host}:#{@fu_port}/webhdfs/v1/#{path}"
134
346
  RestClient.put url, payload, :params => params.merge({:op => op})
135
347
  end
136
348
  private_module_function :fu_put
137
349
 
350
+ # Internal: HTTP POST
351
+ def fu_post(path, op, params={}, payload='')
352
+ url = "http://#{@fu_host}:#{@fu_port}/webhdfs/v1/#{path}"
353
+ RestClient.post url, payload, :params => params.merge({:op => op})
354
+ end
355
+ private_module_function :fu_post
356
+
357
+ # Internal: HTTP DELETE
138
358
  def fu_delete(path, op, params={})
139
359
  url = "http://#{@fu_host}:#{@fu_port}/webhdfs/v1/#{path}"
140
360
  RestClient.delete url, :params => params.merge({:op => op})
141
361
  end
142
362
  private_module_function :fu_delete
143
363
 
364
+ # Internal: Check options Hash
144
365
  def fu_check_options(options, optdecl)
145
366
  h = options.dup
146
367
  optdecl.each do |opt|
@@ -152,6 +373,7 @@ module WebHDFS
152
373
 
153
374
  @fileutils_output = $stderr
154
375
  @fileutils_label = '[webhdfs]: '
376
+ # Internal: Logging
155
377
  def fu_log(msg)
156
378
  @fileutils_output ||= $stderr
157
379
  @fileutils_label ||= ''
@@ -5,6 +5,13 @@ class FileUtilsTest < Test::Unit::TestCase
5
5
  require 'webhdfs'
6
6
  end
7
7
 
8
+ def test_copy_from_local
9
+ WebHDFS::FileUtils.copy_from_local('VERSION', 'VERSION', :verbose => true)
10
+ WebHDFS::FileUtils.copy_to_local('VERSION', 'VERSION2', :verbose => true)
11
+ WebHDFS::FileUtils.append('VERSION', 'foo-bar-buzz', :verbose => true)
12
+ WebHDFS::FileUtils.rm('VERSION', :verbose => true)
13
+ end
14
+
8
15
  def test_rm
9
16
  WebHDFS::FileUtils.mkdir('foo', :mode => 0777, :verbose => true)
10
17
  WebHDFS::FileUtils.rm('foo', :verbose => true)
data/webhdfs.gemspec CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |gem|
18
18
 
19
19
  gem.add_dependency "rest-client", "~> 1.6.7"
20
20
  gem.add_development_dependency "rake", ">= 0.9.2"
21
+ gem.add_development_dependency "rdoc", ">= 3.12"
21
22
  gem.add_development_dependency "simplecov", ">= 0.5.4"
22
23
  gem.add_development_dependency "rr", ">= 1.0.0"
23
24
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webhdfs
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-11 00:00:00.000000000Z
12
+ date: 2012-03-14 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
16
- requirement: &2154610720 !ruby/object:Gem::Requirement
16
+ requirement: &2157003340 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.6.7
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2154610720
24
+ version_requirements: *2157003340
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &2154610240 !ruby/object:Gem::Requirement
27
+ requirement: &2157002860 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,21 @@ dependencies:
32
32
  version: 0.9.2
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2154610240
35
+ version_requirements: *2157002860
36
+ - !ruby/object:Gem::Dependency
37
+ name: rdoc
38
+ requirement: &2157002400 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '3.12'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2157002400
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: simplecov
38
- requirement: &2154609780 !ruby/object:Gem::Requirement
49
+ requirement: &2157001940 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ! '>='
@@ -43,10 +54,10 @@ dependencies:
43
54
  version: 0.5.4
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *2154609780
57
+ version_requirements: *2157001940
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: rr
49
- requirement: &2154609320 !ruby/object:Gem::Requirement
60
+ requirement: &2157001480 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ! '>='
@@ -54,7 +65,7 @@ dependencies:
54
65
  version: 1.0.0
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *2154609320
68
+ version_requirements: *2157001480
58
69
  description: Ruby WebHDFS client
59
70
  email: kazuki.ohta@gmail.com
60
71
  executables: []