Module: WebHDFS::FileUtils
- Defined in:
- lib/webhdfs/fileutils.rb
Class Method Summary (collapse)
-
+ (Object) append(path, body, options = {})
Public: Append to HDFS file.
-
+ (Object) chmod(mode, list, options = {})
Public: Change permission of one or more directories/files.
-
+ (Object) chown(user, group, list, options = {})
Public: Change an ownership of one or more directories/files.
-
+ (Object) copy_from_local(file, path, options = {})
Public: Copy local file into HDFS.
-
+ (Object) copy_to_local(path, file, options = {})
Public: Copy remote HDFS file into local.
-
+ (Object) mkdir(list, options = {})
Public: Create one or more directories.
-
+ (Object) mkdir_p
Public: Create one or more directories.
-
+ (Object) private_module_function(name)
Internal: make functin private.
-
+ (Object) rename(src, dst, options = {})
Public: Rename a file or directory.
-
+ (Object) rm(list, options = {})
Public: Remove one or more directories or files.
-
+ (Object) rmr(list, options = {})
Public: Remove one or more directories/files recursively.
-
+ (Object) set_atime(list, time, options = {})
Public: Set an access time of files.
-
+ (Object) set_httpfs_mode(mode = true)
Public: Set httpfs mode enable/disable.
-
+ (Object) set_mtime(list, time, options = {})
Public: Set a modification time of files.
-
+ (Object) set_repl_factor(list, num, options = {})
Public: Set a replication factor of files.
-
+ (Object) set_server(host, port, user = nil, doas = nil)
Public: Set hostname and port number of WebHDFS.
Instance Method Summary (collapse)
-
- (Object) client
Internal.
-
- (Object) fu_log(msg)
Internal: Logging.
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'
98 99 100 101 102 |
# File 'lib/webhdfs/fileutils.rb', line 98 def append(path, body, ={}) opts = .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'
212 213 214 215 216 217 218 219 220 221 |
# File 'lib/webhdfs/fileutils.rb', line 212 def chmod(mode, list, ={}) opts = .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'
236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/webhdfs/fileutils.rb', line 236 def chown(user, group, list, ={}) opts = .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'
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/webhdfs/fileutils.rb', line 54 def copy_from_local(file, path, ={}) opts = .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_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'
79 80 81 82 83 84 85 |
# File 'lib/webhdfs/fileutils.rb', line 79 def copy_to_local(path, file, ={}) opts = .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
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/webhdfs/fileutils.rb', line 116 def mkdir(list, ={}) opts = .dup list = [list].flatten fu_log "mkdir #{[:mode] ? ('-m %03o ' % [: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
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/webhdfs/fileutils.rb', line 143 def mkdir(list, ={}) opts = .dup list = [list].flatten fu_log "mkdir #{[:mode] ? ('-m %03o ' % [: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
316 317 318 319 |
# File 'lib/webhdfs/fileutils.rb', line 316 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'
194 195 196 197 198 |
# File 'lib/webhdfs/fileutils.rb', line 194 def rename(src, dst, ={}) opts = .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
157 158 159 160 161 162 163 164 165 |
# File 'lib/webhdfs/fileutils.rb', line 157 def rm(list, ={}) opts = .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'
179 180 181 |
# File 'lib/webhdfs/fileutils.rb', line 179 def rmr(list, ={}) self.rm(list, .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
281 282 283 284 285 286 287 288 289 290 |
# File 'lib/webhdfs/fileutils.rb', line 281 def set_atime(list, time, ={}) opts = .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
39 40 41 |
# File 'lib/webhdfs/fileutils.rb', line 39 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
303 304 305 306 307 308 309 310 311 312 |
# File 'lib/webhdfs/fileutils.rb', line 303 def set_mtime(list, time, ={}) opts = .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
259 260 261 262 263 264 265 266 267 268 |
# File 'lib/webhdfs/fileutils.rb', line 259 def set_repl_factor(list, num, ={}) opts = .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)
Public: Set hostname and port number of WebHDFS
host - hostname port - port user - username doas - proxy user name
Examples
FileUtils.set_server 'localhost', 50070
23 24 25 26 27 28 |
# File 'lib/webhdfs/fileutils.rb', line 23 def set_server(host, port, user=nil, doas=nil) @fu_host = host @fu_port = port @fu_user = user @fu_doas = doas end |
Instance Method Details
- (Object) client
Internal
332 333 334 335 336 337 338 |
# File 'lib/webhdfs/fileutils.rb', line 332 def client client = WebHDFS::Client.new(@fu_host, @fu_port, @fu_user, @fu_doas) if @fu_httpfs_mode client.httpfs_mode = true end client end |
- (Object) fu_log(msg)
Internal: Logging
324 325 326 327 328 |
# File 'lib/webhdfs/fileutils.rb', line 324 def fu_log(msg) @fileutils_output ||= $stderr @fileutils_label ||= '' @fileutils_output.puts @fileutils_label + msg end |