dlt.destinations.impl.bigquery.bigquery_adapter
PartitionTransformation Objects
class PartitionTransformation()
template
Template string of the transformation including column name placeholder. E.g. RANGE_BUCKET({column_name}, GENERATE_ARRAY(0, 1000000, 10000))
column_name
Column name to apply the transformation to
bigquery_partition Objects
class bigquery_partition()
Helper class to generate BigQuery partition transformations.
range_bucket
@staticmethod
def range_bucket(column_name: str,
start: int,
end: int,
interval: int = 1) -> PartitionTransformation
Partition by an integer column with the specified range, where:
Arguments:
column_name
- The column to partition bystart
- The start of range partitioning (inclusive)end
- The end of the range partitioning (exclusive)interval
- The width of each range within the partition (default: 1)
Returns:
A PartitionTransformation object for integer range partitioning
bigquery_adapter
def bigquery_adapter(
data: Any,
partition: Union[TColumnNames, PartitionTransformation] = None,
cluster: TColumnNames = None,
round_half_away_from_zero: TColumnNames = None,
round_half_even: TColumnNames = None,
table_description: Optional[str] = None,
table_expiration_datetime: Optional[str] = None,
insert_api: Optional[Literal["streaming", "default"]] = None,
autodetect_schema: Optional[bool] = None,
partition_expiration_days: Optional[int] = None) -> DltResource
Prepares data for loading into BigQuery.
This function takes data, which can be raw or already wrapped in a DltResource object, and prepares it for BigQuery by optionally specifying partitioning, clustering, table description and table expiration settings.
Arguments:
data
Any - The data to be transformed. This can be raw data or an instance of DltResource. If raw data is provided, the function will wrap it into aDltResource
object.partition
Union[TColumnNames, PartitionTransformation], optional - The column to partition the BigQuery table by. This can be a string representing a single column name for simple partitioning, or a PartitionTransformation object for advanced partitioning. Use the bigquery_partition helper class to create transformation objects.cluster
TColumnNames, optional - A column name or list of column names to cluster the BigQuery table by.round_half_away_from_zero
TColumnNames, optional - Determines how values in the column are rounded when written to the table. This mode rounds halfway cases away from zero. The columns specified must be mutually exclusive fromround_half_even
. See https://cloud.google.com/bigquery/docs/schemas#rounding_mode for more information.round_half_even
TColumnNames, optional - Determines how values in the column are rounded when written to the table. This mode rounds halfway cases towards the nearest even digit. The columns specified must be mutually exclusive fromround_half_away_from_zero
. See https://cloud.google.com/bigquery/docs/schemas#rounding_mode for more information.table_description
str, optional - A description for the BigQuery table.table_expiration_datetime
str, optional - String representing the datetime when the BigQuery table expires. This is always interpreted as UTC, BigQuery's default.insert_api
Optional[Literal["streaming", "default"]] - The API to use for inserting data into BigQuery. If "default" is chosen, the original SQL query mechanism is used. If "streaming" is chosen, the streaming API (https://cloud.google.com/bigquery/docs/streaming-data-into-bigquery) is used.NOTE
- due to BigQuery features, streaming insert is only available forappend
write_disposition.autodetect_schema
bool, optional - If set to True, BigQuery schema autodetection will be used to create data tables. This allows to create structured types from nested data.partition_expiration_days
int, optional - For date/time based partitions it tells when partition is expired and removed. Partitions are expired based on a partitioned column value. (https://cloud.google.com/bigquery/docs/managing-partitioned-tables#partition-expiration)
Returns:
A DltResource
object that is ready to be loaded into BigQuery.
Raises:
ValueError
- If any hint is invalid or none are specified.
Examples:
data = [{"name": "Marcel", "description": "Raccoon Engineer", "date_hired": 1700784000}]
bigquery_adapter(data, partition="date_hired", table_expiration_datetime="2024-01-30", table_description="Employee Data")
[DltResource with hints applied]
should_autodetect_schema
def should_autodetect_schema(table: PreparedTableSchema) -> bool
Tells if schema should be auto detected for a given prepared table