0

Get Table

by
Published 4 days ago

Retrieve a Unity Catalog table's full metadata (columns, types, properties) by catalog, schema and table name.

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
export type DynSelect_table = string
6

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

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

45
// Dropdown of the tables in the chosen catalog + schema.
46
export async function table(
47
  auth: RT.Databricks,
48
  catalog: DynSelect_catalog,
49
  schema: DynSelect_schema
50
) {
51
  const base = auth.workspace_url.replace(/\/$/, "")
52
  const url = new URL(`${base}/api/2.1/unity-catalog/tables`)
53
  url.searchParams.append("catalog_name", catalog)
54
  url.searchParams.append("schema_name", schema)
55
  const response = await fetch(url, {
56
    headers: {
57
      Authorization: `Bearer ${auth.token}`,
58
      Accept: "application/json",
59
    },
60
  })
61
  if (!response.ok) {
62
    throw new Error(`${response.status} ${await response.text()}`)
63
  }
64
  const { tables } = (await response.json()) as { tables?: { name: string }[] }
65
  return (tables ?? []).map((t) => ({ value: t.name, label: t.name }))
66
}
67

68
/**
69
 * Get Table
70
 * Retrieve a Unity Catalog table's full metadata (columns, types, properties) by catalog, schema and table name.
71
 */
72
export async function main(
73
  auth: RT.Databricks,
74
  catalog: DynSelect_catalog,
75
  schema: DynSelect_schema,
76
  table: DynSelect_table
77
) {
78
  const base = auth.workspace_url.replace(/\/$/, "")
79
  const full_name = `${catalog}.${schema}.${table}`
80
  const url = new URL(`${base}/api/2.1/unity-catalog/tables/${full_name}`)
81

82
  const response = await fetch(url, {
83
    method: "GET",
84
    headers: {
85
      Authorization: `Bearer ${auth.token}`,
86
      Accept: "application/json",
87
    },
88
  })
89

90
  if (!response.ok) {
91
    throw new Error(`${response.status} ${await response.text()}`)
92
  }
93

94
  return await response.json()
95
}
96