Single purpose ETL script to run SQL queries and publish the results in a native Google Sheet.
2.3K
This a tool that runs a series of .sql files within a Google drive folder, and writes the results as a native google sheet into another folder. This serves as a simple ETL tool that the spreadsheet type people can then slice the data how they like. A lot of the code is actually copy/paste from from various Google API examples, we're just piecing it together.
client_secrets.json file you downloaded in the directory with
loader.py$pip install -r requirements.txtMYSQL_HOST=db.example.comMYSQL_PORT=3306MYSQL_DB=my_databaseMYSQL_USER=db_userMYSQL_PASSWD=ASweetPasswordSQL_SOURCE=0B_jczERcXKwsUEt5dGtrV1h4Y1E
SHEET_DEST=0B_jczERcXKwsYzVsNHFIMDktZ2cCreate a plain text file with the .sql extension in the SQL_SOURCE, then run the loader.py script.
The script will iterate through all .sql files in the SQL_SOURCE folder, execute the query contained
in the file, then create a native Google Sheet in the SHEET_DEST folder containing the results of the query.
It can also be ran easily (in cases such as CRON) within a wrapper script like so...
#!/bin/bash
cd /opt/SheetsETL
source venv/bin/activate
python loader.py
Additionally there is a Docker image stickystyle/sheetsetl that can be used. Simply pull the image and start a container with the appropriate env vars set.
$docker run -it --rm --name SheetsETL \
MYSQL_HOST=db.example.com \
MYSQL_PORT=3306 \
MYSQL_DB=my_database \
MYSQL_USER=db_user \
MYSQL_PASSWD=ASweetPassword \
SQL_SOURCE=0B_jczERcXKwsUEt5dGtrV1h4Y1E \
SHEET_DEST=0B_jczERcXKwsYzVsNHFIMDktZ2c \
stickystyle/sheetsetl
Google sheets has a limitation of two million cells per spreadsheet, this includes all of the sheets ('tabs') that exist in the spreadsheet. So be mindful of the amount of rows and columns your query returns.
With this tool, you are enabling anyone that has access to the SQL_SOURCE folder to be able to write queries to your server, you need to think about your data security before you distribuite access to this tool.
My personal deployment has MYSQL_HOST being a read-only
replica of my main database, and the database defined in MYSQL_DB is a demoralized copy
of only the data that is needed by the annalists at my company. This eliminates the ability for someone
to write statements that can change data, and we ensure that users are not able to select data we don't want
them to.
The first time loader.py is ran you will be prompted to authorize the the application with your google account, the script currently needs three permission scopes as documented here.
https://www.googleapis.com/auth/drive.metadata
https://www.googleapis.com/auth/drive.file
https://www.googleapis.com/auth/drive.readonly
Content type
Image
Digest
Size
31.4 MB
Last updated
over 8 years ago
docker pull stickystyle/sheetsetl