Condividi tramite


DROP CONNECTION (catalogo straniero)

Si applica a:spunta segnato come sì Databricks SQL spunta segnato come sì Databricks Runtime 17.3 e versioni successive

Importante

Questa funzionalità è disponibile in anteprima pubblica ed è attualmente disponibile solo per i clienti partecipanti. Per partecipare all'anteprima, è necessario compilare il modulo. Questa funzionalità supporta solo l'eliminazione della connessione per i cataloghi esterni tramite Hive Metastore (HMS) e Glue Federation.

Usare il DROP CONNECTION comando per convertire un catalogo esterno in un catalogo standard in Unity Catalog. Dopo l'eliminazione della connessione, il catalogo non sincronizza più le tabelle esterne dal catalogo esterno. Funziona invece come un catalogo standard di Unity Catalog contenente tabelle gestite o esterne. Il catalogo è ora etichettato come standard anziché come esterno nel catalogo di Unity. Questo comando non influisce sulle tabelle nel catalogo esterno; influisce solo sul catalogo esterno in Unity Catalog.

Richiede OWNER o MANAGE, USE_CATALOG, e BROWSE le autorizzazioni per il catalogo.

Sintassi

ALTER CATALOG catalog_name DROP CONNECTION { RESTRICT | FORCE }

Parametri

  • catalog_name

    Nome del catalogo esterno da convertire in un catalogo standard.

  • LIMITARE

    Comportamento predefinito. DROP CONNECTION con RESTRICT fallisce quando si converte il catalogo estraneo in un catalogo standard se sono presenti nel catalogo tabelle o viste estranee.

    Per aggiornare le tabelle esterne a tabelle gestite o esterne di Unity Catalog, vedere Convertire una tabella esterna in una tabella del catalogo Unity gestita o Convertire una tabella esterna in una tabella del catalogo Unity esterna. Per convertire le visualizzazioni esterne, consultare SET MANAGED (FOREIGN VIEW).

  • FORZA

    DROP CONNECTION con FORCE elimina qualsiasi tabella o vista esterna rimanente in un catalogo esterno durante la conversione del catalogo esterno in un catalogo standard. Questo comando non rilascia dati o metadati nel catalogo esterno; elimina solo i metadati sincronizzati in Unity Catalog per creare la tabella esterna.

    Avvertimento

    Non è possibile eseguire il rollback di questo comando. Se si desidera eseguire di nuovo la federazione delle tabelle esterne in Unity Catalog, è necessario ricreare il catalogo esterno.

Esempi

-- Convert an existing foreign catalog using default RESTRICT behavior
> ALTER CATALOG hms_federated_catalog DROP CONNECTION;
OK

-- Convert an existing foreign catalog using FORCE to drop foreign tables
> ALTER CATALOG hms_federated_catalog DROP CONNECTION FORCE;
OK

-- RESTRICT fails if foreign tables or views exist
> ALTER CATALOG hms_federated_catalog DROP CONNECTION RESTRICT;
[CATALOG_CONVERSION_FOREIGN_ENTITY_PRESENT] Catalog conversion from UC Foreign to UC Standard failed because catalog contains foreign entities (up to 10 are shown here): <entityNames>. To see the full list of foreign entities in this catalog, please refer to the scripts below.

-- FORCE fails if catalog type isn't supported
> ALTER CATALOG redshift_federated_catalog DROP CONNECTION FORCE;
[CATALOG_CONVERSION_UNSUPPORTED_CATALOG_TYPE] Catalog cannot be converted from UC Foreign to UC Standard. Only HMS and Glue Foreign UC catalogs can be converted to UC Standard.

Script per il controllo di tabelle e viste esterne

Annotazioni

Prima di usare DROP CONNECTION RESTRICT, è possibile usare questi script Python per verificare la presenza di tabelle e viste esterne nel catalogo usando l'API REST del catalogo Unity.

Script per elencare tutte le tabelle e le viste esterne dal catalogo federato:

import requests

def list_foreign_uc_tables_and_views(catalog_name, pat_token, workspace_url):
    """
    Lists all foreign tables and views in the specified Unity Catalog.

    Args:
        catalog_name (str): The name of the catalog to search.
        pat_token (str): Personal Access Token for Databricks API authentication.
        workspace_url (str): Databricks workspace hostname (e.g., "https://adb-xxxx.x.azuredatabricks.net").

    Returns:
        list: A list of dictionaries containing information about the foreign tables/views.
    """
    base_url = f"{workspace_url}/api/2.1/unity-catalog"
    headers = {
        "Authorization": f"Bearer {pat_token}",
        "Content-Type": "application/json"
    }

    # Step 1: List all schemas in the catalog (GET request)
    schemas_url = f"{base_url}/schemas"
    schemas_params = {
        "catalog_name": catalog_name,
        "include_browse": "true"
    }

    schemas_resp = requests.get(schemas_url, headers=headers, params=schemas_params)
    schemas_resp.raise_for_status()
    schemas = schemas_resp.json().get("schemas", [])
    schema_names = [schema["name"] for schema in schemas]

    result = []

    # Step 2: For each schema, list all tables/views and filter (GET request)
    for schema_name in schema_names:
        tables_url = f"{base_url}/table-summaries"
        tables_params = {
            "catalog_name": catalog_name,
            "schema_name_pattern": schema_name,
            "include_manifest_capabilities": "true"
        }

        tables_resp = requests.get(tables_url, headers=headers, params=tables_params)
        tables_resp.raise_for_status()
        tables = tables_resp.json().get("tables", [])

        for table in tables:
            # Use OR for filtering as specified
            if (
                table.get("table_type") == "FOREIGN"
                or table.get("securable_kind") in {
                    "TABLE_FOREIGN_HIVE_METASTORE_VIEW",
                    "TABLE_FOREIGN_HIVE_METASTORE_DBFS_VIEW"
                }
            ):
                result.append(table.get("full_name"))

    return result

# Example usage:
# catalog = "hms_foreign_catalog"
# token = "dapiXXXXXXXXXX"
# workspace = "https://adb-xxxx.x.azuredatabricks.net"
# foreign_tables = list_foreign_uc_tables_and_views(catalog, token, workspace)
# for entry in foreign_tables:
#     print(entry)

Script per elencare tutte le tabelle e le viste esterne in stato di provisioning ATTIVO:

import requests

def list_foreign_uc_tables_and_views(catalog_name, pat_token, workspace_url):
    """
    Lists all foreign tables and views in the specified Unity Catalog.

    Args:
        catalog_name (str): The name of the catalog to search.
        pat_token (str): Personal Access Token for Databricks API authentication.
        workspace_url (str): Databricks workspace hostname (e.g., "https://adb-xxxx.x.azuredatabricks.net").

    Returns:
        list: A list of dictionaries containing information about the foreign tables/views.
    """
    base_url = f"{workspace_url}/api/2.1/unity-catalog"
    headers = {
        "Authorization": f"Bearer {pat_token}",
        "Content-Type": "application/json"
    }

    # Step 1: List all schemas in the catalog (GET request)
    schemas_url = f"{base_url}/schemas"
    schemas_params = {
        "catalog_name": catalog_name,
        "include_browse": "true"
    }

    schemas_resp = requests.get(schemas_url, headers=headers, params=schemas_params)
    schemas_resp.raise_for_status()
    schemas = schemas_resp.json().get("schemas", [])
    schema_names = [schema["name"] for schema in schemas]

    result = []

    # Step 2: For each schema, list all tables/views and filter (GET request)
    for schema_name in schema_names:
        tables_url = f"{base_url}/table-summaries"
        tables_params = {
            "catalog_name": catalog_name,
            "schema_name_pattern": schema_name,
            "include_manifest_capabilities": "true"
        }

        tables_resp = requests.get(tables_url, headers=headers, params=tables_params)
        tables_resp.raise_for_status()
        tables = tables_resp.json().get("tables", [])

        for table in tables:
            # Use OR for filtering as specified
            if (
                table.get("table_type") == "FOREIGN"
                or table.get("securable_kind") in {
                    "TABLE_FOREIGN_HIVE_METASTORE_VIEW",
                    "TABLE_FOREIGN_HIVE_METASTORE_DBFS_VIEW"
                }
            ):
                table_full_name = table.get('full_name')
                get_table_url = f"{base_url}/tables/{table_full_name}"
                tables_params = {
                    "full_name": table_full_name,
                    "include_browse": "true",
                    "include_manifest_capabilities": "true"
                }

                table_resp = requests.get(get_table_url, headers=headers, params=tables_params)
                table_resp.raise_for_status()
                provisioning_info = table_resp.json().get("provisioning_info", dict()).get("state", "")

                if provisioning_info == "ACTIVE":
                    result.append(table_full_name)

    return result

# Example usage:
# catalog = "hms_foreign_catalog"
# token = "dapiXXXXXXXXXX"
# workspace = "https://adb-xxxx.x.azuredatabricks.net"
# foreign_tables = list_foreign_uc_tables_and_views(catalog, token, workspace)
# for entry in foreign_tables:
#     print(entry)