Skip to content

Commit debaf6e

Browse files
committed
Define full table ID upfront.
1 parent e73948d commit debaf6e

File tree

7 files changed

+1054
-41
lines changed

7 files changed

+1054
-41
lines changed

Diff for: Makefile

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
SRCPATH := $(shell pwd)
2-
PROJECTNAME := $(shell basename $(CURDIR))
2+
PROJECTNAME := $(shell basename $CURDIR)
33
ENTRYPOINT := $(PROJECTNAME).ini
44

55
define HELP
@@ -46,7 +46,7 @@ restart: env
4646
.PHONY: deploy
4747
deploy:
4848
make clean
49-
$(shell . ./deploy.sh)
49+
. ./deploy.sh
5050

5151

5252
.PHONY: update
@@ -73,10 +73,3 @@ clean:
7373
find . -name '__pycache__' -delete
7474
find . -name 'poetry.lock' -delete
7575
find . -name 'Pipefile.lock' -delete
76-
find . -name 'logs/*.json' -delete
77-
find . -name '*.log' -delete
78-
find . -name '*/.pytest_cache' -delete
79-
find . -name '*/logs/*.json' -delete
80-
rm -rf .pytest_cache
81-
rm -rf tests/.pytest_cache
82-
rm -rf clients/tests/.pytest_cache

Diff for: bigquery_python_tutorial/__init__.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
GCP_BIGQUERY_DATASET_ID,
1111
GCP_BIGQUERY_TABLE_ID,
1212
GCP_BUCKET_NAME,
13+
GCP_BIGQUERY_FULL_TABLE_ID,
1314
)
1415

1516

@@ -19,9 +20,7 @@ def init_script():
1920
blob = upload_csv_data(LOCAL_CSV_FILEPATH, GCP_BUCKET_NAME, REMOTE_CSV_DESTINATION)
2021

2122
# Insert CSV as new BigQuery table
22-
table = gcs_csv_to_table(
23-
GCP_PROJECT_ID, GCP_BIGQUERY_DATASET_ID, GCP_BIGQUERY_TABLE_ID, blob.name
24-
)
23+
table = gcs_csv_to_table(GCP_BIGQUERY_FULL_TABLE_ID, blob.name)
2524

2625
# Print schema of newly created table
2726
get_table_schema(table)

Diff for: bigquery_python_tutorial/insert.py

+5-12
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,17 @@
66
from config import GCP_BUCKET_NAME
77

88

9-
def gcs_csv_to_table(
10-
project_id: str, dataset_id: str, table_id: str, remote_csv_path: str
11-
) -> Table:
9+
def gcs_csv_to_table(full_table_id: str, remote_csv_path: str) -> Table:
1210
"""
1311
Insert CSV from Google Storage to BigQuery Table.
1412
15-
:param project_id: Google Cloud project ID.
16-
:type project_id: str
17-
:param dataset_id: ID of Google BigQuery dataset.
18-
:type dataset_id: str
19-
:param table_id: ID of Google BigQuery table.
20-
:type table_id: str
13+
:param full_table_id: Full ID of a Google BigQuery table.
14+
:type full_table_id: str
2115
:param remote_csv_path: Path to uploaded CSV.
2216
:type remote_csv_path: str
2317
:returns: str
2418
"""
2519
try:
26-
full_table_id = f"{project_id}.{dataset_id}.{table_id}"
2720
gcs_csv_uri = f"gs://{GCP_BUCKET_NAME}/{remote_csv_path}"
2821
job_config = LoadJobConfig(
2922
autodetect=True,
@@ -37,6 +30,6 @@ def gcs_csv_to_table(
3730
LOGGER.info(load_job.result()) # Waits for table load to complete.
3831
return gbq.get_table(full_table_id)
3932
except BadRequest as e:
40-
LOGGER.error(f"Invalid GCP request when creating table `{table_id}`: {e}")
33+
LOGGER.error(f"Invalid GCP request when creating table `{full_table_id}`: {e}")
4134
except Exception as e:
42-
LOGGER.error(f"Unexpected error when creating table `{table_id}`: {e}")
35+
LOGGER.error(f"Unexpected error when creating table `{full_table_id}`: {e}")

Diff for: bigquery_python_tutorial/tables.py

+7-12
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,28 @@ def list_all_tables() -> List[Optional[Table]]:
1818
for dataset in gbq.list_datasets():
1919
for listed_table in gbq.list_tables(dataset.dataset_id):
2020
table = get_table(
21-
listed_table.project, listed_table.dataset_id, listed_table.table_id
21+
f"{listed_table.project}.{listed_table.dataset_id}.{listed_table.table_id}"
2222
)
2323
tables.append(table)
2424
return tables
2525

2626

27-
def get_table(project_id: str, dataset_id: str, table_id: str) -> Table:
27+
def get_table(full_table_id: str) -> Table:
2828
"""
2929
Get a single Google BigQuery table.
3030
31-
:param project_id: Google Cloud project ID.
32-
:type project_id: str
33-
:param dataset_id: ID of Google BigQuery dataset.
34-
:type dataset_id: str
35-
:param table_id: ID of Google BigQuery table.
36-
:type table_id: str
31+
:param full_table_id: Full ID of a Google BigQuery table.
32+
:type full_table_id: str
3733
:returns: Table
3834
"""
3935
try:
40-
full_table_id = f"{project_id}.{dataset_id}.{table_id}"
4136
table = gbq.get_table(full_table_id)
42-
LOGGER.info(f"{dataset_id}.{table_id}: {table.num_rows} rows.")
37+
LOGGER.info(f"{table.dataset_id}.{table.table_id}: {table.num_rows} rows.")
4338
return table
4439
except BadRequest as e:
45-
LOGGER.error(f"Invalid GCP request when fetching table `{table_id}`: {e}")
40+
LOGGER.error(f"Invalid GCP request when fetching table `{full_table_id}`: {e}")
4641
except Exception as e:
47-
LOGGER.error(f"Unexpected error when fetching table `{table_id}`: {e}")
42+
LOGGER.error(f"Unexpected error when fetching table `{full_table_id}`: {e}")
4843

4944

5045
def get_table_schema(table: Table) -> Sequence[Union[SchemaField, Mapping[str, Any]]]:

Diff for: config.py

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
# Google BigQuery
1414
GCP_BIGQUERY_TABLE_ID: str = getenv("GCP_BIGQUERY_TABLE_ID")
1515
GCP_BIGQUERY_DATASET_ID: str = getenv("GCP_BIGQUERY_DATASET_ID")
16+
GCP_BIGQUERY_FULL_TABLE_ID = (
17+
f"{GCP_PROJECT_ID}.{GCP_BIGQUERY_DATASET_ID}.{GCP_BIGQUERY_TABLE_ID}"
18+
)
1619

1720
# Google Cloud Storage
1821
GCP_BUCKET_URI: str = getenv("GCP_BUCKET_URI")

0 commit comments

Comments
 (0)