The business glossary export utility lets you extract glossaries, categories,
and terms from Knowledge Catalog (formerly Dataplex Universal Catalog) into a Google Sheet. You can then
manage your metadata in bulk and generate reports.

## Before you begin

Before you export glossaries, complete the following prerequisites.

### Set up the service account

To run the export utility, you must set up a service account with the necessary
permissions to access the Google Sheets API and impersonate your user
credentials:

1. Identify or create a service account.

   Select an existing service account or create a new one in the project where
   you run the import utility. For more information, see
   [Create service accounts](https://docs.cloud.google.com/iam/docs/creating-managing-service-accounts).
   Note the service account email
   (for example,
   `SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com`).
2. Configure service account impersonation.

   To run the export utility script locally, your user account must have
   permission to impersonate the service account. Grant your user account
   the **Service Account Token Creator** role (`roles/iam.serviceAccountTokenCreator`)
   on the service account.

   For more information, see
   [Manage access to service accounts](https://docs.cloud.google.com/iam/docs/manage-access-service-accounts.md.txt).
3. Grant the service account `Editor` access to the Google Sheet.

   Open the Google Sheet you want to use for the import process, click **Share** ,
   and add the service account email as an `Editor`. This permission enables
   the service account to read from or write data to your sheet.

### Required roles


To ensure that the service account has the necessary
permissions to export glossaries to a Google Sheet,

ask your administrator to grant the
following IAM roles to the service account:

**Important:** You must grant these roles to the service account, *not* to your user account. Failure to grant the roles to the correct principal might result in permission errors.

- [Dataplex Administrator](https://docs.cloud.google.com/iam/docs/roles-permissions/dataplex.md.txt#dataplex.admin) (`roles/dataplex.admin`) on the project
- [Dataplex Catalog Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/dataplex.md.txt#dataplex.catalogAdmin) (`roles/dataplex.catalogAdmin`) on the project
- [Dataplex Catalog Editor](https://docs.cloud.google.com/iam/docs/roles-permissions/dataplex.md.txt#dataplex.catalogEditor) (`roles/dataplex.catalogEditor`) on the project


For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


Your administrator might also be able to give the service account
the required permissions through [custom
roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles) or other [predefined
roles](https://docs.cloud.google.com/iam/docs/roles-overview#predefined).

### Enable APIs

To export glossaries, enable the following APIs in your project:

<br />

**Roles required to enable APIs**


To enable APIs, you need the `serviceusage.services.enable` permission. If you
created the project, then you likely already have this permission through the
Owner role (`roles/owner`). Otherwise, you can get this permission through the
Service Usage Admin role (`roles/serviceusage.serviceUsageAdmin`).
[Learn how to grant roles](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).

[Enable the APIs](https://console.cloud.google.com/apis/enableflow?apiid=dataplex.googleapis.com,cloudresourcemanager.googleapis.com,storage.googleapis.com,sheets.googleapis.com)

### Set up the git repository

Clone the `dataplex-labs` repository:

```bash
git clone https://github.com/GoogleCloudPlatform/dataplex-labs.git
cd dataplex-labs/dataplex-quickstart-labs/00-resources/scripts/python/business-glossary-import/dataplex-glossary/export
```

### Install dependencies

Install the required Python dependencies:

```python
pip3 install -r requirements.txt
```

If you encounter any issues with the package installation,
[set up a new Python development environment](https://docs.cloud.google.com/python/docs/setup.md.txt).

### Authenticate and configure service account impersonation

Initialize the Google Cloud CLI and authenticate using Application Default
Credentials (ADC) with service account impersonation:

```bash
# Set your service account email address
SA_EMAIL="SERVICE_ACCOUNT_EMAIL"

# Authenticate ADC using service account impersonation and required scopes
gcloud auth application-default login \
  --impersonate-service-account="${SA_EMAIL}" \
  --scopes="https://www.googleapis.com/auth/spreadsheets"
```

Replace <var translate="no">SERVICE_ACCOUNT_EMAIL</var> with the service account email ID.
For example:

`SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com`

## Set up the target Google Sheet for export

Create an empty Google Sheet or use an existing one. The export script writes to
the first sheet. Ensure that you have granted the service account `Editor`
access to the Google Sheet.

## Export glossary to the Google Sheet

Run the `glossary-export.py` script. The utility prompts you for the target
Google Sheet and the specific glossary name that you want to export, during
execution:

```python
python3 glossary-export.py
```

You can review the execution logs in the `logs/` directory in your local
execution path. These logs help you audit the transfer process and identify
skipped entries or formatting warnings.

## Verify the exported glossary data

The export script retrieves metadata from your
Knowledge Catalog business glossary and populates the Google Sheet with a
structured schema, including descriptions, data stewards, and labels. Verify
the exported data in the following columns:

| Field | Description |
|---|---|
| `id` | A unique identifier for the term or category within the glossary. |
| `parent` | The ID of the parent category. |
| `display_name` | The display name of the term or category. |
| `description` | A brief description of the term or category. |
| `overview` | A rich text description of the term or category (supports HTML tags). |
| `type` | The row type: `TERM` or `CATEGORY`. |
| `contact1_email` | Email address of the primary data steward for the term or category. |
| `contact1_name` | Name of the primary data steward for the term or category. |
| `contact2_email` | Email address of the secondary data steward for the term or category. |
| `contact2_name` | Name of the secondary data steward for the term or category. |
| `label1_key` | Key for the first assigned label. |
| `label1_value` | Value for the first assigned label. |
| `label2_key` | Key for the second assigned label. |
| `label2_value` | Value for the second assigned label. |

## What's next

- Learn how to [manage a business glossary](https://docs.cloud.google.com/knowledge-catalog/docs/manage-glossaries.md.txt).
- Learn how to [import glossaries using JSON files](https://docs.cloud.google.com/knowledge-catalog/docs/import-glossaries-entrylinks-json.md.txt).
- Learn how to [import glossaries from a Google Sheet](https://docs.cloud.google.com/knowledge-catalog/docs/import-glossaries-sheets.md.txt).
- Learn more about [metadata management](https://docs.cloud.google.com/knowledge-catalog/docs/metadata-overview.md.txt).