|
| 1 | +import pandas as pd |
| 2 | +import streamlit as st |
| 3 | +import datetime |
| 4 | +from databricks.sdk import WorkspaceClient |
| 5 | + |
| 6 | + |
| 7 | +w = WorkspaceClient() |
| 8 | + |
| 9 | + |
| 10 | +def get_catalogs(): |
| 11 | + catalogs = w.catalogs.list() |
| 12 | + |
| 13 | + catalogs_data = [] |
| 14 | + for catalog in catalogs: |
| 15 | + catalogs_data.append( |
| 16 | + { |
| 17 | + "Catalog name": catalog.name, |
| 18 | + "Owner": catalog.owner, |
| 19 | + "Comment": catalog.comment, |
| 20 | + "Created at": datetime.datetime.fromtimestamp( |
| 21 | + catalog.created_at / 1000 |
| 22 | + ), |
| 23 | + "Updated at": datetime.datetime.fromtimestamp( |
| 24 | + catalog.updated_at / 1000 |
| 25 | + ), |
| 26 | + } |
| 27 | + ) |
| 28 | + return pd.DataFrame(catalogs_data) |
| 29 | + |
| 30 | + |
| 31 | +def get_catalog_names(): |
| 32 | + catalogs = w.catalogs.list() |
| 33 | + return [catalog.name for catalog in catalogs] |
| 34 | + |
| 35 | + |
| 36 | +def get_schemas_for_catalog(catalog_name): |
| 37 | + schema_data = [] |
| 38 | + schemas = w.schemas.list(catalog_name=catalog_name, max_results=10) |
| 39 | + for schema in schemas: |
| 40 | + schema_data.append( |
| 41 | + { |
| 42 | + "Catalog name": schema.catalog_name, |
| 43 | + "Catalog type": schema.catalog_type, |
| 44 | + "Schema name": schema.full_name, |
| 45 | + "Owner": schema.owner, |
| 46 | + "Comment": schema.comment, |
| 47 | + "Created at": datetime.datetime.fromtimestamp(schema.created_at / 1000) |
| 48 | + if schema.created_at |
| 49 | + else None, |
| 50 | + "Updated at": datetime.datetime.fromtimestamp(schema.updated_at / 1000) |
| 51 | + if schema.updated_at |
| 52 | + else None, |
| 53 | + "Effective predictive optimization": schema.effective_predictive_optimization_flag, |
| 54 | + "Properties": schema.properties, |
| 55 | + } |
| 56 | + ) |
| 57 | + return pd.DataFrame(schema_data) |
| 58 | + |
| 59 | + |
| 60 | +st.header(body="Unity Catalog", divider=True) |
| 61 | +st.subheader("Get catalog and schema information") |
| 62 | +st.write("This receipt lists metadata for catalogs and schemas in Unity Catalog.") |
| 63 | + |
| 64 | +tab_a, tab_b, tab_c = st.tabs(["**Try it**", "**Code snippets**", "**Requirements**"]) |
| 65 | + |
| 66 | +with tab_a: |
| 67 | + if st.button("Get catalogs"): |
| 68 | + st.session_state["catalogs_df"] = get_catalogs() |
| 69 | + st.session_state["catalog_names"] = get_catalog_names() |
| 70 | + |
| 71 | + if "catalogs_df" in st.session_state: |
| 72 | + st.write("### Catalogs") |
| 73 | + st.dataframe(st.session_state["catalogs_df"]) |
| 74 | + |
| 75 | + st.write("### Select a Catalog to View its Schemas") |
| 76 | + selected_catalog = st.selectbox( |
| 77 | + "Choose a catalog", options=st.session_state["catalog_names"] |
| 78 | + ) |
| 79 | + |
| 80 | + if st.button("Get schemas for selected catalog"): |
| 81 | + schemas_df = get_schemas_for_catalog(selected_catalog) |
| 82 | + st.write(f"### Schemas for {selected_catalog}") |
| 83 | + if not schemas_df.empty: |
| 84 | + st.dataframe(schemas_df) |
| 85 | + else: |
| 86 | + st.info(f"No schemas found in the catalog '{selected_catalog}'") |
| 87 | + |
| 88 | + |
| 89 | +table = [ |
| 90 | + { |
| 91 | + "type": "Get Catalog", |
| 92 | + "param": "get_catalog", |
| 93 | + "description": "Get the catalogs.", |
| 94 | + "code": """ |
| 95 | + ```python |
| 96 | + from databricks.sdk import WorkspaceClient |
| 97 | +
|
| 98 | + |
| 99 | + w = WorkspaceClient() |
| 100 | +
|
| 101 | + def get_catalogs(): |
| 102 | + catalogs = w.catalogs.list() |
| 103 | + # Parse metadata into a list of dictionaries |
| 104 | + catalogs_data = [] |
| 105 | + for catalog in catalogs: |
| 106 | + catalogs_data.append({ |
| 107 | + "Catalog Name": catalog.name, |
| 108 | + "Owner": catalog.owner, |
| 109 | + "Comment": catalog.comment, |
| 110 | + "Created At": datetime.datetime.fromtimestamp(catalog.created_at/1000), |
| 111 | + "Updated At": datetime.datetime.fromtimestamp(catalog.updated_at/1000), |
| 112 | + }) |
| 113 | + return pd.DataFrame(catalogs_data) |
| 114 | + st.write('### Databricks Catalogs') |
| 115 | + st.dataframe(get_catalogs()) |
| 116 | + ``` |
| 117 | + """, |
| 118 | + }, |
| 119 | + { |
| 120 | + "type": "Get Schemas for Selected Catalog", |
| 121 | + "param": "get_schemas_for_catalog", |
| 122 | + "description": "Get the schemas for a specific catalog", |
| 123 | + "code": """ |
| 124 | + ```python |
| 125 | + from databricks.sdk import WorkspaceClient |
| 126 | + |
| 127 | +
|
| 128 | + w = WorkspaceClient() |
| 129 | + |
| 130 | + def get_catalog_names(): |
| 131 | + catalogs = w.catalogs.list() |
| 132 | + return [catalog.name for catalog in catalogs] |
| 133 | + |
| 134 | + def get_schemas_for_catalog(catalog_name): |
| 135 | + schema_data = [] |
| 136 | + schemas = w.schemas.list(catalog_name=catalog_name, max_results=10) |
| 137 | + for schema in schemas: |
| 138 | + schema_data.append({ |
| 139 | + "Catalog Name": schema.catalog_name, |
| 140 | + "Catalog Type": schema.catalog_type, |
| 141 | + "Schema Name": schema.full_name, |
| 142 | + "Owner": schema.owner, |
| 143 | + "Comment": schema.comment, |
| 144 | + "Created At": datetime.datetime.fromtimestamp(schema.created_at/1000) if schema.created_at else None, |
| 145 | + "Updated At": datetime.datetime.fromtimestamp(schema.updated_at/1000) if schema.updated_at else None, |
| 146 | + "Effective Predictive Optimization": schema.effective_predictive_optimization_flag, |
| 147 | + "Properties": schema.properties |
| 148 | + }) |
| 149 | + return pd.DataFrame(schema_data) |
| 150 | + |
| 151 | + # In the UI: |
| 152 | + selected_catalog = st.selectbox("Choose a catalog", options=get_catalog_names()) |
| 153 | + if st.button("Get Schemas"): |
| 154 | + schemas = get_schemas_for_catalog(selected_catalog) |
| 155 | + st.dataframe(schemas) |
| 156 | + ``` |
| 157 | + """, |
| 158 | + }, |
| 159 | +] |
| 160 | + |
| 161 | +with tab_b: |
| 162 | + for i, row in enumerate(table): |
| 163 | + with st.expander(f"**{row['type']} ({row['param']})**", expanded=(i == 0)): |
| 164 | + st.markdown(f"**Description**: {row['description']}") |
| 165 | + st.markdown(row["code"]) |
| 166 | + |
| 167 | +with tab_c: |
| 168 | + st.info(""" |
| 169 | + To list all catalogs, you need the [metastore admin](https://docs.databricks.com/aws/en/data-governance/unity-catalog/manage-privileges/admin-privileges#metastore-admins) role. |
| 170 | + Otherwise, only catalogs for which you have the `USE_CATALOG` permission will be retrieved. |
| 171 | + """) |
| 172 | + |
| 173 | + col1, col2, col3 = st.columns(3) |
| 174 | + |
| 175 | + with col1: |
| 176 | + st.markdown(""" |
| 177 | + **Permissions (app service principal)** |
| 178 | + * `USE_CATALOG` on the Unity Catalog catalogs to list |
| 179 | + * `USE_SCHEMA` on the schemas you want to view |
| 180 | + """) |
| 181 | + with col2: |
| 182 | + st.markdown(""" |
| 183 | + **Databricks resources** |
| 184 | + * Unity Catalog enabled workspace |
| 185 | + """) |
| 186 | + with col3: |
| 187 | + st.markdown(""" |
| 188 | + **Dependencies** |
| 189 | + * [Databricks SDK](https://pypi.org/project/databricks-sdk/) - `databricks-sdk` |
| 190 | + * [Streamlit](https://pypi.org/project/streamlit/) - `streamlit` |
| 191 | + """) |
0 commit comments