Class: Google::Cloud::Bigtable::Table

Inherits:
Object
  • Object
show all
Includes:
MutationOperations, ReadOperations
Defined in:
lib/google/cloud/bigtable/table.rb,
lib/google/cloud/bigtable/table/list.rb,
lib/google/cloud/bigtable/table/restore_job.rb,
lib/google/cloud/bigtable/table/cluster_state.rb

Overview

Table

A collection of user data indexed by row, column, and timestamp. Each table is served using the resources of its parent cluster.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table "my-instance", "my-table"

if table.exists?
  p "Table exists."
else
  p "Table does not exist"
end

Defined Under Namespace

Classes: ClusterState, List, RestoreJob

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#app_profile_idString

Returns App profile ID for request routing.

Returns:

  • (String)

    App profile ID for request routing.



70
71
72
# File 'lib/google/cloud/bigtable/table.rb', line 70

def app_profile_id
  @app_profile_id
end

Instance Method Details

#check_and_mutate_row(key, predicate, on_match: nil, otherwise: nil) ⇒ Boolean Originally defined in module MutationOperations

Mutates a row atomically based on the output of a predicate reader filter.

NOTE: Condition predicate filter is not supported.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

predicate_filter = Google::Cloud::Bigtable::RowFilter.key "user-10"
on_match_mutations = Google::Cloud::Bigtable::MutationEntry.new
on_match_mutations.set_cell(
  "cf1",
  "field1",
  "XYZ",
  timestamp: (Time.now.to_f * 1_000_000).round(-3) # microseconds
).delete_cells "cf2", "field02"

otherwise_mutations = Google::Cloud::Bigtable::MutationEntry.new
otherwise_mutations.delete_from_family "cf3"

predicate_matched = table.check_and_mutate_row(
  "user01",
  predicate_filter,
  on_match: on_match_mutations,
  otherwise: otherwise_mutations
)

if predicate_matched
  puts "All predicates matched"
end

Parameters:

  • key (String)

    Row key. The row key of the row to which the conditional mutation should be applied.

  • predicate (SimpleFilter, ChainFilter, InterleaveFilter)

    Predicate filter. The filter to be applied to the contents of the specified row. Depending on whether or not any results are yielded, either +true_mutations+ or +false_mutations+ will be executed. If unset, checks that the row contains any values.

  • on_match (Google::Cloud::Bigtable::MutationEntry) (defaults to: nil)

    Mutation entry applied to predicate filter match. Changes to be atomically applied to the specified row if +predicate_filter+ yields at least one cell when applied to +row_key+. Entries are applied in order, meaning that earlier mutations can be masked by later ones. Must contain at least one entry if +false_mutations+ is empty and at most 100,000 entries.

  • otherwise (Google::Cloud::Bigtable::MutationEntry) (defaults to: nil)

    Mutation entry applied when predicate filter does not match. Changes to be atomically applied to the specified row if +predicate_filter+ does not yield any cells when applied to +row_key+. Entries are applied in order, meaning that earlier mutations can be masked by later ones. Must contain at least one entry if +true_mutations+ is empty and at most 100,000 entries.

Returns:

  • (Boolean)

    Predicate match or not status.

#check_consistency(token) ⇒ Boolean

Checks replication consistency based on a consistency token. Replication is considered consistent if replication has caught up based on the conditions specified in the token and the check request.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

instance = bigtable.instance "my-instance"
table = instance.table "my-table"

token = "l947XelENinaxJQP0nnrZJjHnAF7YrwW8HCJLotwrF"

if table.check_consistency token
  puts "Replication is consistent"
end

Parameters:

  • token (String)

    Consistency token

Returns:

  • (Boolean)

    true if replication is consistent



526
527
528
529
530
# File 'lib/google/cloud/bigtable/table.rb', line 526

def check_consistency token
  ensure_service!
  response = service.check_consistency instance_id, name, token
  response.consistent
end

#cluster_statesArray<Google::Cloud::Bigtable::Table::ClusterState>

Returns an array of ClusterState objects that map cluster ID to per-cluster table state.

If it could not be determined whether or not the table has data in a particular cluster (for example, if its zone is unavailable), then the cluster state's replication_state will be UNKNOWN.

Reloads the table with the FULL view type to retrieve the cluster states data, unless the table was previously loaded with view type ENCRYPTION_VIEW, REPLICATION_VIEW or FULL.

Examples:

Retrieve a table with cluster states.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table "my-instance", "my-table", view: :FULL, perform_lookup: true

table.cluster_states.each do |cs|
  puts cs.cluster_name
  puts cs.replication_state
  puts cs.encryption_infos.first.encryption_type
end

Returns:



171
172
173
174
175
176
# File 'lib/google/cloud/bigtable/table.rb', line 171

def cluster_states
  check_view_and_load :FULL, skip_if: [:ENCRYPTION_VIEW, :REPLICATION_VIEW]
  @grpc.cluster_states.map do |name, state_grpc|
    ClusterState.from_grpc state_grpc, name
  end
end

#column_families {|column_families| ... } ⇒ ColumnFamilyMap

Returns a frozen object containing the column families configured for the table, mapped by column family name.

Reloads the table if necessary to retrieve the column families data, since it is only available in a table with view type SCHEMA_VIEW or FULL. Previously loaded data is retained.

Also accepts a block for making modifications to the table's column families. After the modifications are completed, the table will be updated with the changes, and the updated column families will be returned.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table "my-instance", "my-table", perform_lookup: true

table.column_families.each do |name, cf|
  puts name
  puts cf.gc_rule
end

# Get a column family by name
cf1 = table.column_families["cf1"]

Modify the table's column families

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table "my-instance", "my-table", perform_lookup: true

table.column_families do |cfm|
  cfm.add "cf4", gc_rule: Google::Cloud::Bigtable::GcRule.max_age(600)
  cfm.add "cf5", gc_rule: Google::Cloud::Bigtable::GcRule.max_versions(5)

  rule_1 = Google::Cloud::Bigtable::GcRule.max_versions 3
  rule_2 = Google::Cloud::Bigtable::GcRule.max_age 600
  rule_union = Google::Cloud::Bigtable::GcRule.union rule_1, rule_2
  cfm.update "cf2", gc_rule: rule_union

  cfm.delete "cf3"
end

puts table.column_families["cf3"] #=> nil

Yields:

  • (column_families)

    A block for modifying the table's column families. Applies multiple column modifications. Performs a series of column family modifications on the specified table. Either all or none of the modifications will occur before this method returns, but data requests received prior to that point may see a table where only some modifications have taken effect.

Yield Parameters:

  • column_families (ColumnFamilyMap)

    A mutable object containing the column families for the table, mapped by column family name. Any changes made to this object will be stored in API.

Returns:

  • (ColumnFamilyMap)

    A frozen object containing the column families for the table, mapped by column family name.

See Also:



243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/google/cloud/bigtable/table.rb', line 243

def column_families
  check_view_and_load :SCHEMA_VIEW

  if block_given?
    column_families = ColumnFamilyMap.from_grpc @grpc.column_families
    yield column_families
    modifications = column_families.modifications @grpc.column_families
    @grpc = service.modify_column_families instance_id, table_id, modifications if modifications.any?
  end

  ColumnFamilyMap.from_grpc(@grpc.column_families).freeze
end

#deleteBoolean

Permanently deletes the table from a instance.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table "my-instance", "my-table"
table.delete

Returns:

  • (Boolean)

    Returns true if the table was deleted.



405
406
407
408
409
# File 'lib/google/cloud/bigtable/table.rb', line 405

def delete
  ensure_service!
  service.delete_table instance_id, name
  true
end

#delete_all_rows(timeout: nil) ⇒ Boolean

Deletes all rows.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

instance = bigtable.instance "my-instance"
table = instance.table "my-table"
table.delete_all_rows

# With timeout
table.delete_all_rows timeout: 120 # 120 seconds.

Parameters:

  • timeout (Integer) (defaults to: nil)

    Call timeout in seconds. Use in case of insufficient deadline for DropRowRange, then try again with a longer request deadline.

Returns:

  • (Boolean)


597
598
599
# File 'lib/google/cloud/bigtable/table.rb', line 597

def delete_all_rows timeout: nil
  drop_row_range delete_all_data: true, timeout: timeout
end

#delete_rows_by_prefix(prefix, timeout: nil) ⇒ Boolean

Deletes rows using row key prefix.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table "my-instance", "my-table"

table.delete_rows_by_prefix "user-100"

# With timeout
table.delete_rows_by_prefix "user-1", timeout: 120 # 120 seconds.

Parameters:

  • prefix (String)

    Row key prefix (for example, "user").

  • timeout (Integer) (defaults to: nil)

    Call timeout in seconds.

Returns:

  • (Boolean)


619
620
621
# File 'lib/google/cloud/bigtable/table.rb', line 619

