In this article I will explain how you can quickly download list of tables from Oracle database into csv format and copy them into google cloud store.
Required Software
- gsutil: This is a command-line tool for interacting with GCS. You can download and install it from the Google Cloud SDK.
- SQLcl: This is a command-line interface for interacting with Oracle databases. You can download it from the Oracle Technology Network.
Instructions
- Install gsutil and SQLcl on your local machine.
- Create a bash script named
copy_tables_gcs.shin thesqlcl/binfolder of your local machine. - Add the following code to the
copy_tables_gcs.shscript:
#!/bin/bash
while read value
do
echo "Processing $value"
echo "SET TERM OFF" > command.sql
echo "SET FEED OFF" >> command.sql
echo "SET SQLFORMAT CSV" >> command.sql
echo "UNLOAD $value" >> command.sql
echo "EXIT" >> command.sql
wc -l $value*.csv
rm -rf $value*.csv
./sql $CONN_STR @command.sql
gsutil cp $value*.csv $GCS_BUCKET
done < table_list.txt
This script exports each table listed in the table_list.txt file to a CSV file, copies the CSV file to the specified GCS bucket, and deletes the local CSV file.
- Create a file named
table_list.txtin thesqlcl/binfolder and add the names of all the tables that need to be exported to CSV.
table1
table2
- Set the database connection details in an environment variable named
CONN_STR.
export CONN_STR=username/password@hostname:portnumber/service_name
- Set the name of the GCS bucket in an environment variable named
GCS_BUCKET.
export GCS_BUCKET=your-bucket-name
- Make the
copy_tables_gcs.shscript executable.
chmod 755 copy_tables_gcs.sh
- Execute the
copy_tables_gcs.shscript in the background using the following command:
nohup ./copy_tables_gcs.sh > one_time_copy.log 2>&1 &
This command runs the script in the background, redirects the output to the one_time_copy.log file, and detaches the script from the terminal.
This article was originally published at https://medium.com/@aradsouza/using-sqlcl-bulk-export-oracle-table-into-csv-and-load-into-gcs-a5b784ac3bdf
