Last active
October 25, 2024 11:59
-
-
Save huevos-y-bacon/53e166ce72dbe5d58c8167bfff989b97 to your computer and use it in GitHub Desktop.
AWS Athena - Export Named Queries
This file contains 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
#!/usr/bin/env bash | |
WORKGROUP=inspector # Optional | |
if [ -n "${WORKGROUP}" ]; then | |
mkdir -p ${WORKGROUP} | |
WG="--work-group ${WORKGROUP}" | |
DIR="${WORKGROUP}" | |
else | |
WG="" | |
DIR="." | |
fi | |
for nq in $(aws athena list-named-queries ${WG} --query 'NamedQueryIds' --out text); do | |
json=$(aws athena get-named-query --named-query-id ${nq} --query 'NamedQuery.[Name,QueryString]' --out json) | |
name=$(echo ${json} | jq -r '.[0]') | |
# Replace spaces and hyphens with underscores | |
name=$(echo ${name} | tr ' -' '_') | |
echo "Exporting ${name}..." | |
echo ${json} | jq -r '.[1]' > "${DIR}/${name}.sql" | |
done |
This file contains 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
#!/usr/bin/env bash | |
WORKGROUP=inspector # Optional | |
DATABASE=inspector # Optional | |
if [ -n "${WORKGROUP}" ] | |
then WG="--work-group ${WORKGROUP}"; DIR="${WORKGROUP}" | |
else WG=""; DIR="." | |
fi | |
if [ -n "${DATABASE}" ]; | |
then DB="--database ${DATABASE}" | |
else DB="--database default" | |
fi | |
# for each .sql file, create a named query | |
for f in "${DIR}"/*.sql; do | |
name=$(basename ${f} .sql) | |
echo "Creating Athena Query ${name}..." | |
query=$(cat ${f}) | |
aws athena create-named-query --name "${name}" --query-string "${query}" ${WG} ${DB} | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment