Module: WebHDFS::FileUtils

Defined in:
lib/webhdfs/fileutils.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) append(path, body, options = {})

Public: Append to HDFS file

path - HDFS file path body - contents options - :buffersize, :verbose

Examples

FileUtils.append 'remote_path', 'contents'


127
128
129
130
131
# File 'lib/webhdfs/fileutils.rb', line 127

def append(path, body, options={})
  opts = options.dup
  fu_log "append #{body.bytesize} bytes to #{path}" if opts.delete(:verbose)
  client.append(path, body, opts)
end

+ (Object) chmod(mode, list, options = {})

Public: Change permission of one or more directories/files.

mode - permission list - file/directory name or list of them. options - :verbose

Examples

FileUtils.chmod 0755, 'dir'
FileUtils.chmod 0644, 'file'


241
242
243
244
245
246
247
248
249
250
# File 'lib/webhdfs/fileutils.rb', line 241

def chmod(mode, list, options={})
  opts = options.dup
  list = [list].flatten
  fu_log sprintf('chmod %o %s', mode, list.join(' ')) if opts.delete(:verbose)
  mode = ('%03o' % mode) if mode.is_a? Integer
  c = client
  list.each { |entry|
    c.chmod(entry, mode, opts)
  }
end

+ (Object) chown(user, group, list, options = {})

Public: Change an ownership of one or more directories/files.

user - username group - groupname list - file/directory name or list of them options - :verbose

Examples

FileUtils.chmod 0755, 'dir'
FileUtils.chmod 0644, 'file'


265
266
267
268
269
270
271
272
273
274
275
# File 'lib/webhdfs/fileutils.rb', line 265

def chown(user, group, list, options={})
  opts = options.dup
  list = [list].flatten
  fu_log sprintf('chown %s%s',
                 [user,group].compact.join(':') + ' ',
                 list.join(' ')) if opts.delete(:verbose)
  c = client
  list.each { |entry|
    c.chown(entry, {:owner => user, :group => group})
  }
end

+ (Object) copy_from_local(file, path, options = {})

Public: Copy local file into HDFS

file - local file path path - HDFS file path options - :overwrite, :blocksize, :replication, :mode, :buffersize, :verbose

Examples

FileUtils.copy_from_local 'local_file', 'remote_file'


58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/webhdfs/fileutils.rb', line 58

def copy_from_local(file, path, options={})
  opts = options.dup
  fu_log "copy_from_local local=#{file} hdfs=#{path}" if opts.delete(:verbose)
  if mode = opts.delete(:mode)
    mode = ('%03o' % mode) if mode.is_a? Integer
  else
    mode = '644'
  end
  opts[:permission] = mode
  opts[:overwrite] ||= true

  client.create(path, File.new(file, 'rb').read(File.size(file)), opts)
end

+ (Object) copy_from_local_via_stream(file, path, options = {})

Public: Copy local file into HDFS with IOStream

file - local file IO handle path - HDFS file path options - :overwrite, :blocksize, :replication, :mode, :buffersize, :verbose

Examples

FileUtils.copy_from_local_via_stream 'local_file_IO_handle', 'remote_file'


83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/webhdfs/fileutils.rb', line 83

def copy_from_local_via_stream(file, path, options={})
  opts = options.dup
  fu_log "copy_from_local_via_stream local=#{file} hdfs=#{path}" if opts.delete(:verbose)
  if mode = opts.delete(:mode)
    mode = ('%03o' % mode) if mode.is_a? Integer
  else
    mode = '644'
  end
  opts[:permission] = mode
  opts[:overwrite] ||= true

  client.create(path, File.new(file, 'rb'), opts)
end

+ (Object) copy_to_local(path, file, options = {})

Public: Copy remote HDFS file into local

path - HDFS file path file - local file path options - :offset, :length, :buffersize, :verbose

Examples

FileUtils.copy_to_local 'remote_file', 'local_file'


108
109
110
111
112
113
114
# File 'lib/webhdfs/fileutils.rb', line 108

def copy_to_local(path, file, options={})
  opts = options.dup
  fu_log "copy_to_local hdfs=#{path} local=#{file}" if opts.delete(:verbose)
  File.open(file, "wb") do |f|
    f.write client.read(path, opts)
  end
end

+ (Object) mkdir(list, options = {})

Public: Create one or more directories.

list - directory name, or list of them options - :mode, :verbose

Examples

FileUtils.mkdir 'test'
FileUtils.mkdir %w( tmp data )
FileUtils.mkdir 'tmp', :mode => 0700


145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/webhdfs/fileutils.rb', line 145

def mkdir(list, options={})
  opts = options.dup
  list = [list].flatten
  fu_log "mkdir #{options[:mode] ? ('-m %03o ' % options[:mode]) : ''}#{list.join ' '}" if opts.delete(:verbose)
  if mode = opts[:mode]
    mode = ('0%03o' % mode) if mode.is_a? Integer
  else
    mode = '0755'
  end
  c = client
  list.each { |dir|
    c.mkdir(dir, {:permission => mode})
  }
end

+ (Object) mkdir_p

Public: Create one or more directories.

list - directory name, or list of them options - :mode, :verbose

Examples

FileUtils.mkdir 'test'
FileUtils.mkdir %w( tmp data )
FileUtils.mkdir 'tmp', :mode => 0700

Public: Create one or more directories recursively.

list - directory name, or list of them options - :mode, :verbose

Examples

FileUtils.mkdir_p 'dir/subdir'
FileUtils.mkdir_p %w( tmp data )
FileUtils.mkdir_p 'dir/subdir', :mode => 0700


172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/webhdfs/fileutils.rb', line 172

def mkdir(list, options={})
  opts = options.dup
  list = [list].flatten
  fu_log "mkdir #{options[:mode] ? ('-m %03o ' % options[:mode]) : ''}#{list.join ' '}" if opts.delete(:verbose)
  if mode = opts[:mode]
    mode = ('0%03o' % mode) if mode.is_a? Integer
  else
    mode = '0755'
  end
  c = client
  list.each { |dir|
    c.mkdir(dir, {:permission => mode})
  }
end

+ (Object) private_module_function(name)

Internal: make functin private



345
346
347
348
# File 'lib/webhdfs/fileutils.rb', line 345

def self.private_module_function(name)
  module_function name
  private_class_method name
end

+ (Object) rename(src, dst, options = {})

Public: Rename a file or directory.

src - from dst - to options - :verbose

Examples

FileUtils.rename 'from', 'to'


223
224
225
226
227
# File 'lib/webhdfs/fileutils.rb', line 223

def rename(src, dst, options={})
  opts = options.dup
  fu_log "rename #{src} #{dst}" if opts.delete(:verbose)
  client.rename(src, dst, opts)
end

+ (Object) rm(list, options = {})

Public: Remove one or more directories or files.

list - directory name, or list of them options - :recursive, :verbose

Examples

FileUtils.rm 'dir'
FileUtils.rm %w( tmp data )
FileUtils.rm 'dir', :recursive => true


186
187
188
189
190
191
192
193
194
# File 'lib/webhdfs/fileutils.rb', line 186

def rm(list, options={})
  opts = options.dup
  list = [list].flatten
  fu_log "rm #{list.join ' '}" if opts.delete(:verbose)
  c = client
  list.each { |dir|
    c.delete(dir, {:recursive => opts[:recursive] || false})
  }
end

+ (Object) rmr(list, options = {})

Public: Remove one or more directories/files recursively.

list - directory name, or list of them options - :verbose

Examples

FileUtils.rmr 'dir'
FileUtils.rmr %w( tmp data )
FileUtils.rmr 'dir'


208
209
210
# File 'lib/webhdfs/fileutils.rb', line 208

def rmr(list, options={})
  self.rm(list, options.merge({:recursive => true}))
end

+ (Object) set_atime(list, time, options = {})

Public: Set an access time of files

list - file/directory name or list of them time - new access time options - :verbose

Examples

FileUtils.set_atime 'file', Time.now


310
311
312
313
314
315
316
317
318
319
# File 'lib/webhdfs/fileutils.rb', line 310

def set_atime(list, time, options={})
  opts = options.dup
  list = [list].flatten
  time = time.to_i
  fu_log sprintf('set_atime %s %d', list.join(' '), time) if opts.delete(:verbose)
  c = client
  list.each { |entry|
    c.touch(entry, {:accesstime => time})
  }
end

+ (Object) set_httpfs_mode(mode = true)

Public: Set httpfs mode enable/disable

mode - boolean (default true)

Examples

FileUtils.set_httpfs_mode


43
44
45
# File 'lib/webhdfs/fileutils.rb', line 43

def set_httpfs_mode(mode=true)
  @fu_httpfs_mode = mode
end

+ (Object) set_mtime(list, time, options = {})

Public: Set a modification time of files

list - file/directory name or list of them time - new modification time options - :verbose

Examples

FileUtils.set_mtime 'file', Time.now


332
333
334
335
336
337
338
339
340
341
# File 'lib/webhdfs/fileutils.rb', line 332

def set_mtime(list, time, options={})
  opts = options.dup
  list = [list].flatten
  time = time.to_i
  fu_log sprintf('set_mtime %s %d', list.join(' '), time) if opts.delete(:verbose)
  c = client
  list.each { |entry|
    c.touch(entry, {:modificationtime => time})
  }
end

+ (Object) set_repl_factor(list, num, options = {})

Public: Set a replication factor of files

list - file/directory name or list of them num - replication factor options - :verbose

Examples

FileUtils.set_repl_factor 'file', 3


288
289
290
291
292
293
294
295
296
297
# File 'lib/webhdfs/fileutils.rb', line 288

def set_repl_factor(list, num, options={})
  opts = options.dup
  list = [list].flatten
  fu_log sprintf('set_repl_factor %s %d',
                 list.join(' '), num) if opts.delete(:verbose)
  c = client
  list.each { |entry|
    c.replication(entry, num, opts)
  }
end

+ (Object) set_server(host, port, user = nil, doas = nil, proxy_address = nil, proxy_port = nil)

Public: Set hostname and port number of WebHDFS

host - hostname port - port user - username doas - proxy user name proxy_address - address of the net http proxy to use proxy_port - port of the net http proxy to use

Examples

FileUtils.set_server 'localhost', 50070


25
26
27
28
29
30
31
32
# File 'lib/webhdfs/fileutils.rb', line 25

def set_server(host, port, user=nil, doas=nil, proxy_address=nil, proxy_port=nil)
  @fu_host = host
  @fu_port = port
  @fu_user = user
  @fu_doas = doas
  @fu_paddr = proxy_address
  @fu_pport = proxy_port
end

Instance Method Details

- (Object) client

Internal



361
362
363
364
365
366
367
# File 'lib/webhdfs/fileutils.rb', line 361

def client
  client = WebHDFS::Client.new(@fu_host, @fu_port, @fu_user, @fu_doas, @fu_paddr, @fu_pport)
  if @fu_httpfs_mode
    client.httpfs_mode = true
  end
  client
end

- (Object) fu_log(msg)

Internal: Logging



353
354
355
356
357
# File 'lib/webhdfs/fileutils.rb', line 353

def fu_log(msg)
  @fileutils_output ||= $stderr
  @fileutils_label  ||= ''
  @fileutils_output.puts @fileutils_label + msg
end