0

List Tables

by
Published 4 days ago

List the tables within a Unity Catalog schema. Paginate with page_token.

Script databricks Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 days ago
1
//native
2

3
export type DynSelect_catalog = string
4
export type DynSelect_schema = string
5

6
// Dropdown of the metastore's catalogs.
7
export async function catalog(auth: RT.Databricks) {
8
  const base = auth.workspace_url.replace(/\/$/, "")
9
  const response = await fetch(`${base}/api/2.1/unity-catalog/catalogs`, {
10
    headers: {
11
      Authorization: `Bearer ${auth.token}`,
12
      Accept: "application/json",
13
    },
14
  })
15
  if (!response.ok) {
16
    throw new Error(`${response.status} ${await response.text()}`)
17
  }
18
  const { catalogs } = (await response.json()) as {
19
    catalogs?: { name: string }[]
20
  }
21
  return (catalogs ?? []).map((c) => ({ value: c.name, label: c.name }))
22
}
23

24
// Dropdown of the schemas in the chosen catalog (recomputes when catalog changes).
25
export async function schema(auth: RT.Databricks, catalog: DynSelect_catalog) {
26
  const base = auth.workspace_url.replace(/\/$/, "")
27
  const url = new URL(`${base}/api/2.1/unity-catalog/schemas`)
28
  url.searchParams.append("catalog_name", catalog)
29
  const response = await fetch(url, {
30
    headers: {
31
      Authorization: `Bearer ${auth.token}`,
32
      Accept: "application/json",
33
    },
34
  })
35
  if (!response.ok) {
36
    throw new Error(`${response.status} ${await response.text()}`)
37
  }
38
  const { schemas } = (await response.json()) as {
39
    schemas?: { name: string }[]
40
  }
41
  return (schemas ?? []).map((s) => ({ value: s.name, label: s.name }))
42
}
43

44
/**
45
 * List Tables
46
 * List the tables within a Unity Catalog schema. Paginate with page_token.
47
 */
48
export async function main(
49
  auth: RT.Databricks,
50
  catalog: DynSelect_catalog,
51
  schema: DynSelect_schema,
52
  max_results: number | undefined,
53
  page_token: string | undefined
54
) {
55
  const base = auth.workspace_url.replace(/\/$/, "")
56
  const url = new URL(`${base}/api/2.1/unity-catalog/tables`)
57
  url.searchParams.append("catalog_name", catalog)
58
  url.searchParams.append("schema_name", schema)
59
  if (max_results !== undefined)
60
    url.searchParams.append("max_results", String(max_results))
61
  if (page_token !== undefined && page_token !== "")
62
    url.searchParams.append("page_token", page_token)
63

64
  const response = await fetch(url, {
65
    method: "GET",
66
    headers: {
67
      Authorization: `Bearer ${auth.token}`,
68
      Accept: "application/json",
69
    },
70
  })
71

72
  if (!response.ok) {
73
    throw new Error(`${response.status} ${await response.text()}`)
74
  }
75

76
  return await response.json()
77
}
78