Module: OkHbase::Concerns::Table

Extended by:
ActiveSupport::Concern
Included in:
ClassMethods::ClassMethods, Table
Defined in:
lib/ok_hbase/concerns/table.rb,
lib/ok_hbase/concerns/table/batch.rb,
lib/ok_hbase/concerns/table/class_methods.rb,
lib/ok_hbase/concerns/table/instrumentation.rb

Defined Under Namespace

Modules: ClassMethods, Instrumentation Classes: Batch

Constant Summary collapse

SCANNER_DEFAULTS =
{
    start_row: nil,
    stop_row: nil,
    row_prefix: nil,
    columns: nil,
    filter_string: nil,
    timestamp: nil,
    include_timestamp: false,
    caching: 1000,
    limit: nil,
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



20
21
22
# File 'lib/ok_hbase/concerns/table.rb', line 20

def connection
  @connection
end

#table_nameObject

Returns the value of attribute table_name.



20
21
22
# File 'lib/ok_hbase/concerns/table.rb', line 20

def table_name
  @table_name
end

Class Method Details

.connectionObject



30
31
32
# File 'lib/ok_hbase/concerns/table.rb', line 30

def self.connection
  @self.connection
end

.connection=(val) ⇒ Object



34
35
36
# File 'lib/ok_hbase/concerns/table.rb', line 34

def self.connection=(val)
  @self.connection = val
end

Instance Method Details

#_column_family_namesObject



209
210
211
# File 'lib/ok_hbase/concerns/table.rb', line 209

def _column_family_names()
  self.connection.client.getColumnDescriptors(self.connection.table_name(table_name)).keys()
end

#_make_row(cell_map, include_timestamp) ⇒ Object



232
233
234
235
236
237
238
# File 'lib/ok_hbase/concerns/table.rb', line 232

def _make_row(cell_map, include_timestamp)
  row = {}
  cell_map.each_pair do |cell_name, cell|
    row[cell_name] = include_timestamp ? [cell.value, cell.timestamp] : cell.value
  end
  row
end

#_scanner(opts) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/ok_hbase/concerns/table.rb', line 213

def _scanner(opts)
  scanner = Apache::Hadoop::Hbase::Thrift::TScan.new()
  scanner_fields = Apache::Hadoop::Hbase::Thrift::TScan::FIELDS

  opts.each_pair do |k, v|
    const = k.to_s.upcase.gsub('_', '')
    const_value = Apache::Hadoop::Hbase::Thrift::TScan.const_get(const) rescue nil

    if const_value
      v.force_encoding(Encoding::UTF_8) if v.is_a?(String)
      OkHbase.logger.info "setting scanner.#{scanner_fields[const_value][:name]}: #{v}"
      scanner.send("#{scanner_fields[const_value][:name]}=", v)
    else
    end
  end
  scanner

end

#batch(timestamp = nil, batch_size = nil, transaction = false) ⇒ Object



185
186
187
# File 'lib/ok_hbase/concerns/table.rb', line 185

def batch(timestamp = nil, batch_size = nil, transaction = false)
  Batch.new(self, timestamp, batch_size, transaction)
end

#cells(row_key, column, versions = nil, timestamp = nil, include_timestamp = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ok_hbase/concerns/table.rb', line 92

def cells(row_key, column, versions = nil, timestamp = nil, include_timestamp = nil)

  row_key.force_encoding(Encoding::UTF_8)

  versions ||= (2 ** 31) -1

  raise TypeError.new "'versions' parameter must be a number or nil" unless versions.is_a? Integer
  raise ArgumentError.new "'versions' parameter must be >= 1" unless versions >= 1

  cells = if timestamp
    raise TypeError.new "'timestamp' must be an integer" unless timestamp.is_a? Integer

    self.connection.client.getVerTs(self.connection.table_name(table_name), row_key, column, timestamp, versions)
  else
    self.connection.client.getVer(self.connection.table_name(table_name), row_key, column, versions)
  end

  cells.map { |cell| include_timestamp ? [cell.value, cell.timestamp] : cell.value }
end

#counter_dec(row_key, column, value = 1) ⇒ Object



203
204
205
# File 'lib/ok_hbase/concerns/table.rb', line 203

def counter_dec(row_key, column, value = 1)
  counter_inc(row_key, column, -value)
end

#counter_get(row_key, column) ⇒ Object



189
190
191
# File 'lib/ok_hbase/concerns/table.rb', line 189

def counter_get(row_key, column)
  counter_inc(row_key, column, 0)
end

#counter_inc(row_key, column, value = 1) ⇒ Object



199
200
201
# File 'lib/ok_hbase/concerns/table.rb', line 199

def counter_inc(row_key, column, value = 1)
  self.connection.client.atomicIncrement(self.connection.table_name(table_name), row_key, column, value)
end

#counter_set(row_key, column, value = 0) ⇒ Object



193
194
195
196
197
# File 'lib/ok_hbase/concerns/table.rb', line 193

def counter_set(row_key, column, value = 0)
  self.batch.transaction do |batch|
    batch.put(row_key, { column => [value].pack('Q>') })
  end
end

#delete(row_key, columns = nil, timestamp = nil) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/ok_hbase/concerns/table.rb', line 173

def delete(row_key, columns = nil, timestamp = nil)
  if columns
    batch = self.batch(timestamp)
    batch.transaction do |batch|
      batch.delete(row_key, columns)
    end

  else
    timestamp ? self.connection.client.deleteAllRowTs(self.connection.table_name(table_name), row_key, timestamp) : self.connection.client.deleteAllRow(self.connection.table_name(table_name), row_key)
  end
end

#familiesObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ok_hbase/concerns/table.rb', line 39

def families()
  descriptors = self.connection.client.getColumnDescriptors(self.connection.table_name(table_name))

  families = {}

  descriptors.each_pair do |name, descriptor|
    name = name[0...-1] # remove trailing ':'
    families[name] = OkHbase.thrift_type_to_dict(descriptor)
  end
  families
end

#put(row_key, data, timestamp = nil) ⇒ Object



165
166
167
168
169
170
171
# File 'lib/ok_hbase/concerns/table.rb', line 165

def put(row_key, data, timestamp = nil)
  batch = self.batch(timestamp)

  batch.transaction do |batch|
    batch.put(row_key, data)
  end
end

#regionsObject



51
52
53
54
# File 'lib/ok_hbase/concerns/table.rb', line 51

def regions
  regions = self.connection.client.getTableRegions(self.connection.table_name(table_name))
  regions.map { |r| OkHbase.thrift_type_to_dict(r) }
end

#row(row_key, columns = nil, timestamp = nil, include_timestamp = false) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ok_hbase/concerns/table.rb', line 56

def row(row_key, columns = nil, timestamp = nil, include_timestamp = false)
  raise TypeError.new "'columns' must be a tuple or list" if columns && !columns.is_a?(Array)

  row_key.force_encoding(Encoding::UTF_8)

  rows = if timestamp
    raise TypeError.new "'timestamp' must be an integer" unless timestamp.is_a? Integer

    self.connection.client.getRowWithColumnsTs(self.connection.table_name(table_name), row_key, columns, timestamp)
  else
    self.connection.client.getRowWithColumns(self.connection.table_name(table_name), row_key, columns)
  end

  rows.empty? ? {} : _make_row(rows[0].columns, include_timestamp)
end

#rows(row_keys, columns = nil, timestamp = nil, include_timestamp = false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ok_hbase/concerns/table.rb', line 72

def rows(row_keys, columns = nil, timestamp = nil, include_timestamp = false)
  raise TypeError.new "'columns' must be a tuple or list" if columns && !columns.is_a?(Array)

  row_keys.map! { |r| r.force_encoding(Encoding::UTF_8) }

  return {} if row_keys.blank?

  rows = if timestamp
    raise TypeError.new "'timestamp' must be an integer" unless timestamp.is_a? Integer

    columns = _column_family_names() unless columns

    self.connection.client.getRowsWithColumnsTs(self.connection.table_name(table_name), row_keys, columns, timestamp)
  else
    self.connection.client.getRowsWithColumns(self.connection.table_name(table_name), row_keys, columns)
  end

  rows.map { |row| _make_row(row.columns, include_timestamp) }
end

#scan(opts = {}) ⇒ Object Also known as: find



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/ok_hbase/concerns/table.rb', line 112

def scan(opts={})

  rows = [] unless block_given?
  opts = SCANNER_DEFAULTS.merge opts.select { |k| SCANNER_DEFAULTS.keys.include? k }


  raise ArgumentError.new "'caching' must be >= 1" unless opts[:caching] && opts[:caching] >= 1
  raise ArgumentError.new "'limit' must be >= 1" if opts[:limit] && opts[:limit] < 1

  if opts[:row_prefix]
    raise ArgumentError.new "'row_prefix' cannot be combined with 'start_row' or 'stop_row'" if opts[:start_row] || opts[:stop_row]

    opts[:start_row] = opts[:row_prefix]
    opts[:stop_row] = OkHbase::increment_string opts[:start_row]

  end
  opts[:start_row] ||= ''

  scanner = _scanner(opts)

  scanner_id = self.connection.client.scannerOpenWithScan(self.connection.table_name(table_name), scanner)

  fetched_count = returned_count = 0

  begin
    while true
      how_many = opts[:limit] ? [opts[:caching], opts[:limit] - returned_count].min : opts[:caching]

      items = if how_many == 1
        self.connection.client.scannerGet(scanner_id)
      else
        self.connection.client.scannerGetList(scanner_id, how_many)
      end

      fetched_count += items.length

      items.map.with_index do |item, index|
        if block_given?
          yield item.row, _make_row(item.columns, opts[:include_timestamp])
        else
          rows << [item.row, _make_row(item.columns, opts[:include_timestamp])]
        end
        return rows if opts[:limit] && index + 1 + returned_count == opts[:limit]
      end

      break if items.length < how_many
    end
  ensure
    self.connection.client.scannerClose(scanner_id)
  end
  rows
end