Files
toolshed-extensions/databricks/index.sh
T

42 lines
2.2 KiB
Bash

#!/bin/sh
# index.sh PROFILE OUT writes what a workspace has to OUT, a line each, tab-separated: its kind
# (catalog, schema, table, volume or job), its name, its type (a job's: its id) and comment.
# Unity Catalog has no call that lists everything, so it's walked a catalog, then a schema, at a
# time, several at once; the jobs take a call of their own. OUT is swapped in whole when done, so
# a search never reads half of it; a failure says why on stderr.
P="$1"
OUT="$2"
T="$OUT.parts"
export P T
rm -rf "$T"
mkdir -p "$T"
trap 'rm -rf "$T"' EXIT
# Every catalog has an information_schema of the same forty views, which would bury the rest.
if ! databricks catalogs list -p "$P" -o json 2>"$T/error" |
jq -r '.[] | select(.name != "__databricks_internal") | [.name, .catalog_type // "", (.comment // "" | gsub("\\s+"; " "))] | @tsv' >"$T/catalogs"; then
grep -v "aitools" "$T/error" >&2
exit 1
fi
awk -F'\t' '{ print "catalog\t" $1 "\t" $2 "\t" $3 }' "$T/catalogs" >"$T/a"
cut -f1 "$T/catalogs" | xargs -P 8 -I{} sh -c '
databricks schemas list "$1" -p "$P" -o json 2>/dev/null |
jq -r ".[] | select(.name != \"information_schema\") | [\"schema\", .full_name, \"\", (.comment // \"\" | gsub(\"\\\\s+\"; \" \"))] | @tsv" >"$T/s.$1"
databricks tables list-summaries "$1" -p "$P" -o json 2>/dev/null |
jq -r ".[] | select(.full_name | contains(\".information_schema.\") | not) | [\"table\", .full_name, .table_type // \"\", \"\"] | @tsv" >"$T/t.$1"
' sh {}
# Volumes are listed a schema at a time, and only managed catalogs have them.
awk -F'\t' '$2 == "MANAGED_CATALOG" { print $1 }' "$T/catalogs" >"$T/managed"
cat "$T"/s.* 2>/dev/null | awk -F'\t' 'NR == FNR { m[$1] = 1; next } { split($2, n, "."); if (m[n[1]]) print $2 }' "$T/managed" - |
xargs -P 8 -I{} sh -c '
databricks volumes list "${1%%.*}" "${1#*.}" -p "$P" -o json 2>/dev/null |
jq -r ".[]? | [\"volume\", .full_name, .volume_type // \"\", (.comment // \"\" | gsub(\"\\\\s+\"; \" \"))] | @tsv" >"$T/v.$1"
' sh {}
databricks jobs list -p "$P" -o json 2>/dev/null |
jq -r '.[] | ["job", .settings.name // "", (.job_id | tostring), ""] | @tsv' >"$T/jobs"
cat "$T/a" "$T"/s.* "$T"/t.* "$T"/v.* "$T/jobs" 2>/dev/null >"$OUT.tmp" && mv "$OUT.tmp" "$OUT"