From 61f439493fcf24d8d9a29362a8498b38389686ef Mon Sep 17 00:00:00 2001 From: Adler Santos Date: Thu, 24 Jun 2021 17:38:14 -0400 Subject: [PATCH] feat: Onboard BLS - CPSAAT 2020 dataset (#105) * bls.cpsaat18 YAML config * generate terraform files * generate DAG * set schedule to yearly * set source_objects to match all CSV files * use multiline YAML block for BLS dataset description --- datasets/bls/_terraform/bls_dataset.tf | 35 +++++++++ datasets/bls/_terraform/cpsaat18_pipeline.tf | 36 +++++++++ datasets/bls/_terraform/provider.tf | 28 +++++++ datasets/bls/_terraform/variables.tf | 23 ++++++ datasets/bls/cpsaat18/cpsaat18_dag.py | 67 ++++++++++++++++ datasets/bls/cpsaat18/pipeline.yaml | 80 ++++++++++++++++++++ datasets/bls/dataset.yaml | 37 +++++++++ 7 files changed, 306 insertions(+) create mode 100644 datasets/bls/_terraform/bls_dataset.tf create mode 100644 datasets/bls/_terraform/cpsaat18_pipeline.tf create mode 100644 datasets/bls/_terraform/provider.tf create mode 100644 datasets/bls/_terraform/variables.tf create mode 100644 datasets/bls/cpsaat18/cpsaat18_dag.py create mode 100644 datasets/bls/cpsaat18/pipeline.yaml create mode 100644 datasets/bls/dataset.yaml diff --git a/datasets/bls/_terraform/bls_dataset.tf b/datasets/bls/_terraform/bls_dataset.tf new file mode 100644 index 000000000..aa67382d2 --- /dev/null +++ b/datasets/bls/_terraform/bls_dataset.tf @@ -0,0 +1,35 @@ +/** + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +resource "google_bigquery_dataset" "bls" { + dataset_id = "bls" + project = var.project_id + description = "Overview: This dataset includes economic statistics on inflation, prices, unemployment, and pay \u0026 benefits provided by the Bureau of Labor Statistics (BLS).\n\nUpdate frequency: Monthly\n\nDataset source: U.S. Bureau of Labor Statistics\n\nTerms of use: This dataset is publicly available for anyone to use under the following terms provided by the Dataset Source -\u00a0http://www.data.gov/privacy-policy#data_policy\u00a0- and is provided \"AS IS\" without any warranty, express or implied, from Google. Google disclaims all liability for any damages, direct or indirect, resulting from the use of the dataset.\n\nSee the GCP Marketplace listing for more details and sample queries: https://console.cloud.google.com/marketplace/details/bls-public-data/bureau-of-labor-statistics" +} + +output "bigquery_dataset-bls-dataset_id" { + value = google_bigquery_dataset.bls.dataset_id +} + +resource "google_storage_bucket" "bls" { + name = "${var.bucket_name_prefix}-bls" + force_destroy = true +} + +output "storage_bucket-bls-name" { + value = google_storage_bucket.bls.name +} diff --git a/datasets/bls/_terraform/cpsaat18_pipeline.tf b/datasets/bls/_terraform/cpsaat18_pipeline.tf new file mode 100644 index 000000000..ee26b2c32 --- /dev/null +++ b/datasets/bls/_terraform/cpsaat18_pipeline.tf @@ -0,0 +1,36 @@ +/** + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +resource "google_bigquery_table" "cpsaat18" { + project = var.project_id + dataset_id = "bls" + table_id = "cpsaat18" + + description = "Current population survey 18: Employed persons by detailed industry, sex, race, and Hispanic or Latino ethnicity" + + depends_on = [ + google_bigquery_dataset.bls + ] +} + +output "bigquery_table-cpsaat18-table_id" { + value = google_bigquery_table.cpsaat18.table_id +} + +output "bigquery_table-cpsaat18-id" { + value = google_bigquery_table.cpsaat18.id +} diff --git a/datasets/bls/_terraform/provider.tf b/datasets/bls/_terraform/provider.tf new file mode 100644 index 000000000..23ab87dcd --- /dev/null +++ b/datasets/bls/_terraform/provider.tf @@ -0,0 +1,28 @@ +/** + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +provider "google" { + project = var.project_id + impersonate_service_account = var.impersonating_acct + region = var.region +} + +data "google_client_openid_userinfo" "me" {} + +output "impersonating-account" { + value = data.google_client_openid_userinfo.me.email +} diff --git a/datasets/bls/_terraform/variables.tf b/datasets/bls/_terraform/variables.tf new file mode 100644 index 000000000..c3ec7c506 --- /dev/null +++ b/datasets/bls/_terraform/variables.tf @@ -0,0 +1,23 @@ +/** + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +variable "project_id" {} +variable "bucket_name_prefix" {} +variable "impersonating_acct" {} +variable "region" {} +variable "env" {} + diff --git a/datasets/bls/cpsaat18/cpsaat18_dag.py b/datasets/bls/cpsaat18/cpsaat18_dag.py new file mode 100644 index 000000000..3a655fa7d --- /dev/null +++ b/datasets/bls/cpsaat18/cpsaat18_dag.py @@ -0,0 +1,67 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from airflow import DAG +from airflow.contrib.operators import gcs_to_bq + +default_args = { + "owner": "Google", + "depends_on_past": False, + "start_date": "2021-06-23", +} + + +with DAG( + dag_id="bls.cpsaat18", + default_args=default_args, + max_active_runs=1, + schedule_interval="@yearly", + catchup=False, + default_view="graph", +) as dag: + + # Task to load the CPSAAT18 data to the BigQuery table + load_csv_to_bq = gcs_to_bq.GoogleCloudStorageToBigQueryOperator( + task_id="load_csv_to_bq", + bucket="{{ var.json.bls.source_bucket }}", + source_objects=["cpsaat18/*.csv"], + source_format="CSV", + destination_project_dataset_table="bls.cpsaat18", + skip_leading_rows=1, + write_disposition="WRITE_TRUNCATE", + schema_fields=[ + {"name": "year", "type": "integer", "nullable": False}, + {"name": "sector", "type": "string", "nullable": False}, + {"name": "subsector", "type": "string", "nullable": True}, + {"name": "industry_group", "type": "string", "nullable": True}, + {"name": "industry", "type": "string", "nullable": True}, + { + "name": "total_employed_in_thousands", + "type": "integer", + "nullable": True, + }, + {"name": "percent_women", "type": "float", "nullable": True}, + {"name": "percent_white", "type": "float", "nullable": True}, + { + "name": "percent_black_or_african_american", + "type": "float", + "nullable": True, + }, + {"name": "percent_asian", "type": "float", "nullable": True}, + {"name": "percent_hispanic_or_latino", "type": "float", "nullable": True}, + ], + ) + + load_csv_to_bq diff --git a/datasets/bls/cpsaat18/pipeline.yaml b/datasets/bls/cpsaat18/pipeline.yaml new file mode 100644 index 000000000..918756048 --- /dev/null +++ b/datasets/bls/cpsaat18/pipeline.yaml @@ -0,0 +1,80 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +--- +resources: + - type: bigquery_table + table_id: cpsaat18 + description: "Current population survey 18: Employed persons by detailed industry, sex, race, and Hispanic or Latino ethnicity" + +dag: + initialize: + dag_id: cpsaat18 + default_args: + owner: "Google" + depends_on_past: False + start_date: '2021-06-23' + max_active_runs: 1 + schedule_interval: "@yearly" + catchup: False + default_view: graph + + tasks: + - operator: "GoogleCloudStorageToBigQueryOperator" + description: "Task to load the CPSAAT18 data to the BigQuery table" + args: + task_id: "load_csv_to_bq" + bucket: "{{ var.json.bls.source_bucket }}" + source_objects: ["cpsaat18/*.csv"] + source_format: "CSV" + destination_project_dataset_table: "bls.cpsaat18" + skip_leading_rows: 1 + write_disposition: "WRITE_TRUNCATE" + schema_fields: + - name: "year" + type: "integer" + nullable: false + - name: "sector" + type: "string" + nullable: false + - name: "subsector" + type: "string" + nullable: true + - name: "industry_group" + type: "string" + nullable: true + - name: "industry" + type: "string" + nullable: true + - name: "total_employed_in_thousands" + type: "integer" + nullable: true + - name: "percent_women" + type: "float" + nullable: true + - name: "percent_white" + type: "float" + nullable: true + - name: "percent_black_or_african_american" + type: "float" + nullable: true + - name: "percent_asian" + type: "float" + nullable: true + - name: "percent_hispanic_or_latino" + type: "float" + nullable: true + + graph_paths: + - "load_csv_to_bq" diff --git a/datasets/bls/dataset.yaml b/datasets/bls/dataset.yaml new file mode 100644 index 000000000..c2326d5b1 --- /dev/null +++ b/datasets/bls/dataset.yaml @@ -0,0 +1,37 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +dataset: + name: bls + friendly_name: ~ + description: ~ + dataset_sources: ~ + terms_of_use: ~ + + +resources: + - type: bigquery_dataset + dataset_id: bls + description: |- + Overview: This dataset includes economic statistics on inflation, prices, unemployment, and pay & benefits provided by the Bureau of Labor Statistics (BLS). + + Update frequency: Monthly + + Dataset source: U.S. Bureau of Labor Statistics + + Terms of use: This dataset is publicly available for anyone to use under the following terms provided by the Dataset Source - http://www.data.gov/privacy-policy#data_policy - and is provided "AS IS" without any warranty, express or implied, from Google. Google disclaims all liability for any damages, direct or indirect, resulting from the use of the dataset. + + See the GCP Marketplace listing for more details and sample queries: https://console.cloud.google.com/marketplace/details/bls-public-data/bureau-of-labor-statistics + - type: storage_bucket + name: bls