def delete_rows_by_prefix prefix, timeout: nil
  drop_row_range row_key_prefix: prefix, timeout: timeout
end

#drop_row_range(row_key_prefix: nil, delete_all_data: nil, timeout: nil) ⇒ Boolean

Drops row range by row key prefix or deletes all.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table "my-instance", "my-table"

# Delete rows using row key prefix.
table.drop_row_range row_key_prefix: "user-100"

# Delete all data With timeout
table.drop_row_range delete_all_data: true, timeout: 120 # 120 seconds.

Parameters:

  • row_key_prefix (String) (defaults to: nil)

    Row key prefix (for example, "user").

  • delete_all_data (Boolean) (defaults to: nil)
  • timeout (Integer) (defaults to: nil)

    Call timeout in seconds.

Returns:

  • (Boolean)


644
645
646
647
648
649
650
651
652
653
654
# File 'lib/google/cloud/bigtable/table.rb', line 644

def drop_row_range row_key_prefix: nil, delete_all_data: nil, timeout: nil
  ensure_service!
  service.drop_row_range(
    instance_id,
    name,
    row_key_prefix:             row_key_prefix,
    delete_all_data_from_table: delete_all_data,
    timeout:                    timeout
  )
  true
end

#exists?Boolean

Checks to see if the table exists.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table "my-instance", "my-table"

if table.exists?
  p "Table exists."
else
  p "Table does not exist"
end

Using Cloud Bigtable instance

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

instance = bigtable.instance "my-instance"
table = instance.table "my-table"

if table.exists?
  p "Table exists."
else
  p "Table does not exist"
end

Returns:

  • (Boolean)


443
444
445
446
447
# File 'lib/google/cloud/bigtable/table.rb', line 443

def exists?
  !service.get_table(instance_id, name, view: :NAME_ONLY).nil?
rescue Google::Cloud::NotFoundError
  false
end

#filterGoogle::Cloud::Bigtable::RowRange Originally defined in module ReadOperations

Gets a row filter.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

filter = table.filter.key "user-*"

Returns:

#generate_consistency_tokenString

Generates a consistency token for a table. The token can be used in CheckConsistency to check whether mutations to the table that finished before this call started have been replicated. The tokens will be available for 90 days.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

instance = bigtable.instance "my-instance"
table = instance.table "my-table"

table.generate_consistency_token # "l947XelENinaxJQP0nnrZJjHnAF7YrwW8HCJLotwrF"

Returns:

  • (String)

    The generated consistency token



499
500
501
502
503
# File 'lib/google/cloud/bigtable/table.rb', line 499

def generate_consistency_token
  ensure_service!
  response = service.generate_consistency_token instance_id, name
  response.consistency_token
end

#granularitySymbol

The granularity (e.g. MILLIS, MICROS) at which timestamps are stored in this table. Timestamps not matching the granularity will be rejected. If unspecified at creation time, the value will be set to MILLIS.

Reloads the table if necessary to retrieve the column families data, since it is only available in a table with view type SCHEMA_VIEW or FULL. Previously loaded data is retained.

Returns:

  • (Symbol)


267
268
269
270
# File 'lib/google/cloud/bigtable/table.rb', line 267

def granularity
  check_view_and_load :SCHEMA_VIEW
  @grpc.granularity
end

#granularity_millis?Boolean

The table keeps data versioned at a granularity of 1 ms.

Returns:

  • (Boolean)


277
278
279
# File 'lib/google/cloud/bigtable/table.rb', line 277

def granularity_millis?
  granularity == :MILLIS
end

#instance_idString

The unique identifier for the instance to which the table belongs.

Returns:

  • (String)


98
99
100
# File 'lib/google/cloud/bigtable/table.rb', line 98

def instance_id
  @grpc.name.split("/")[3]
end

#mutate_row(entry) ⇒ Boolean Originally defined in module MutationOperations

Mutates a row atomically. Cells in the row are left unchanged unless explicitly changed by the mutations. Changes to be atomically applied to the specified row. Entries are applied in order, meaning that earlier mutations can be masked by later mutations. Must contain at least one mutation and at most 100,000.

Examples:

Single mutation on row.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table "my-instance", "my-table"

entry = table.new_mutation_entry "user-1"
entry.set_cell "cf1", "field1", "XYZ"
table.mutate_row entry

Multiple mutations on row.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table "my-instance", "my-table"

