> [!CAUTION]
>
> Dataproc Hub and
> Vertex AI Workbench user-managed notebooks are
> deprecated. On January 30, 2025, support for user-managed notebooks
> will end and the ability to create user-managed notebooks instances
> will be removed. For alternative notebook solutions on Google Cloud, see:
>
> - [Install
>   the Jupyter component on your Managed Service for Apache Spark cluster](https://docs.cloud.google.com/managed-spark/docs/concepts/components/jupyter#install_jupyter).
> - [Create
>   a Managed Service for Apache Spark-enabled
>   Vertex AI Workbench instance](https://docs.cloud.google.com/vertex-ai/docs/workbench/instances/create-dataproc-enabled).

Dataproc Hub is a customized [JupyterHub](https://jupyterhub.readthedocs.io/en/stable/)server.
Admins configure and create Dataproc Hub instances that can spawn **single-user**
Managed Service for Apache Spark clusters to host [Jupyter](https://jupyter.org/) and [JupyterLab](https://jupyterlab.readthedocs.io/en/stable/) notebook environments (see [Use Dataproc Hub](https://docs.cloud.google.com/managed-spark/docs/tutorials/dataproc-hub-users)).

**Launch Notebooks for multiple users.** You can create a Managed Service for Apache Spark-enabled
[Vertex AI Workbench instance](https://docs.cloud.google.com/vertex-ai/docs/workbench/instances/create-dataproc-enabled)
or [install the Managed Service for Apache Spark JupyterLab plugin](https://docs.cloud.google.com/managed-spark/docs/quickstarts/jupyterlab-sessions)
on a VM to to serve notebooks to multiple users.

## Objectives

1. Define a Managed Service for Apache Spark cluster configuration (or use one
   of the predefined config files).

2. Set Dataproc Hub instance environment variables.

3. Create a Dataproc Hub instance.

## Before you begin

If you haven't already done so, create a Google Cloud project and a Cloud Storage bucket.

1. **Setting up your project**


   <br />

2. **Creating a Cloud Storage [bucket](https://docs.cloud.google.com/storage/docs/xml-api/put-bucket-create.md.txt)**
   in your project to hold the data used in this tutorial.


   1. In the Google Cloud console, go to the Cloud Storage **Buckets** page.

      [Go to Buckets](https://console.cloud.google.com/storage/browser)
   2. Click **Create**.
   3. On the **Create a bucket** page, enter your bucket information. To go to the next step, click **Continue** .
      1. In the **Get started** section, do the following:
         - Enter a globally unique name that meets the [bucket naming requirements](https://docs.cloud.google.com/storage/docs/bucket-naming#requirements).
         - To add a [bucket label](https://docs.cloud.google.com/storage/docs/tags-and-labels.md.txt#bucket-labels), expand the **Labels** section (), click **Add label** , and specify a `key` and a `value` for your label.
      2. In the **Choose where to store your data** section, do the following:
         1. Select a [Location type](https://docs.cloud.google.com/storage/docs/locations.md.txt).
         2. Choose a location where your bucket's data is permanently stored from the **[Location type](https://docs.cloud.google.com/storage/docs/locations.md.txt#available-locations)** drop-down menu.
            - If you select the [dual-region](https://docs.cloud.google.com/storage/docs/locations.md.txt#location-dr) location type, you can also choose to enable [turbo replication](https://docs.cloud.google.com/storage/docs/availability-durability.md.txt#turbo-replication) by using the relevant checkbox.
         3. To set up [cross-bucket replication](https://docs.cloud.google.com/storage/docs/availability-durability.md.txt#cross-bucket-replication), select **Add cross-bucket replication via Storage Transfer Service** and follow these steps:

            #### Set up cross-bucket replication

            1. In the **Bucket** menu, select a bucket.
            2. In the **Replication settings** section,
               click **Configure** to configure settings for the
               replication job.

               The **Configure cross-bucket replication** pane
               appears.
               - To filter objects to replicate by object name prefix, enter a prefix that you want to include or exclude objects from, then click **Add a prefix**.
               - To set a storage class for the replicated objects, select a storage class from the **Storage class** menu. If you skip this step, the replicated objects will use the destination bucket's storage class by default.
               - Click **Done**.
      3. In the **Choose how to store your data** section, do the following:
         1. Select a [default storage class](https://docs.cloud.google.com/storage/docs/storage-classes.md.txt) for the bucket or [Autoclass](https://docs.cloud.google.com/storage/docs/autoclass.md.txt) for automatic storage class management of your bucket's data.
         2. To enable [hierarchical namespace](https://docs.cloud.google.com/storage/docs/hns-overview.md.txt), in the **Optimize storage for data-intensive workloads** section, select **Enable hierarchical namespace on this bucket** . **Note:** You cannot enable hierarchical namespace in existing buckets.
      4. In the **Choose how to control access to objects** section, select whether or not your bucket enforces [public access prevention](https://docs.cloud.google.com/storage/docs/public-access-prevention.md.txt), and select an [access control method](https://docs.cloud.google.com/storage/docs/access-control.md.txt) for your bucket's objects. **Note:** You cannot change the **Prevent public access** setting if this setting is enforced at an [organization policy](https://docs.cloud.google.com/storage/docs/org-policy-constraints.md.txt#public-access-prevention).
      5. In the **Choose how to protect object data** section, do the following:
         - Select any of the options under **Data protection** that you want to set for your bucket.
           - To enable [soft delete](https://docs.cloud.google.com/storage/docs/soft-delete.md.txt), click the **Soft delete policy (For data recovery)** checkbox, and specify the number of days you want to retain objects after deletion.
           - To set [Object Versioning](https://docs.cloud.google.com/storage/docs/object-versioning.md.txt), click the **Object versioning (For version control)** checkbox, and specify the maximum number of versions per object and the number of days after which the noncurrent versions expire.
           - To enable the retention policy on objects and buckets, click the **Retention (For compliance)** checkbox, and then do the following:
             - To enable [Object Retention Lock](https://docs.cloud.google.com/storage/docs/object-lock.md.txt), click the **Enable object retention** checkbox.
             - To enable [Bucket Lock](https://docs.cloud.google.com/storage/docs/bucket-lock.md.txt), click the **Set bucket retention policy** checkbox, and choose a unit of time and a length of time for your retention period.
         - To choose how your object data will be encrypted, expand the **Data encryption** section (), and select a [**Data encryption** method](https://docs.cloud.google.com/storage/docs/encryption.md.txt).
   4. Click **Create**.

   <br />

## Define a cluster configuration

A Dataproc Hub instance creates a cluster from configuration values
contained in a YAML cluster configuration file.

> [!IMPORTANT]
> **Predefined cluster configs:** You can use the following predefined config files located in Cloud Storage:
>
> - `example-cluster` config: Sets up a standard Jupyter component cluster with one master and 2 worker nodes
> - `example-single-node` config: Sets up a Jupyter component cluster with one node
>
> To view these public Cloud Storage config files, run:
>
> ```
> gcloud storage cat gs://dataproc-spawner-dist/example-configs/example-cluster.yaml
> gcloud storage cat gs://dataproc-spawner-dist/example-configs/example-single-node.yaml
> ```
> **Create a quickstart hub.** To create a hub using the predefined cluster configs, skip to [Create a Managed Service for Apache Spark Hub
> instance](https://docs.cloud.google.com/managed-spark/docs/tutorials/hub-admins#create_a_dataproc_hub_instance). The Cloud Storage location of the predefined cluster configs listed above is the default value of the DATAPROC_CONFIGS environment variable field.

Your cluster configuration can specify any feature or component available
to Managed Service for Apache Spark clusters (such as machine type, initialization
actions, and optional components). **The cluster image version must be
1.4.13 or higher.** Attempting to spawn a cluster with an image version lower
than 1.4.13 will cause an error and fail.

> [!NOTE]
> **Note:** Admins can allow users to override some of the predefined cluster configuration parameters. For more information, see `DATAPROC_ALLOW_CUSTOM_CLUSTERS` in [Set Dataproc Hub instance environment values](https://docs.cloud.google.com/managed-spark/docs/tutorials/hub-admins.md.txt#set_dataproc_hub_instance_environment_variables).

**Sample YAML cluster configuration file**

```
clusterName: cluster-name
config:
  softwareConfig:
    imageVersion: 2.2-ubuntu22
    optionalComponents:
    - JUPYTER
```

Each configuration must be [saved in Cloud Storage](https://docs.cloud.google.com/managed-spark/docs/tutorials/hub-admins.md.txt#save_the_yaml_configuration_file_in).
You can create and save multiple configuration files to give
users a choice when they
[use Dataproc Hub](https://docs.cloud.google.com/managed-spark/docs/tutorials/dataproc-hub-users)
to create their Managed Service for Apache Spark cluster notebook environment.

There are two ways to create a YAML cluster configuration file:

1. [Create YAML cluster configuration file from the console](https://docs.cloud.google.com/managed-spark/docs/tutorials/hub-admins.md.txt#create_yaml_cluster_configuration_file_from_the_console)

2. [Export a YAML cluster configuration file from an existing cluster](https://docs.cloud.google.com/managed-spark/docs/tutorials/hub-admins.md.txt#export_a_yaml_cluster_configuration_file_from_an_existing_cluster)

### Create YAML cluster configuration file from the console

1. Open the [**Create a cluster**](https://console.cloud.google.com/dataproc/clustersAdd) page in the Google Cloud console, then select and fill in the fields to specify the type of cluster the Dataproc Hub will spawn for users. The region and zone settings will be overridden when the user's cluster is spawned: the spawned cluster's region will be the region where the Dataproc Hub is located, and the user will select a zone within this region.
   1. At the bottom of the left panel, select "Equivalent REST".
   2. Copy the generated JSON block, excluding the leading POST request line, then paste the JSON block into an online JSON-to-YAML converter (search online for "Convert JSON to YAML"). Some JSON to YAML converters generate a first line containing "---". The inclusion of this line in the YAML file is optional.
   3. Copy the converted YAML into a local <var translate="no">cluster-config-filename</var>.yaml file.

### Export a YAML cluster configuration file from an existing cluster

1. [Create a cluster](https://docs.cloud.google.com/managed-spark/docs/guides/create-cluster.md.txt) that matches your requirements.
2. Export the cluster configuration to a local <var translate="no">cluster-config-filename</var>.yaml file.

   ```
   gcloud dataproc clusters export cluster-name \
       --destination cluster-config-filename.yaml  \
       --region region
    
   ```

### Save the YAML configuration file in Cloud Storage

Copy your local YAML cluster configuration file to your
Cloud Storage bucket.

```
gcloud storage cp cluster-config-filename.yaml gs://bucket-name/
```

## Set Dataproc Hub instance environment variables

The administrator can set the hub environment variables listed in the table, below,
to set attributes of the Managed Service for Apache Spark clusters that will be
spawned by hub users.

> [!NOTE]
> **Note:** If hub environment variables are not set, Managed Service for Apache Spark will use default values, including setting `DATAPROC_CONFIGS` to the Cloud Storage location of the predefined cluster configs (see [Define a cluster configuration](https://docs.cloud.google.com/managed-spark/docs/tutorials/hub-admins.md.txt#define_a_cluster_configuration)).

| Variable | Description | Example |
|---|---|---|
| NOTEBOOKS_LOCATION | Cloud Storage bucket or bucket folder that contains user notebooks. The \`gs://\` prefix is optional. **Default:** The [Managed Service for Apache Spark staging bucket](https://docs.cloud.google.com/managed-spark/docs/concepts/configuring-clusters/staging-bucket.md.txt). | gs://<var translate="no">bucket-name</var>/ |
| DATAPROC_CONFIGS | Comma delimited list of strings of the Cloud Storage paths to [YAML cluster config files](https://docs.cloud.google.com/managed-spark/docs/tutorials/hub-admins.md.txt#define_a_cluster_configuration). The \`gs://\` prefix is optional. **Default:** `gs://dataproc-spawner-dist/example-configs/`. which contains predefined [`example-cluster.yaml` and `example-single-node.yaml`](https://docs.cloud.google.com/managed-spark/docs/tutorials/hub-admins.md.txt#define_a_cluster_configuration). | gs://<var translate="no">cluster-config-filename</var>.yaml |
| DATAPROC_LOCATIONS_LIST | Zone suffixes in region where the Dataproc Hub instance is located. Users can select one of these zones as the zone where their Managed Service for Apache Spark cluster will be spawned. **Default:** "b". | b,c,d |
| DATAPROC_DEFAULT_SUBNET | Subnet on which Dataproc Hub instance will spawn Managed Service for Apache Spark clusters. **Default:** the Dataproc Hub instance subnet. | https://www.googleapis.com/compute/v1/projects/<var translate="no">project-id</var>/regions/<var translate="no">region</var>/subnetworks/<var translate="no">subnet-name</var> |
| DATAPROC_SERVICE_ACCOUNT | [Service account](https://docs.cloud.google.com/managed-spark/docs/concepts/iam/principals.md.txt#vm_service_account_data_plane_identity) that Managed Service for Apache Spark VMs will run as. **Default:** If not set, the [default Managed Service for Apache Spark service account](https://docs.cloud.google.com/managed-spark/docs/concepts/configuring-clusters/service-accounts.md.txt#service_accounts_in_dataproc) is used. | <var translate="no">service-account</var>@<var translate="no">project-id</var>.iam.gserviceaccount.com |
| SPAWNER_DEFAULT_URL | Whether to show the Jupyter or JupyterLab UI on spawned Managed Service for Apache Spark clusters by default. **Default:** "/lab". | \`/\` or \`/lab\`, for Jupyter or JupyterLab, respectively. |
| DATAPROC_ALLOW_CUSTOM_CLUSTERS | Whether to allow users to customize their Managed Service for Apache Spark clusters. **Default:** false. | "true" or "false" |
| DATAPROC_MACHINE_TYPES_LIST | List of machine types that users are allowed to choose for their spawned Managed Service for Apache Spark clusters, if cluster customization (DATAPROC_ALLOW_CUSTOM_CLUSTERS) is enabled. **Default:** empty (all machine types are allowed). | n1-standard-4,n1-standard-8,e2-standard-4,n1-highcpu-4 |
| NOTEBOOKS_EXAMPLES_LOCATION | Cloud Storage path to notebooks bucket or bucket folder to be downloaded to the spawned Managed Service for Apache Spark cluster when the cluster starts. **Default:** empty. | gs://<var translate="no">bucket-name</var>/ |

### Setting hub environment variables

There are two ways to set hub environment variables:

1. [Set hub environment variables from the console](https://docs.cloud.google.com/managed-spark/docs/tutorials/hub-admins.md.txt#variables-console)

2. [Set hub environment variables in a text file](https://docs.cloud.google.com/managed-spark/docs/tutorials/hub-admins.md.txt#variables-text)

#### Set hub environment variables from the console

When you [create a Dataproc Hub instance](https://docs.cloud.google.com/managed-spark/docs/tutorials/hub-admins.md.txt#create_a_dataproc_hub_instance)
from the **User-Managed Notebooks** tab on the
**[Dataproc→Workbench](https://console.cloud.google.com/dataproc/workbench)**
page in the Google Cloud console, you can click the **Populate**
button to open a **Populate Dataproc Hub** form that allows you to set each
environment variable.

#### Set hub environment variables in a text file

1. **Create the file.** You can use a text editor to set
   Dataproc Hub instance environment variables in a local file.
   Alternatively, you can create the file by running the following command
   after filling in placeholder values and changing or adding variables and their values.

   ```
   cat <<EOF > environment-variables-file
   DATAPROC_CONFIGS=gs://bucket/cluster-config-filename.yaml
   NOTEBOOKS_LOCATION=gs://bucket/notebooks
   DATAPROC_LOCATIONS_LIST=b,c
   EOF
   ```

   <br />

2. **Save the file in Cloud Storage.** Copy your local
   Dataproc Hub instance environment variables
   file to your Cloud Storage bucket.

   ```
   gcloud storage cp environment-variable-filename gs://bucket-name/folder-name/
   ```

<br />

## Set Identity and Access Management (IAM) roles

Dataproc Hub includes the following identities with the
following abilities:

- Administrator: creates a Dataproc Hub instance
- Data and ML user: accesses the Dataproc Hub UI
- Dataproc Hub service account: represents Dataproc Hub
- Managed Service for Apache Spark service account: represents the Managed Service for Apache Spark cluster that Dataproc Hub creates.

Each identity requires specific roles or permissions to perform their associated
tasks. The table below summarizes IAM roles and permissions required by each
identity.

| Identity | Type | Role or permission |
|---|---|---|
| Dataproc Hub administrator | User or Service account | [roles/notebooks.admin](https://docs.cloud.google.com/vertex-ai/docs/workbench/user-managed/iam#notebooks.admin) |
| Dataproc Hub user | User | [notebooks.instances.use](https://docs.cloud.google.com/iam/docs/roles-permissions/notebooks.md.txt#notebooks.instances.use), [dataproc.clusters.use](https://docs.cloud.google.com/iam/docs/roles-permissions/dataproc.md.txt#dataproc.clusters.use) |
| Dataproc Hub | Service account | [roles/dataproc.hubAgent](https://docs.cloud.google.com/iam/docs/roles-permissions/dataproc.md.txt#dataproc.hubAgent) |
| Dataproc | Service account | [roles/dataproc.worker](https://docs.cloud.google.com/iam/docs/roles-permissions/dataproc.md.txt#dataproc.worker) |

> [!IMPORTANT]
> By default, the Dataproc Hub instance and Managed Service for Apache Spark cluster nodes run as the project [default Compute Engine service account](https://docs.cloud.google.com/compute/docs/access/service-accounts.md.txt#default_service_account).

## Create a Dataproc Hub instance

1. **Before you begin:** To create a Managed Service for Apache Spark Hub instance
   from the Google Cloud console, your user account must have
   `compute.instances.create` permission. Also, the service account of the
   instance---the Compute Engine default service account or your
   user-specified service account listed in
   [IAM \& admin \> Service Accounts](https://console.cloud.google.com/iam-admin/serviceaccounts) (see
   [Managed Service for Apache Spark VM service account](https://docs.cloud.google.com/managed-spark/docs/concepts/configuring-clusters/service-accounts.md.txt#VM_service_account))--- must have `iam.serviceAccounts.actAs`
   permission.

2. Go to the
   **[Dataproc→Workbench](https://console.cloud.google.com/dataproc/workbench)**
   page in the Google Cloud console, then select the **User-Managed Notebooks**
   tab.

3. If not pre-selected as a filter, click in the **Filter** box, then select \*\*Environment:Dataproc Hub"".

4. Click **New Notebook→Dataproc Hub**.

5. On the **Create a user-managed notebook** page, provide the following information:

   1. **Notebook name**: Dataproc Hub instance name.
   2. **Region** : Select a [region](https://docs.cloud.google.com/compute/docs/regions-zones.md.txt#available) for the Dataproc Hub instance. Managed Service for Apache Spark clusters spawned by this Dataproc Hub instance will also be created in this region.

      > [!IMPORTANT]
      > For best performance, select a geographically close region.

   3. **Zone**: Select a zone within the selected region.
   4. **Environment:**
      1. `Environment`: Select `Dataproc Hub`.
      2. `Select a script to run after creation` (optional): You can insert or browse and select an [initialization action](https://docs.cloud.google.com/managed-spark/docs/concepts/configuring-clusters/init-actions.md.txt) script or executable to run on the spawned Managed Service for Apache Spark cluster.
      3. `Populate Dataproc Hub (optional)`: Click **Populate** to open a form that allows you to set each of the hub environment variables (see [Set Dataproc Hub instance environment variables](https://docs.cloud.google.com/managed-spark/docs/tutorials/hub-admins.md.txt#set_dataproc_hub_instance_environment_variables) for a description of each variable). Managed Service for Apache Spark uses default values for any unset environment variables. As an alternative, you can set **Metadata** `key:value` pairs to set environment variables (see next item).
      4. `Metadata`:
         1. If you created a text file that contains your hub environment variable settings (see [Setting hub environment variables](https://docs.cloud.google.com/managed-spark/docs/tutorials/hub-admins.md.txt#setting_hub_environment_variables)), provide the name of the file as the `key` and the `gs://bucket-name/folder-name/environment-variable-filename` Cloud Storage location of the file as the `value`. Managed Service for Apache Spark uses default values for any unset environment variables.
   5. **Machine configuration:**
      1. `Machine Type`: Select the Compute Engine [machine type](https://docs.cloud.google.com/compute/docs/machine-types).
      2. Set other machine configuration options.
   6. **Other Options** :
      1. You can expand and set or replace default values in the **Disks** , **Networking** , **Permission** , **Security** , and **Environment upgrade and system health** sections.
   7. Click **Create** to launch the Dataproc Hub instance.
6. The **Open JupyterLab** link for the Dataproc Hub instance becomes
   active after the instance is created. Users click this link to open the
   **JupyterHub** server page to configure and create a Managed Service for Apache Spark JupyterLab
   cluster (see [Use Dataproc Hub](https://docs.cloud.google.com/managed-spark/docs/tutorials/dataproc-hub-users)).

## Clean up

### Delete the Dataproc Hub instance

- To delete your Dataproc Hub instance:

  ```
  gcloud compute instances delete --project=${PROJECT} ${INSTANCE_NAME}
  ```

### Delete the bucket

- To delete the Cloud Storage bucket you created in [Before you begin](https://docs.cloud.google.com/managed-spark/docs/tutorials/hub-admins.md.txt#before-you-begin), including the data files stored in the bucket:

  ```
  gcloud storage rm gs://${BUCKET_NAME} --recursive
  ```

## What's next

- [Use Dataproc Hub](https://docs.cloud.google.com/managed-spark/docs/tutorials/dataproc-hub-users)