-
Notifications
You must be signed in to change notification settings - Fork 7
NHS Import to BQ #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
luccasmmg
wants to merge
7
commits into
master
Choose a base branch
from
nhs2.11-import-bq
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
NHS Import to BQ #137
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
87a6a60
NHS Import to BQ
luccasmmg b0859e9
New task as well
luccasmmg 832667c
Add safeguard
luccasmmg 4358c47
v2 schema detection
luccasmmg 58bfb30
Add logging
luccasmmg 3a2c848
Infer from schema on error
luccasmmg e9f14b7
Update bg handler
luccasmmg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
|
|
||
| import logging | ||
| import time | ||
| import json | ||
| import ast | ||
| from datetime import date, datetime | ||
|
|
||
| # Local imports | ||
| from aircan.dependencies.google_cloud.bigquery_handler import bq_import_csv | ||
|
|
||
| # Third-party library imports | ||
| from airflow import DAG | ||
| from airflow.exceptions import AirflowException | ||
|
|
||
| from airflow.models import Variable | ||
| from airflow.operators.python_operator import PythonOperator | ||
| from airflow.utils.dates import days_ago | ||
|
|
||
|
|
||
| args = { | ||
| 'start_date': days_ago(0), | ||
| 'params': { | ||
| "resource": { | ||
| "path": "path/to/my.csv", | ||
| "format": "CSV", | ||
| "ckan_resource_id": "res-id-123", | ||
| "schema": { | ||
| "fields": "['field1', 'field2']" | ||
| } | ||
| }, | ||
| "ckan_config": { | ||
| "api_key": "API_KEY", | ||
| "site_url": "URL", | ||
| }, | ||
| "big_query": { | ||
| "bq_project_id": "bigquery_project_id", | ||
| "bq_dataset_id": "bigquery_dataset_id" | ||
| }, | ||
| "output_bucket": str(date.today()) | ||
| } | ||
| } | ||
|
|
||
| dag = DAG( | ||
| dag_id='ckan_api_import_to_bq_v2', | ||
| default_args=args, | ||
| schedule_interval=None | ||
| ) | ||
|
|
||
| def task_import_resource_to_bq(**context): | ||
| logging.info('Invoking import resource to bigquery') | ||
| logging.info("resource: {}".format(context['params'].get('resource', {}))) | ||
|
|
||
| gc_file_url = context['params'].get('big_query', {}).get('gcs_uri') | ||
| bq_project_id = context['params'].get('big_query', {}).get('bq_project_id') | ||
| bq_dataset_id = context['params'].get('big_query', {}).get('bq_dataset_id') | ||
| bq_table_name = context['params'].get('big_query', {}).get('bq_table_name') | ||
| logging.info("bq_table_name: {}".format(bq_table_name)) | ||
|
|
||
| raw_schema = context['params'].get('resource', {}).get('schema') | ||
| eval_schema = json.loads(raw_schema) | ||
| schema = eval_schema.get('fields') | ||
| logging.info("SCHEMA: {}".format(schema)) | ||
|
|
||
| # sample bq_table_id: "bigquerytest-271707.nhs_test.dag_test" | ||
| bq_table_id = '%s.%s.%s' % (bq_project_id, bq_dataset_id, bq_table_name) | ||
| logging.info('Importing %s to BQ %s' % (gc_file_url, bq_table_id)) | ||
| ckan_conf = context['params'].get('ckan_config', {}) | ||
| ckan_conf['resource_id'] = context['params'].get('resource', {}).get('ckan_resource_id') | ||
| bq_import_csv(bq_table_id, gc_file_url, schema, ckan_conf) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import_resource_to_bq_task = PythonOperator( | ||
| task_id='import_resource_to_bq_v2', | ||
| provide_context=True, | ||
| python_callable=task_import_resource_to_bq, | ||
| dag=dag, | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.