entry = table.new_mutation_entry "user-1"
entry.set_cell(
  "cf1",
  "field1",
  "XYZ",
  timestamp: (Time.now.to_f * 1_000_000).round(-3) # microseconds
).delete_cells "cf2", "field02"

table.mutate_row entry

Parameters:

Returns:

  • (Boolean)

#mutate_rows(entries) ⇒ Array<Google::Cloud::Bigtable::V2::MutateRowsResponse::Entry> Originally defined in module MutationOperations

Mutates multiple rows in a batch. Each individual row is mutated atomically as in ##mutate_row, but the entire batch is not executed atomically.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table "my-instance", "my-table"

entries = []
entries << table.new_mutation_entry("row-1").set_cell("cf1", "field1", "XYZ")
entries << table.new_mutation_entry("row-2").set_cell("cf1", "field1", "ABC")
responses = table.mutate_rows entries

responses.each do |response|
  puts response.status.description
end

Parameters:

  • entries (Array<Google::Cloud::Bigtable::MutationEntry>)

    The row keys and corresponding mutations to be applied in bulk. Each entry is applied as an atomic mutation, but the entries may be applied in arbitrary order (even between entries for the same row). At least one entry must be specified, and in total the entries can contain a maximum of 100,000 mutations.

Returns:

  • (Array<Google::Cloud::Bigtable::V2::MutateRowsResponse::Entry>)

#nameString Also known as: table_id

The unique identifier for the table.

Returns:

  • (String)


107
108
109
# File 'lib/google/cloud/bigtable/table.rb', line 107

def name
  @grpc.name.split("/")[5]
end

#new_column_range(family) ⇒ Google::Cloud::Bigtable::ColumnRange Originally defined in module ReadOperations

Gets a new instance of ColumnRange.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

range = table.new_column_range "test-family"
range.from "abc"
range.to "xyz"

# OR
range = table.new_column_range("test-family").from("key-1").to("key-5")

With exclusive from range.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

range = table.new_column_range("test-family").from("key-1", inclusive: false).to("key-5")

Parameters:

  • family (String)

    Column family name.

Returns:

#new_mutation_entry(row_key = nil) ⇒ Google::Cloud::Bigtable::MutationEntry Originally defined in module MutationOperations

Creates a mutation entry instance.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

entry = table.new_mutation_entry "row-key-1"

# Without row key
entry = table.new_mutation_entry

Parameters:

  • row_key (String) (defaults to: nil)

    Row key. Optional. The row key of the row to which the mutation should be applied.

Returns:

#new_read_modify_write_rule(family, qualifier) ⇒ Google::Cloud::Bigtable::ReadModifyWriteRule Originally defined in module MutationOperations

Create a read/modify/write rule to append or increment the value of the cell qualifier.

Examples:

Create rule to append to qualifier value.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"
rule = table.new_read_modify_write_rule "cf", "qualifier-1"
rule.append "append-xyz"

Create rule to increment qualifier value.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"
rule = table.new_read_modify_write_rule "cf", "qualifier-1"
rule.increment 100

Parameters:

  • family (String)

    The name of the column family to which the read/modify/write rule should be applied.

  • qualifier (String)

    The qualifier of the column to which the read/modify/write rule should be applied.

Returns:

#new_row_rangeGoogle::Cloud::Bigtable::RowRange Originally defined in module ReadOperations

Gets a new instance of RowRange.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

range = table.new_row_range
range.from "abc"
range.to "xyz"

# OR
range = table.new_row_range.from("key-1").to("key-5")

With exclusive from range.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

range = table.new_row_range.from("key-1", inclusive: false).to("key-5")

Returns:

#new_value_rangeGoogle::Cloud::Bigtable::ValueRange Originally defined in module ReadOperations

Creates a new instance of ValueRange.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

range = table.new_value_range
range.from "abc"
range.to "xyz"

# OR
range = table.new_value_range.from("abc").to("xyz")

With exclusive from range.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

range = table.new_value_range.from("abc", inclusive: false).to("xyz")

Returns:

#pathString

The full path for the table resource. Values are of the form projects/<project_id>/instances/<instance_id>/table/<table_id>.

Returns:

  • (String)


118
119
120
# File 'lib/google/cloud/bigtable/table.rb', line 118

def path
  @grpc.name
end

#policy {|policy| ... } ⇒ Policy

Gets the Cloud IAM access control policy for the table.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table "my-instance", "my-table", perform_lookup: true
policy = table.policy

Update the policy by passing a block.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table "my-instance", "my-table", perform_lookup: true

table.policy do |p|
  p.add "roles/owner", "user:[email protected]"
end # 2 API calls

Yields:

  • (policy)

    A block for updating the policy. The latest policy will be read from the Bigtable service and passed to the block. After the block completes, the modified policy will be written to the service.

Yield Parameters:

  • policy (Policy)

    the current Cloud IAM Policy for this table.

Returns:

  • (Policy)

    The current Cloud IAM Policy for the table.

See Also:



315
316
317
318
319
320
321
322
# File 'lib/google/cloud/bigtable/table.rb', line 315

def policy
  ensure_service!
  grpc = service.get_table_policy instance_id, name
  policy = Policy.from_grpc grpc
  return policy unless block_given?
  yield policy
  update_policy policy
end

#project_idString

The unique identifier for the project to which the table belongs.

Returns:

  • (String)


89
90
91
# File 'lib/google/cloud/bigtable/table.rb', line 89

def project_id
  @grpc.name.split("/")[1]
end

#read_modify_write_row(key, rules) ⇒ Google::Cloud::Bigtable::Row Originally defined in module MutationOperations

Modifies a row atomically on the server. The method reads the latest existing timestamp and value from the specified columns and writes a new entry based on pre-defined read/modify/write rules. The new value for the timestamp is the greater of the existing timestamp or the current server time. The method returns the new contents of all modified cells.

Examples:

Apply multiple modification rules.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

rule_1 = table.new_read_modify_write_rule "cf", "field01"
rule_1.append "append-xyz"

rule_2 = table.new_read_modify_write_rule "cf", "field01"
rule_2.increment 1

row = table.read_modify_write_row "user01", [rule_1, rule_2]

puts row.cells

Apply single modification rules.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

rule = table.new_read_modify_write_rule("cf", "field01").append("append-xyz")

row = table.read_modify_write_row "user01", rule

puts row.cells

Parameters:

Returns:

#read_row(key, filter: nil) ⇒ Google::Cloud::Bigtable::Row Originally defined in module ReadOperations

Reads a single row by row key.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

row = table.read_row "user-1"

Read row with filter.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

filter = Google::Cloud::Bigtable::RowFilter.cells_per_row 3

row = table.read_row "user-1", filter: filter

Parameters:

  • key (String)

    Row key. Required.

  • filter (Google::Cloud::Bigtable::RowFilter) (defaults to: nil)

    The filter to apply to the contents of the specified row. Optional.

Returns:

#read_rows(keys: nil, ranges: nil, filter: nil, limit: nil, &block) ⇒ Array<Google::Cloud::Bigtable::Row> | :yields: row Originally defined in module ReadOperations

Reads rows.

Streams back the contents of all requested rows in key order, optionally applying the same Reader filter to each. read_rows, row_ranges and filter if not specified, reads from all rows.

See Google::Cloud::Bigtable::RowFilter for filter types.

Examples:

Read with limit.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

table.read_rows(limit: 10).each do |row|
  puts row
end

Read using row keys.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

table.read_rows(keys: ["user-1", "user-2"]).each do |row|
  puts row
end

Read using row ranges.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

range = table.new_row_range.between "user-1", "user-100"

table.read_rows(ranges: range).each do |row|
  puts row
end

Read using filter.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

filter = table.filter.key "user-*"
# OR
# filter = Google::Cloud::Bigtable::RowFilter.key "user-*"

table.read_rows(filter: filter).each do |row|
  puts row
end

Read using filter with limit.

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

filter = table.filter.key "user-*"
# OR
# filter = Google::Cloud::Bigtable::RowFilter.key "user-*"

table.read_rows(filter: filter, limit: 10).each do |row|
  puts row
end

Parameters:

  • keys (Array<String>) (defaults to: nil)

    List of row keys to be read. Optional.

  • ranges (Google::Cloud::Bigtable::RowRange | Array<Google::Cloud::Bigtable::RowRange>) (defaults to: nil)

    Row ranges array or single range. Optional.

  • filter (SimpleFilter, ChainFilter, InterleaveFilter, ConditionFilter) (defaults to: nil)

    The filter to apply to the contents of the specified row(s). If unset, reads the entries of each row. Optional.

  • limit (Integer) (defaults to: nil)

    Limit number of read rows count. Optional. The read will terminate after committing to N rows' worth of results. The default (zero) is to return all results.

Returns:

#reload!(view: nil) ⇒ Google::Cloud::Bigtable::Table

Reloads table data with the provided view, or with SCHEMA_VIEW if none is provided. Previously loaded data is not retained.

