Skip to content

Commit 507f60c

Browse files
[io] feat: use GOOGLE_CLOUD_PROJECT to initialize BigQueryWriter
* use default dataset `garf` if not provided
1 parent 9c5c122 commit 507f60c

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

libs/garf_io/garf_io/writers/bigquery_writer.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
"""Module for writing data to BigQuery."""
14+
"""Writes GarfReport to BigQuery."""
1515

1616
from __future__ import annotations
1717

18+
import os
19+
1820
try:
1921
from google.cloud import bigquery
2022
except ImportError as e:
2123
raise ImportError(
22-
'Please install garf-io with BigQuery support '
23-
'- `pip install garf-io[bq]`'
24+
'Please install garf-io with BigQuery support - `pip install garf-io[bq]`'
2425
) from e
2526

2627
import datetime
@@ -34,10 +35,14 @@
3435

3536
from garf_core import parsers
3637
from garf_core import report as garf_report
37-
from garf_io import formatter
38+
from garf_io import exceptions, formatter
3839
from garf_io.writers import abs_writer
3940

4041

42+
class BigQueryWriterError(exceptions.GarfIoError):
43+
"""BigQueryWriter specific errors."""
44+
45+
4146
class BigQueryWriter(abs_writer.AbsWriter):
4247
"""Writes Garf Report to BigQuery.
4348
@@ -50,10 +55,10 @@ class BigQueryWriter(abs_writer.AbsWriter):
5055

5156
def __init__(
5257
self,
53-
project: str,
54-
dataset: str,
58+
project: str | None = os.getenv('GOOGLE_CLOUD_PROJECT'),
59+
dataset: str = 'garf',
5560
location: str = 'US',
56-
write_disposition: bigquery.WriteDisposition = (
61+
write_disposition: bigquery.WriteDisposition | str = (
5762
bigquery.WriteDisposition.WRITE_TRUNCATE
5863
),
5964
**kwargs,
@@ -68,9 +73,18 @@ def __init__(
6873
kwargs: Optional keywords arguments.
6974
"""
7075
super().__init__(**kwargs)
76+
if not project:
77+
raise BigQueryWriterError(
78+
'project is required. Either provide it as project parameter '
79+
'or GOOGLE_CLOUD_PROJECT env variable.'
80+
)
7181
self.project = project
7282
self.dataset_id = f'{project}.{dataset}'
7383
self.location = location
84+
if isinstance(write_disposition, str):
85+
write_disposition = getattr(
86+
bigquery.WriteDisposition, write_disposition.upper()
87+
)
7488
self.write_disposition = write_disposition
7589

7690
def __str__(self) -> str:

0 commit comments

Comments
 (0)