Class: HBase::Scoped

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Aggregation, Util
Defined in:
lib/hbase-jruby/scoped.rb,
lib/hbase-jruby/scoped/aggregation.rb

Overview

@return [HBase::Table] HBase::Table instance for this scope

Defined Under Namespace

Modules: Aggregation

Constant Summary

Constants included from Util

Util::JAVA_BYTE_ARRAY_CLASS, Util::JAVA_BYTE_ARRAY_EMPTY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

append_0, from_bytes, java_bytes?, parse_column_name, to_bytes

Methods included from Aggregation

#aggregate

Instance Attribute Details

#tableObject (readonly)

Returns the value of attribute table.



11
12
13
# File 'lib/hbase-jruby/scoped.rb', line 11

def table
  @table
end

Instance Method Details

#at(ts) ⇒ HBase::Scoped

Returns an HBase::Scoped object with the specified timestamp

Parameters:

  • ts (Fixnum|Time)

    Timestamp

Returns:

  • (HBase::Scoped)

    HBase::Scoped object with the specified timestamp



174
175
176
# File 'lib/hbase-jruby/scoped.rb', line 174

def at ts
  spawn :@trange, time_to_long(ts)
end

#batch(b) ⇒ HBase::Scoped

Returns an HBase::Scoped object with the specified batch limit

Parameters:

  • b (Fixnum)

    Sets the maximum number of values to fetch each time

Returns:

  • (HBase::Scoped)

    HBase::Scoped object with the specified batch limit

Raises:

  • (ArgumentError)


215
216
217
218
# File 'lib/hbase-jruby/scoped.rb', line 215

def batch b
  raise ArgumentError, "Invalid batch size. Must be a positive integer." unless b.is_a?(Fixnum) && b > 0
  spawn :@batch, b
end

#caching(rows) ⇒ HBase::Scoped

Sets the number of rows for caching that will be passed to scanners.

Parameters:

  • rows (Fixnum)

    The number of rows to cache

Returns:

  • (HBase::Scoped)

    HBase::Scoped object with the caching option

Raises:

  • (ArgumentError)


83
84
85
86
# File 'lib/hbase-jruby/scoped.rb', line 83

def caching rows
  raise ArgumentError, "Invalid caching size. Must be a non-negative integer." unless rows.is_a?(Fixnum) && rows >= 0
  spawn :@caching, rows
end

#countFixnum, Bignum

Number of rows in the scope

Returns:

  • (Fixnum, Bignum)

    The number of rows in the scope



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hbase-jruby/scoped.rb', line 21

def count
  cnt = 0
  begin
    if block_given?
      scanner = htable.getScanner(filtered_scan)
      scanner.each do |result|
        cnt += 1 if yield(Result.send(:new, result))
      end
    else
      scanner = htable.getScanner(filtered_scan_minimum)
      scanner.each { cnt += 1 }
    end
  ensure
    scanner.close if scanner
  end
  cnt
end

#each {|row| ... } ⇒ Object

Iterate through the scope.

Yields:

  • (row)

    Yields each row in the scope

Yield Parameters:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hbase-jruby/scoped.rb', line 65

def each
  if block_given?
    begin
      scanner = htable.getScanner(filtered_scan)
      scanner.each do |result|
        yield Result.send(:new, result)
      end
    ensure
      scanner.close if scanner
    end
  else
    self
  end
end

#filter(*filters) ⇒ HBase::Scoped

Returns an HBase::Scoped object with the filters added

Parameters:

  • filters (Array<Hash, FilterBase, FilterList>)

Returns:

  • (HBase::Scoped)

    HBase::Scoped object also with the specified filters



144
145
146
# File 'lib/hbase-jruby/scoped.rb', line 144

def filter *filters
  spawn :@filters, @filters + parse_filter_input(filters)
end

#get(rowkey) ⇒ HBase::Result? #get(rowkeys) ⇒ Array<HBase::Result>

Performs GET operations

Overloads:

  • #get(rowkey) ⇒ HBase::Result?

    Single GET. Gets a record with the given rowkey. If the record is not found, nil is returned.

    Parameters:

    • rowkey (Object)

      Rowkey

    Returns:

  • #get(rowkeys) ⇒ Array<HBase::Result>

    Batch GET. Gets an array of records with the given rowkeys. Nonexistent records will be returned as nils.

    Parameters:

    • *rowkeys (Array<Object>)

      Rowkeys

    Returns:



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hbase-jruby/scoped.rb', line 50

