Created
December 8, 2016 15:35
-
-
Save larruda/0efd7c1ba872b972bf4512b905e45930 to your computer and use it in GitHub Desktop.
Bash script to import data into a partitioned table on BigQuery from another table using a DATE field as a filter.
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
#!/bin/bash | |
#set -x | |
oldifs="$IFS" | |
IFS=$'\n' | |
echo -n "Type in the project id and press [ENTER]: " | |
read project_id | |
echo -n "Type in the dataset name and press [ENTER]: " | |
read dataset | |
echo -n "Type in the source table name and press [ENTER]: " | |
read source_table | |
echo -n "Type in the destination partitioned table and press [ENTER]: " | |
read dest_table | |
echo -n "Type in the DATE column name to be used as partition and press [ENTER]: " | |
read part_column | |
declare dates=($(bq query \ | |
--use_legacy_sql=false \ | |
--format=csv \ | |
"SELECT DISTINCT ${part_column} FROM \`${project_id}.${dataset}.${source_table}\`" \ | |
| tail -n +3)) | |
echo -e "\n" | |
for partition in ${dates[@]}; do | |
echo "Importing partition date $partition ..." | |
bq query --format=none \ | |
--use_legacy_sql=false \ | |
--allow_large_results \ | |
--replace \ | |
--noflatten_results \ | |
--destination_table "${dataset}.${dest_table}\$${partition//-/}" \ | |
"SELECT * FROM \`${project_id}.${dataset}.${source_table}\` WHERE ${part_column}=CAST('${partition}' AS DATE)" | |
done; | |
echo "Import finished." | |
IFS="$oldifs" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment