Class: Swineherd::HadoopFileSystem::HadoopFile

Inherits:
Object
  • Object
show all
Defined in:
lib/swineherd/filesystem/hadoopfilesystem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, mode, fs, &blk) ⇒ HadoopFile

In order to open input and output streams we must pass around the hadoop fs object itself



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/swineherd/filesystem/hadoopfilesystem.rb', line 142

def initialize path, mode, fs, &blk
  @fs   = fs
  @path = Path.new(path)
  case mode
  when "r" then
    raise "#{@fs.type(path)} is not a readable file - #{path}" unless @fs.type(path) == "file"
    @handle = @fs.hdfs.open(@path).to_io(&blk)
  when "w" then
    # Open path for writing
    raise "Path #{path} is a directory." unless (@fs.type(path) == "file") || (@fs.type(path) == "unknown")
    @handle = @fs.hdfs.create(@path).to_io.to_outputstream
    if block_given?
      yield self
      self.close # muy muy importante
    end
  end
end

Instance Attribute Details

#handleObject

Returns the value of attribute handle.



137
138
139
# File 'lib/swineherd/filesystem/hadoopfilesystem.rb', line 137

def handle
  @handle
end

#hdfsObject

Returns the value of attribute hdfs.



137
138
139
# File 'lib/swineherd/filesystem/hadoopfilesystem.rb', line 137

def hdfs
  @hdfs
end

#pathObject

Returns the value of attribute path.



137
138
139
# File 'lib/swineherd/filesystem/hadoopfilesystem.rb', line 137

def path
  @path
end

Instance Method Details

#closeObject



176
177
178
# File 'lib/swineherd/filesystem/hadoopfilesystem.rb', line 176

def close
  @handle.close
end

#puts(string) ⇒ Object



172
173
174
# File 'lib/swineherd/filesystem/hadoopfilesystem.rb', line 172

def puts string
  write(string+"\n")
end

#readObject



160
161
162
# File 'lib/swineherd/filesystem/hadoopfilesystem.rb', line 160

def read
  @handle.read
end

#readlineObject



164
165
166
# File 'lib/swineherd/filesystem/hadoopfilesystem.rb', line 164

def readline
  @handle.readline
end

#write(string) ⇒ Object



168
169
170
# File 'lib/swineherd/filesystem/hadoopfilesystem.rb', line 168

def write string
  @handle.write(string.to_java_string.get_bytes)
end