Class: Impala::Cursor

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/impala/cursor.rb

Overview

Cursors are used to iterate over result sets without loading them all into memory at once. This can be useful if you’re dealing with lots of rows. It implements Enumerable, so you can use each/select/map/etc.

Instance Method Summary collapse

Constructor Details

#initialize(handle, service, buffer_length = 1024) ⇒ Cursor

Returns a new instance of Cursor.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/impala/cursor.rb', line 8

def initialize(handle, service, buffer_length=1024)
  @handle = handle
  @service = service
  @metadata = @service.(@handle)

  @buffer_length = buffer_length
  @row_buffer = []

  @done = false
  @open = true
end

Instance Method Details

#closeObject

Close the cursor on the remote server. Once a cursor is closed, you can no longer fetch any rows from it.



57
58
59
60
# File 'lib/impala/cursor.rb', line 57

def close
  @open = false
  @service.close(@handle)
end

#eachObject



24
25
26
27
28
# File 'lib/impala/cursor.rb', line 24

def each
  while row = fetch_row
    yield row
  end
end

#fetch_allArray<Hash>

Returns all the remaining rows in the result set.

Returns:

  • (Array<Hash>)

    the remaining rows in the result set

See Also:

  • #fetch_one


51
52
53
# File 'lib/impala/cursor.rb', line 51

def fetch_all
  self.to_a
end

#fetch_rowHash?

Returns the next available row as a hash, or nil if there are none left.

Returns:

  • (Hash, nil)

    the next available row, or nil if there are none left

Raises:

See Also:



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/impala/cursor.rb', line 34

def fetch_row
  raise CursorError.new("Cursor has expired or been closed") unless @open

  if @row_buffer.empty?
    if @done
      return nil
    else
      fetch_more
    end
  end

  @row_buffer.shift
end

#has_more?Boolean

Returns true if there are any more rows to fetch.

Returns:

  • (Boolean)


68
69
70
# File 'lib/impala/cursor.rb', line 68

def has_more?
  !@done || !@row_buffer.empty?
end

#inspectObject



20
21
22
# File 'lib/impala/cursor.rb', line 20

def inspect
  "#<#{self.class}#{open? ? '' : ' (CLOSED)'}>"
end

#open?Boolean

Returns true if the cursor is still open.

Returns:

  • (Boolean)


63
64
65
# File 'lib/impala/cursor.rb', line 63

def open?
  @open
end