Skip to main content

snippets_py

Integration

New integration from credential
big_table_client = yepcode.integration.google_big_table("credential-slug")
New integration from plain authentication data
from google.cloud.bigtable.client import Client
from google.oauth2.service_account import Credentials

project_id = "yepcode"
credentialsDict = {
"type": "service_account",
"project_id": "yepcode",
"private_key_id": "XXXXX",
"private_key": "-----BEGIN PRIVATE KEY-----\nx\n-----END PRIVATE KEY-----",
"client_email": "yepcode@example.org",
"client_id": "1234567890",
"auth_uri": "https://example.org",
"token_uri": "https://example.org",
"auth_provider_x509_cert_url": "https://example.org",
"client_x509_cert_url": "https://example.org",
}
credentials = Credentials.from_service_account_info(credentialsDict)
big_table_client = Client(project=project_id, credentials=credentials)

Create Instance

Create an instance
from google.cloud.bigtable import enums

my_instance_id = "instance-id"
my_cluster_id = "cluster-id"
location_id = "location-id"
serve_nodes = 1
storage_type = enums.StorageType.SSD
production = enums.Instance.Type.PRODUCTION
labels = {"prod-label": "prod-label"}
instance = big_table_client.instance(my_instance_id, instance_type=production, labels=labels)
cluster = instance.cluster(
my_cluster_id,
location_id=location_id,
serve_nodes=serve_nodes,
default_storage_type=storage_type,
)
operation = instance.create(clusters=[cluster])
operation.result(timeout=100)

Create Table

Create a table
instance = big_table_client.instance("instance-id")
table = instance.table("table_id")
table.create()

Create Column Family

Create a column family
instance = big_table_client.instance("instance-id")
table = instance.table("table_id")
column_family = table.column_family("column_family_id"")
column_family.create()

Write a Row

Simple
instance = big_table_client.instance("instance-id")
table = instance.table("table_id")

column_family_id = "stats_summary"
row_key = "id#4c410523#20190501"
row = table.direct_row(row_key)
row.set_cell(column_family_id, "kind", "human")
row.set_cell(column_family_id, "nature", "agressive")
row.set_cell(column_family_id, "constitution", "strong")

row.commit()

print(f"Successfully wrote row {row_key}.")

Multiple
instance = big_table_client.instance("instance-id")
table = instance.table("table_id")
column_family_id = "stats_summary"

rows = [
table.direct_row("id#7x410521#90190501"),
table.direct_row("id#4c410523#20190501"),
]

rows[0].set_cell(column_family_id, "kind", "fremen")
rows[0].set_cell(column_family_id, "nature", "peaceful")
rows[0].set_cell(column_family_id, "constitution", "strong")
rows[1].set_cell(column_family_id, "kind", "human")
rows[1].set_cell(column_family_id, "nature", "agressive")
rows[1].set_cell(column_family_id, "constitution", "strong")

response = table.mutate_rows(rows)
for i, status in enumerate(response):
if status.code != 0:
print(f"Error writing row {i}: {status.message}")
raise Exception()
print("Successfully wrote 2 rows.")
Conditional
from google.cloud.bigtable import row_filters

instance = big_table_client.instance("instance-id")
table = instance.table("table_id")
column_family_id = "stats_summary"

row_key = "row-id"

row_filter = row_filters.RowFilterChain(
filters=[
row_filters.FamilyNameRegexFilter(column_family_id),
row_filters.ColumnQualifierRegexFilter("conditional-column-qualifier"),
row_filters.ValueRegexFilter("conditional-value"),
]
)
row = table.conditional_row(row_key, filter_=row_filter)
row.set_cell(column_family_id, "column-qualifier", "value")
row.commit()

print("Successfully updated row.")

Read a Row

Simple
instance = big_table_client.instance("instance-id")
table = instance.table("table_id")

row_key = "row-id"
row = table.read_row(row_key)
print(row)
Multiple
from google.cloud.bigquery.row_set import RowSet
instance = big_table_client.instance("instance-id")
table = instance.table("table_id")

row_set = RowSet()
row_set.add_row_key(b"row-id-1")
row_set.add_row_key(b"row-id-2")


rows = table.read_rows(row_set=row_set)
for row in rows:
print(row.cells)
Multiple based on prefix
from google.cloud.bigquery.row_set import RowSet
instance = big_table_client.instance("instance-id")
table = instance.table("table_id")

prefix = "prefix"
end_key = prefix[:-1] + chr(ord(prefix[-1]) + 1)

row_set = RowSet()
row_set.add_row_range_from_keys(prefix.encode("utf-8"), end_key.encode("utf-8"))

rows = table.read_rows(row_set=row_set)
for row in rows:
print(row.cells)
Conditional

from google.cloud.bigtable import row_filters

instance = big_table_client.instance("instance-id")
table = instance.table("table_id")
filter = row_filters.RowFilterChain(
filters=[
row_filters.FamilyNameRegexFilter("column-family-id"),
row_filters.ColumnQualifierRegexFilter("column-qualifier"),
row_filters.ValueRegexFilter("value"),
]
)
rows = table.read_rows(filter_=filter)
for row in rows:
print(row.cells)