def get rowkeys
  case rowkeys
  when Array
    htable.get(rowkeys.map { |rk| getify rk }).map { |result|
      result.isEmpty ? nil : Result.new(result)
    }
  else
    result = htable.get(getify rowkeys)
    result.isEmpty ? nil : Result.new(result)
  end
end

#limit(rows) ⇒ HBase::Scoped

Returns an HBase::Scoped object with the specified row number limit

Parameters:

  • rows (Fixnum)

    Sets the maximum number of rows to return from scan

Returns:

  • (HBase::Scoped)

    HBase::Scoped object with the specified row number limit

Raises:

  • (ArgumentError)


158
159
160
161
# File 'lib/hbase-jruby/scoped.rb', line 158

def limit rows
  raise ArgumentError, "Invalid limit. Must be a non-negative integer." unless rows.is_a?(Fixnum) && rows >= 0
  spawn :@limit, rows
end

#project(*columns) ⇒ HBase::Scoped

Returns an HBase::Scoped object with the specified projection

Parameters:

  • columns (Array<String>)

    Array of column expressions

Returns:

  • (HBase::Scoped)

    HBase::Scoped object with the specified projection



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/hbase-jruby/scoped.rb', line 181

def project *columns
  if columns.first.is_a?(Hash)
    hash = columns.first
    unless (hash.keys - [:prefix, :range, :limit, :offset]).empty?
      raise ArgumentError, "Invalid projection"
    end

    if l = hash[:limit]
      unless l.is_a?(Fixnum) && l >= 0
        raise ArgumentError, ":limit must be a non-negative integer"
      end
    end

    if o = hash[:offset]
      unless o.is_a?(Fixnum) && o >= 0
        raise ArgumentError, ":offset must be a non-negative integer"
      end
    end
  end
  spawn :@project, @project + columns
end

#range(start_key, opts = {}) ⇒ HBase::Scoped #range(start_key, stop_key, opts = {}) ⇒ HBase::Scoped #range(start_stop_range, opts = {}) ⇒ HBase::Scoped #range(opts) ⇒ HBase::Scoped

Overloads:

  • #range(start_key, opts = {}) ⇒ HBase::Scoped

    Returns an HBase::Scoped object with the specified rowkey range Overrides current range.

    Parameters:

    • start_key (Object)

      Start rowkey

    • opts (Hash) (defaults to: {})

      Prefix filter

    Options Hash (opts):

    • :prefix (Object, Array<Object>)

      Only rows matching any of the given prefixes are returned

    Returns:

  • #range(start_key, stop_key, opts = {}) ⇒ HBase::Scoped

    Returns an HBase::Scoped object with the specified rowkey range Overrides current range.

    Parameters:

    • start_key (Object, nil)

      Start rowkey. Can be nil.

    • stop_key (Object)

      Stop rowkey (exclusive)

    • opts (Hash) (defaults to: {})

      Prefix filter

    Options Hash (opts):

    • :prefix (Object, Array<Object>)

      Only rows matching any of the given prefixes are returned

    Returns:

  • #range(start_stop_range, opts = {}) ⇒ HBase::Scoped

    Returns an HBase::Scoped object with the specified rowkey range Overrides current range.

    Parameters:

    • start_stop_range (Range)

      Rowkey scan range

    • opts (Hash) (defaults to: {})

      Prefix filter

    Options Hash (opts):

    • :prefix (Object, Array<Object>)

      Only rows matching any of the given prefixes are returned

    Returns:

  • #range(opts) ⇒ HBase::Scoped

    Returns an HBase::Scoped object with the specified rowkey range Overrides current range.

    Examples:

    table.range(:prefix => '2012')
    table.range(:prefix => ['2010', '2012'])

    Parameters:

    • opts (Hash)

      Prefix filter

    Options Hash (opts):

    • :prefix (Object, Array<Object>)

      Only rows matching any of the given prefixes are returned

    Returns:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/hbase-jruby/scoped.rb', line 119

