Edits history of script submission #22652 for ' Get Table (databricks)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_catalog = string
    export type DynSelect_schema = string
    export type DynSelect_table = string
    
    // Dropdown of the metastore's catalogs.
    export async function catalog(auth: RT.Databricks) {
      const base = auth.workspace_url.replace(/\/$/, "")
      const response = await fetch(`${base}/api/2.1/unity-catalog/catalogs`, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { catalogs } = (await response.json()) as {
        catalogs?: { name: string }[]
      }
      return (catalogs ?? []).map((c) => ({ value: c.name, label: c.name }))
    }
    
    // Dropdown of the schemas in the chosen catalog (recomputes when catalog changes).
    export async function schema(auth: RT.Databricks, catalog: DynSelect_catalog) {
      const base = auth.workspace_url.replace(/\/$/, "")
      const url = new URL(`${base}/api/2.1/unity-catalog/schemas`)
      url.searchParams.append("catalog_name", catalog)
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { schemas } = (await response.json()) as {
        schemas?: { name: string }[]
      }
      return (schemas ?? []).map((s) => ({ value: s.name, label: s.name }))
    }
    
    // Dropdown of the tables in the chosen catalog + schema.
    export async function table(
      auth: RT.Databricks,
      catalog: DynSelect_catalog,
      schema: DynSelect_schema
    ) {
      const base = auth.workspace_url.replace(/\/$/, "")
      const url = new URL(`${base}/api/2.1/unity-catalog/tables`)
      url.searchParams.append("catalog_name", catalog)
      url.searchParams.append("schema_name", schema)
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { tables } = (await response.json()) as { tables?: { name: string }[] }
      return (tables ?? []).map((t) => ({ value: t.name, label: t.name }))
    }
    
    /**
     * Get Table
     * Retrieve a Unity Catalog table's full metadata (columns, types, properties) by catalog, schema and table name.
     */
    export async function main(
      auth: RT.Databricks,
      catalog: DynSelect_catalog,
      schema: DynSelect_schema,
      table: DynSelect_table
    ) {
      const base = auth.workspace_url.replace(/\/$/, "")
      const full_name = `${catalog}.${schema}.${table}`
      const url = new URL(`${base}/api/2.1/unity-catalog/tables/${full_name}`)
    
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 days ago