Running a PySpark job
In a YTsaurus cluster, you can run Spark and PySpark jobs using the spark-submit script. It saves the calculation results to HDFS. For more information about spark-submit, see the Spark documentation
The example below shows how to run an application in Python
To run a PySpark job:
If you no longer need the resources you created, delete them.
Required paid resources
- The YTsaurus cluster, which includes the use of computing resources of cluster components and storage size (see YTsaurus pricing).
- VM instance: use of computing resources, storage, public IP address, and OS (see Compute Cloud pricing).
Getting started
Set up your infrastructure:
-
Create a service account named
ytsaurus-sawith themanaged-ytsaurus.editorrole. -
Create a network named
ytsaurus-network. Disable Create subnets when creating it. -
In
ytsaurus-network, create a subnet with the following settings:- Name:
ytsaurus-subnet-a - Availability zone:
ru-central1-a - CIDR:
10.1.0.0/16
- Name:
-
Create security groups in
ytsaurus-network:-
For the virtual machine: a security group named
vm-security-groupwith rules allowing incoming TCP traffic to port 22 from all addresses and outgoing traffic to all ports and addresses. -
For the YTsaurus cluster: a security group named
ytsaurus-security-groupwith a rule allowing incoming traffic on all ports from all addresses.
-
-
Create a
vm-ubuntu-24-04virtual machine with the following parameters:- Operating system:
Ubuntu 24.04 - Availability zone:
ru-central1-a - Subnet:
ytsaurus-subnet-a - Security group:
vm-security-group - Service account:
ytsaurus-sa
- Operating system:
-
Create a YTsaurus cluster with the following settings:
- Cluster type:
Demo cluster - Cluster name:
ytsaurus-cluster - Availability zone:
ru-central1-a - Subnet:
ytsaurus-subnet-a - Security group:
ytsaurus-security-group
- Cluster type:
-
If you do not have Terraform yet, install it.
-
Get the authentication credentials. You can add them to environment variables or specify them later in the provider configuration file.
-
Configure and initialize a provider. There is no need to create a provider configuration file manually, you can download it
. -
Place the configuration file in a separate working directory and specify the parameter values. If you did not add the authentication credentials to environment variables, specify them in the configuration file.
-
Download the ytsaurus-for-spark-jobs.tf
configuration file to the same working directory.This file describes:
- Network.
- Subnet.
- Security groups.
- Service account to work with cluster resources.
- Virtual machine.
- YTsaurus cluster.
-
In the
ytsaurus-for-spark-jobs.tfconfiguration file, specify the required parameters. -
Validate your Terraform configuration files using this command:
terraform validateTerraform will display any configuration errors detected in your files.
-
Create the required infrastructure:
-
Run this command to view the planned changes:
terraform planIf you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
-
If everything looks correct, apply the changes:
-
Run this command:
terraform apply -
Confirm updating the resources.
-
Wait for the operation to complete.
-
All the required resources will be created in the specified folder. You can check resource availability and their settings in the management console
. -
Prepare your VM
-
If you do not have the Yandex Cloud CLI installed yet, install and initialize it.
-
Install the dependencies:
sudo apt update && sudo apt install -y python3 python3-pip python3-venv -
Create and activate a virtual environment to run a Python script:
python3 -m venv .venv/yt312 && \ source .venv/yt312/bin/activate -
Install Java:
sudo apt install -y openjdk-11-jdk -
Install the YTsaurus CLI:
pip install ytsaurus-client -
Create an IAM token:
yc iam create-token -
Get the service account ID:
yc iam whoami -
Set the environment variables:
export YT_PROXY=http://hp.<YTsaurus_cluster_ID>.ytsaurus.mdb.yandexcloud.net:32100 ; \ export YT_TOKEN=<IAM_token> ; \ export YT_USER=<service_account_ID> -
Get the SPYR version:
yt list //home/spark/conf/releasesResult:
2.7.0 -
Install the
ytsaurus-spytpackage of the same version as SPYT:pip install ytsaurus-spyt==2.7.0 -
Get the Spark version from the name of the tgz file:
yt find //home/spark/distrib --name *tgzResult:
//home/spark/distrib/3/5/6/spark-3.5.6-bin-hadoop3.tgz -
Install
pysparkof the same version as thesparkdistribution:pip install pyspark==3.5.6 -
Activate the SPYT configuration:
source spyt-envResult:
SPYT environment has been successfully activated -
Check the value of the
$SPARK_CONF_DIRvariable:echo $SPARK_CONF_DIRResult:
/home/<home_directory>/.venv/yt312/lib/python3.12/site-packages/spyt/conf
Prepare your test data
Run these commands on the VM:
-
Create a table named
table1in YTsaurus:yt create table //tmp/table1 \ --attributes '{schema = [{name = id; type = int64}; {name = text; type = string}]}' -
Write the data to the table:
echo '{ "id": 0, "text": "Hello" } { "id": 1, "text": "World!" }' | yt write-table //tmp/table1 --format json
Create and run a PySpark job
Perform these actions on the VM:
-
Create the
spark-job.pyfile with the PySpark job.import spyt from pyspark import SparkConf conf = SparkConf() conf.set("spark.app.name","Show table") with spyt.direct_spark_session("http-proxies-lb.yt.svc.cluster.local", conf) as spark: spark.read.yt('//tmp/table1').show() -
Run the job:
spark-submit --master "ytsaurus://${YT_PROXY}" \ --deploy-mode cluster \ --num-executors 1 \ --conf spark.hadoop.yt.proxyNetworkName=external \ --queue research \ ./spark-job.pyResult:
... 25/12/09 23:29:49 INFO YTsaurusClusterApplication: Operation: fdff4aa2-d11767f6-e03e8-6affdd7c, State: running 25/12/09 23:30:23 INFO YTsaurusClusterApplication: Operation: fdff4aa2-d11767f6-e03e8-6affdd7c, State: completed
Delete the resources you created
Some resources are not free of charge. Delete the resources you no longer need to avoid paying for them:
-
In the terminal window, go to the directory containing the infrastructure plan.
Warning
Make sure the directory has no Terraform manifests with the resources you want to keep. Terraform deletes all resources that were created using the manifests in the current directory.
-
Delete resources:
-
Run this command:
terraform destroy -
Confirm deleting the resources and wait for the operation to complete.
All the resources described in the Terraform manifests will be deleted.
-