GCSToBigQueryOperator

Google

Loads files from Google Cloud Storage into BigQuery.

View on GitHub

Last Updated: Mar. 20, 2023

Access Instructions

Install the Google provider package into your Airflow environment.

Import the module into your DAG file and instantiate it with your desired params.

Parameters

bucketRequiredThe bucket to load from. (templated)
source_objectsRequiredString or List of Google Cloud Storage URIs to load from. (templated) If source_format is ‘DATASTORE_BACKUP’, the list must only contain a single URI.
destination_project_dataset_tableRequiredThe dotted (.|:). BigQuery table to load data into. If is not included, project will be the project defined in the connection json. (templated)
schema_fieldsIf set, the schema field list as defined here: https://cloud.google.com/bigquery/docs/reference/v2/jobs#configuration.load Should not be set when source_format is ‘DATASTORE_BACKUP’. Parameter must be defined if ‘schema_object’ is null and autodetect is False.
schema_objectIf set, a GCS object path pointing to a .json file that contains the schema for the table. (templated) Parameter must be defined if ‘schema_fields’ is null and autodetect is False.
schema_object_bucket[Optional] If set, the GCS bucket where the schema object template is stored. (templated) (Default: the value of bucket)
source_formatFile format to export.
compression[Optional] The compression type of the data source. Possible values include GZIP and NONE. The default value is NONE. This setting is ignored for Google Cloud Bigtable, Google Cloud Datastore backups and Avro formats.
create_dispositionThe create disposition if the table doesn’t exist.
skip_leading_rowsThe number of rows at the top of a CSV file that BigQuery will skip when loading the data. When autodetect is on, the behavior is the following: skip_leading_rows unspecified - Autodetect tries to detect headers in the first row. If they are not detected, the row is read as data. Otherwise, data is read starting from the second row. skip_leading_rows is 0 - Instructs autodetect that there are no headers and data should be read starting from the first row. skip_leading_rows = N > 0 - Autodetect skips N-1 rows and tries to detect headers in row N. If headers are not detected, row N is just skipped. Otherwise, row N is used to extract column names for the detected schema. Default value set to None so that autodetect option can detect schema fields.
write_dispositionThe write disposition if the table already exists.
field_delimiterThe delimiter to use when loading from a CSV.
max_bad_recordsThe maximum number of bad records that BigQuery can ignore when running the job.
quote_characterThe value that is used to quote data sections in a CSV file.
ignore_unknown_values[Optional] Indicates if BigQuery should allow extra values that are not represented in the table schema. If true, the extra values are ignored. If false, records with extra columns are treated as bad records, and if there are too many bad records, an invalid error is returned in the job result.
allow_quoted_newlinesWhether to allow quoted newlines (true) or not (false).
allow_jagged_rowsAccept rows that are missing trailing optional columns. The missing values are treated as nulls. If false, records with missing trailing columns are treated as bad records, and if there are too many bad records, an invalid error is returned in the job result. Only applicable to CSV, ignored for other formats.
encodingThe character encoding of the data. See: https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs#configuration.query.tableDefinitions.(key).csvOptions.encoding https://cloud.google.com/bigquery/docs/reference/rest/v2/tables#externalDataConfiguration.csvOptions.encoding
max_id_keyIf set, the name of a column in the BigQuery table that’s to be loaded. This will be used to select the MAX value from BigQuery after the load occurs. The results will be returned by the execute() command, which in turn gets stored in XCom for future operators to use. This can be helpful with incremental loads–during future executions, you can pick up from the max ID.
delegate_toThe account to impersonate using domain-wide delegation of authority, if any. For this to work, the service account making the request must have domain-wide delegation enabled.
schema_update_optionsAllows the schema of the destination table to be updated as a side effect of the load job.
src_fmt_configsconfigure optional fields specific to the source format
external_tableFlag to specify if the destination table should be a BigQuery external table. Default Value is False.
time_partitioningconfigure optional time partitioning fields i.e. partition by field, type and expiration as per API specifications. Note that ‘field’ is not available in concurrency with dataset.table$partition.
cluster_fieldsRequest that the result of this load be stored sorted by one or more columns. BigQuery supports clustering for both partitioned and non-partitioned tables. The order of columns given determines the sort order. Not applicable for external tables.
autodetect[Optional] Indicates if we should automatically infer the options and schema for CSV and JSON sources. (Default: True). Parameter must be set to True if ‘schema_fields’ and ‘schema_object’ are undefined. It is suggested to set to True if table are create outside of Airflow. If autodetect is None and no schema is provided (neither via schema_fields nor a schema_object), assume the table already exists.
encryption_configuration[Optional] Custom encryption configuration (e.g., Cloud KMS keys). Example: encryption_configuration = { "kmsKeyName": "projects/testp/locations/us/keyRings/test-kr/cryptoKeys/test-key" }
location[Optional] The geographic location of the job. Required except for US and EU. See details at https://cloud.google.com/bigquery/docs/locations#specifying_your_location
impersonation_chainOptional service account to impersonate using short-term credentials, or chained list of accounts required to get the access_token of the last account in the list, which will be impersonated in the request. If set as a string, the account must grant the originating account the Service Account Token Creator IAM role. If set as a sequence, the identities from the list must grant Service Account Token Creator IAM role to the directly preceding identity, with first account from the list granting this role to the originating account (templated).
labels[Optional] Labels for the BiqQuery table.
description[Optional] Description for the BigQuery table. This will only be used if the destination table is newly created. If the table already exists and a value different than the current description is provided, the job will fail.
deferrableRun operator in the deferrable mode

Documentation

Loads files from Google Cloud Storage into BigQuery.

The schema to be used for the BigQuery table may be specified in one of two ways. You may either directly pass the schema fields in, or you may point the operator to a Google Cloud Storage object name. The object in Google Cloud Storage must be a JSON file with the schema fields in it.

See also

For more information on how to use this operator, take a look at the guide: Operator

Was this page helpful?