Parameters:

  • view (Symbol) (defaults to: nil)

    Table view type. Default view type is :SCHEMA_VIEW. Valid view types are:

    • :NAME_ONLY - Only populates name.
    • :SCHEMA_VIEW - Only populates name and fields related to the table's schema.
    • :REPLICATION_VIEW - Only populates name and fields related to the table's replication state.
    • :FULL - Populates all fields.

Returns:



137
138
139
140
141
142
# File 'lib/google/cloud/bigtable/table.rb', line 137

def reload! view: nil
  view ||= :SCHEMA_VIEW
  @grpc = service.get_table instance_id, name, view: view
  @loaded_views = Set[view]
  self
end

#sample_row_keys:yields: sample_row_key Originally defined in module ReadOperations

Reads sample row keys.

Returns a sample of row keys in the table. The returned row keys will delimit contiguous sections of the table of approximately equal size. The sections can be used to break up the data for distributed tasks like MapReduces.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new
table = bigtable.table "my-instance", "my-table"

table.sample_row_keys.each do |sample_row_key|
  p sample_row_key.key # user00116
  p sample_row_key.offset # 805306368
end

Yield Returns:

Returns:

  • (:yields: sample_row_key)

    Yield block for each processed SampleRowKey.

#test_iam_permissions(*permissions) ⇒ Array<String>

Tests the specified permissions against the Cloud IAM access control policy.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table "my-instance", "my-table", perform_lookup: true

permissions = table.test_iam_permissions(
  "bigtable.tables.delete",
  "bigtable.tables.get"
)
permissions.include? "bigtable.tables.delete" #=> false
permissions.include? "bigtable.tables.get" #=> true

Parameters:

  • permissions (String, Array<String>)

    permissions The set of permissions to check access for. Permissions with wildcards (such as * or bigtable.*) are not allowed. See Access Control.

Returns:

  • (Array<String>)

    The permissions that are configured for the policy.

See Also:



386
387
388
389
390
# File 'lib/google/cloud/bigtable/table.rb', line 386

def test_iam_permissions *permissions
  ensure_service!
  grpc = service.test_table_permissions instance_id, name, permissions.flatten
  grpc.permissions.to_a
end

#update_policy(new_policy) ⇒ Policy Also known as: policy=

Updates the Cloud IAM access control policy for the table. The policy should be read from #policy. See Policy for an explanation of the policy etag property and how to modify policies.

You can also update the policy by passing a block to #policy, which will call this method internally after the block completes.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table "my-instance", "my-table", perform_lookup: true

policy = table.policy
policy.add "roles/owner", "user:[email protected]"
updated_policy = table.update_policy policy

puts updated_policy.roles

Parameters:

  • new_policy (Policy)

    a new or modified Cloud IAM Policy for this table

Returns:

  • (Policy)

    The policy returned by the API update operation.



351
352
353
354
355
# File 'lib/google/cloud/bigtable/table.rb', line 351

def update_policy new_policy
  ensure_service!
  grpc = service.set_table_policy instance_id, name, new_policy.to_grpc
  Policy.from_grpc grpc
end

#wait_for_replication(timeout: 600, check_interval: 5) ⇒ Boolean

Wait for replication to check replication consistency. Checks replication consistency by generating a consistency token and making the check_consistency API call 5 times (by default). If the response is consistent, returns true. Otherwise tries again repeatedly until the timeout. If the check does not succeed by the timeout, returns false.

Examples:

require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

table = bigtable.table "my-instance", "my-table", perform_lookup: true

if table.wait_for_replication
  puts "Replication done"
end

# With custom timeout and interval
if table.wait_for_replication timeout: 300, check_interval: 10
  puts "Replication done"
end

Parameters:

  • timeout (Integer) (defaults to: 600)

    Timeout in seconds. Defaults value is 600 seconds.

  • check_interval (Integer) (defaults to: 5)

    Consistency check interval in seconds. Default is 5 seconds.

Returns:

  • (Boolean)

    true if replication is consistent

Raises:

  • (InvalidArgumentError)


562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/google/cloud/bigtable/table.rb', line 562

def wait_for_replication timeout: 600, check_interval: 5
  raise InvalidArgumentError, "'check_interval' cannot be greater than timeout" if check_interval > timeout
  token = generate_consistency_token
  status = false
  start_at = Time.now

  loop do
    status = check_consistency token

    break if status || (Time.now - start_at) >= timeout
    sleep check_interval
  end
  status
end