def range *key_range
  if key_range.last.is_a?(Hash)
    prefixes  = arrayfy(key_range.last[:prefix]).compact
    raise ArgumentError,
      "Invalid range. Unknown option(s) specified." unless (key_range.last.keys - [:prefix]).empty?
    key_range = key_range[0...-1]
  end

  if prefixes
    raise ArgumentError, "Invalid range" unless [0, 1, 2].include?(key_range.length)
  else
    raise ArgumentError, "Invalid range" unless [1, 2].include?(key_range.length)
  end

  spawn :@range,
        key_range[0].is_a?(Range) ?
            key_range[0] :
            (key_range.empty? ? nil : key_range),
        :@prefixes,
        prefixes || []
end

#time_range(min, max) ⇒ HBase::Scoped

Returns an HBase::Scoped object with the specified time range

Parameters:

  • min (Fixnum|Time)

    Minimum timestamp (inclusive)

  • max (Fixnum|Time)

    Maximum timestamp (exclusive)

Returns:

  • (HBase::Scoped)

    HBase::Scoped object with the specified time range



167
168
169
# File 'lib/hbase-jruby/scoped.rb', line 167

def time_range min, max
  spawn :@trange, [min, max].map { |e| time_to_long e }
end

#unscopeHBase::Scope

A clean HBase::Scoped object for the same table

Returns:

  • (HBase::Scope)

    A clean HBase::Scoped object for the same table



15
16
17
# File 'lib/hbase-jruby/scoped.rb', line 15

def unscope
  Scoped.send(:new, @table)
end

#versions(vs) ⇒ HBase::Scoped

Returns an HBase::Scoped object with the specified version number limit. If not set, all versions of each value are fetched by default.

Parameters:

  • vs (Fixnum)

    Sets the maximum number of versions

Returns:

  • (HBase::Scoped)

    HBase::Scoped object with the version number limit

Raises:

  • (ArgumentError)


207
208
209
210
# File 'lib/hbase-jruby/scoped.rb', line 207

def versions vs
  raise ArgumentError, "Invalid versions. Must be a positive integer." unless vs.is_a?(Fixnum) && vs > 0
  spawn :@versions, vs
end

#while(*filters) ⇒ HBase::Scoped

Returns an HBase::Scoped object with the additional filters which will cause early termination of scan

Parameters:

  • filters (Array<Hash, FilterBase, FilterList>)

Returns:

  • (HBase::Scoped)

    HBase::Scoped object with the additional filters which will cause early termination of scan



151
152
153
# File 'lib/hbase-jruby/scoped.rb', line 151

def while *filters
  spawn :@filters, @filters + parse_filter_input(filters).map { |filter| WhileMatchFilter.new(filter) }
end

#with_java_get {|org.apache.hadoop.hbase.client.Get| ... } ⇒ HBase::Scoped

Returns an HBase::Scoped object with the Get-customization block added The given block will be evaluated just before an actual get operation. With method-chaining, multiple blocks can be registered to be evaluated sequentially.

Yields:

  • (org.apache.hadoop.hbase.client.Get)

Returns:

Raises:

  • (ArgumentError)


236
237
238
239
240
# File 'lib/hbase-jruby/scoped.rb', line 236

def with_java_get &block
  raise ArgumentError, "Block not given" if block.nil?
  raise ArgumentError, "Invalid arity: should be 1" unless block.arity == 1
  spawn :@get_cbs, @get_cbs + [block]
end

#with_java_scan {|org.apache.hadoop.hbase.client.Scan| ... } ⇒ HBase::Scoped

Returns an HBase::Scoped object with the Scan-customization block added. The given block will be evaluated just before an actual scan operation. With method-chaining, multiple blocks can be registered to be evaluated sequentially.

Yields:

  • (org.apache.hadoop.hbase.client.Scan)

Returns:

Raises:

  • (ArgumentError)


225
226
227
228
229
# File 'lib/hbase-jruby/scoped.rb', line 225

def with_java_scan &block
  raise ArgumentError, "Block not given" if block.nil?
  raise ArgumentError, "Invalid arity: should be 1" unless block.arity == 1
  spawn :@scan_cbs, @scan_cbs + [block]
end