google-cloud-bigtable - Class Google::Cloud::Bigtable::Table (v2.6.5)

Reference documentation and code samples for the google-cloud-bigtable class Google::Cloud::Bigtable::Table.

Table

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

Inherits

  • Object

Example

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

Methods

#app_profile_id

def app_profile_id() -> String
Returns
  • (String) — App profile ID for request routing.

#app_profile_id=

def app_profile_id=(value) -> String
Parameter
  • value (String) — App profile ID for request routing.
Returns
  • (String) — App profile ID for request routing.

#check_consistency

def 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.

Parameter
  • token (String) — Consistency token
Returns
  • (Boolean) — true if replication is consistent
Example
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

#cluster_states

def cluster_states() -> Array<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.

Example

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

#column_families

def 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.

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 Parameter
  • 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.
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

#delete

def delete() -> Boolean

Permanently deletes the table from a instance.

Returns
  • (Boolean) — Returns true if the table was deleted.
Example
require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

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

#delete_all_rows

def delete_all_rows(timeout: nil) -> Boolean

Deletes all rows.

Parameter
  • 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)
Example
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.

#delete_rows_by_prefix

def delete_rows_by_prefix(prefix, timeout: nil) -> Boolean

Deletes rows using row key prefix.

Parameters
  • prefix (String) — Row key prefix (for example, "user").
  • timeout (Integer) (defaults to: nil) — Call timeout in seconds.
Returns
  • (Boolean)
Example
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.

#drop_row_range

def drop_row_range(row_key_prefix: nil, delete_all_data: nil, timeout: nil) -> Boolean

Drops row range by row key prefix or deletes all.

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)
Example
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.

#exists?

def exists?() -> Boolean

Checks to see if the table exists.

Returns
  • (Boolean)
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

#generate_consistency_token

def generate_consistency_token() -> String

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.

Returns
  • (String) — The generated consistency token
Example
require "google/cloud/bigtable"

bigtable = Google::Cloud::Bigtable.new

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

table.generate_consistency_token # "l947XelENinaxJQP0nnrZJjHnAF7YrwW8HCJLotwrF"

#granularity

def granularity() -> Symbol

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)

#granularity_millis?

def granularity_millis?() -> Boolean

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

Returns
  • (Boolean)

#instance_id

def instance_id() -> String

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

Returns
  • (String)

#name

def name() -> String
Aliases

The unique identifier for the table.

Returns
  • (String)

#path

def path() -> String

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

Returns
  • (String)

#policy

def policy() { |policy| ... } -> Policy

Gets the Cloud IAM access control policy for the table.

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 Parameter
  • policy (Policy) — the current Cloud IAM Policy for this table.
Returns
  • (Policy) — The current Cloud IAM 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:owner@example.com"
end # 2 API calls

#policy=

def policy=(new_policy) -> Policy
Alias Of: #update_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.

Parameter
  • new_policy (Policy) — a new or modified Cloud IAM Policy for this table
Returns
  • (Policy) — The policy returned by the API update operation.
Example
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:owner@example.com"
updated_policy = table.update_policy policy

puts updated_policy.roles

#project_id

def project_id() -> String

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

Returns
  • (String)

#reload!

def 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.

Parameter
  • 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.

#table_id

def table_id() -> String
Alias Of: #name

The unique identifier for the table.

Returns
  • (String)

#test_iam_permissions

def test_iam_permissions(*permissions) -> Array<String>

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

Parameter
  • 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.
Example
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

#update_policy

def update_policy(new_policy) -> Policy
Aliases

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.

Parameter
  • new_policy (Policy) — a new or modified Cloud IAM Policy for this table
Returns
  • (Policy) — The policy returned by the API update operation.
Example
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:owner@example.com"
updated_policy = table.update_policy policy

puts updated_policy.roles

#wait_for_replication

def 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.

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)
Example
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

Constants

FIELDS_BY_VIEW

value: { SCHEMA_VIEW: ["granularity", "column_families"], ENCRYPTION_VIEW: ["cluster_states"], REPLICATION_VIEW: ["cluster_states"], FULL: ["granularity", "column_families", "cluster_states"] }.freeze