"Managed Service for Apache Spark" is the new name for the product formerly known as "Dataproc on Compute Engine" (cluster deployment) and "Google Cloud Serverless for Apache Spark" (serverless deployment).
Use the Spark Spanner connector
Stay organized with collections
Save and categorize content based on your preferences.
This page shows you how to create a Managed Service for Apache Spark cluster that uses the
Spark Spanner connector
to read data from and write data to Spanner
using Apache Spark.
The Spanner connector works with Spark to read data from
and write data to the Spanner database using the
Spanner Java library.
The Spanner connector supports reading Spanner
tables and
graphs into Spark
DataFrames
and
GraphFrames,
and writing DataFrame data into Spanner tables.
Costs
In this document, you use the following billable components of Google Cloud:
Managed Service for Apache Spark
Spanner
Cloud Storage
To generate a cost estimate based on your projected usage,
use the pricing calculator.
New Google Cloud users might be eligible for a free trial.
Before you begin
Sign in to your Google Cloud account. If you're new to
Google Cloud,
create an account to evaluate how our products perform in
real-world scenarios. New customers also get 300ドル in free credits to
run, test, and deploy workloads.
In the Google Cloud console, on the project selector page,
select or create a Google Cloud project.
Roles required to select or create a project
Select a project: Selecting a project doesn't require a specific
IAM role—you can select any project that you've been
granted a role on.
Create a project: To create a project, you need the Project Creator role
(roles/resourcemanager.projectCreator), which contains the
resourcemanager.projects.create permission. Learn how to grant
roles.
Enable the Spanner, Managed Service for Apache Spark, and Cloud Storage APIs.
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.
In the Google Cloud console, on the project selector page,
select or create a Google Cloud project.
Roles required to select or create a project
Select a project: Selecting a project doesn't require a specific
IAM role—you can select any project that you've been
granted a role on.
Create a project: To create a project, you need the Project Creator role
(roles/resourcemanager.projectCreator), which contains the
resourcemanager.projects.create permission. Learn how to grant
roles.
Enable the Spanner, Managed Service for Apache Spark, and Cloud Storage APIs.
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.
Certain IAM roles are required to
run the examples on this page. Depending on organization policies, these
roles may have already been granted. To check role grants, see
Do you need to grant roles?.
To ensure that the Compute Engine default service account has the necessary
permissions to create a Managed Service for Apache Spark cluster,
ask your administrator to grant the
following IAM roles to the Compute Engine default service account on the project:
Set up a Spanner instance with a Singers database table
Create a Spanner instance
with a database that contains a Singers table. Note the Spanner
instance ID and database ID.
Use the Spanner connector with Spark
The Spanner connector is available for Spark versions 3.1+. You
specify the
connector version
as part of the Cloud Storage connector JAR file specification when you
submit a job to a
Managed Service for Apache Spark cluster.
Example: gcloud CLI Spark job submission with the
Spanner connector.
CONNECTOR_VERSION: Spanner connector version.
Choose the Spanner connector version from the version list in the GitHub
GoogleCloudDataproc/spark-spanner-connector repository.
Read Spanner tables
You can use Python or Scala to read Spanner table data into a
Spark Dataframe using the
Spark data source API.
PySpark
You can run the example PySpark code in this section on your cluster by submitting the job to the
Managed Service for Apache Spark or by running the job from the spark-submit REPL
on the cluster master node.
Managed Service for Apache Spark job
Create a singers.py file in using a local
text editor or in Cloud Shell using the
pre-installed vi, vim, or nano text editor.
After populating the placehoder variables, paste the following code
into the singers.py file. Note that
the Spanner
Data Boost feature is
enabled, which has near-zero impact on the main Spanner instance.
#!/usr/bin/env python"""Spanner PySpark read example."""frompyspark.sqlimportSparkSessionspark=SparkSession \
.builder \
.master('yarn') \
.appName('spark-spanner-demo') \
.getOrCreate()# Load data from Spanner.singers=spark.read.format('cloud-spanner') \
.option("projectId","PROJECT_ID") \
.option("instanceId","INSTANCE_ID") \
.option("databaseId","DATABASE_ID") \
.option("table","TABLE_NAME") \
.option("enableDataBoost","true") \
.load()singers.createOrReplaceTempView('Singers')# Read from Singersresult=spark.sql('SELECT * FROM Singers')result.show()result.printSchema()
Replace the following:
PROJECT_ID: Your Google Cloud project ID.
Project IDs are listed in the Project info section on
the Google Cloud console Dashboard.
REGION: An available Compute Engine
region to run the workload.
CONNECTOR_VERSION: Spanner connector version.
Choose the Spanner connector version from the version list in the GitHub
GoogleCloudDataproc/spark-spanner-connector repository.
spark-submit job
Connect to the Managed Service for Apache Spark cluster master node using SSH.
Go to the Managed Service for Apache Spark
Clusters
page in the Google Cloud console, then click the name of your cluster.
On the Cluster details page, select the VM Instances tab. Then click
SSH to the right of the name of the cluster master node.
Screenshot of the Dataproc Cluster details page in the Google Cloud console, showing the SSH button used to connect to the cluster master node.
A browser window opens at your home directory on the master node.
Create a singers.py file on the master node using the pre-installed vi,
vim, or nano text editor.
Paste the following code into the singers.py file after
populating the placehoder variables into the singers.py file.
Note that the Spanner
Data Boost feature is
enabled, which has near-zero impact on the main Spanner instance.
#!/usr/bin/env python"""Spanner PySpark read example."""frompyspark.sqlimportSparkSessionspark=SparkSession \
.builder \
.master('yarn') \
.appName('spark-spanner-demo') \
.getOrCreate()# Load data from Spanner.singers=spark.read.format('cloud-spanner') \
.option("projectId","PROJECT_ID") \
.option("instanceId","INSTANCE_ID") \
.option("databaseId","DATABASE_ID") \
.option("table","TABLE_NAME") \
.option("enableDataBoost","true") \
.load()singers.createOrReplaceTempView('Singers')# Read from Singersresult=spark.sql('SELECT * FROM Singers')result.show()result.printSchema()
Replace the following:
PROJECT_ID: Your Google Cloud project ID.
Project IDs are listed in the Project info section on
the Google Cloud console Dashboard.
CONNECTOR_VERSION: Spanner connector version.
Choose the Spanner connector version from the version list in the GitHub
GoogleCloudDataproc/spark-spanner-connector repository.
To run the example Scala code on your cluster, complete the following steps:
Connect to the Managed Service for Apache Spark cluster master node using SSH.
Go to the Managed Service for Apache Spark
Clusters
page in the Google Cloud console, then click the name of your cluster.
On the Cluster details page, select the VM Instances tab. Then click
SSH to the right of the name of the cluster master node.
Dataproc Cluster details page in the Google Cloud console.
A browser window opens at your home directory on the master node.
Create a singers.scala file on the master node using the pre-installed vi,
vim, or nano text editor.
Paste the following code into the singers.scala file. Note that the Spanner
Data Boost feature is
enabled, which has near-zero impact on the main Spanner instance.
objectsingers{defmain():Unit={/* * Uncomment (use the following code) if you are not running in spark-shell. * import org.apache.spark.sql.SparkSession val spark = SparkSession.builder() .appName("spark-spanner-demo") .getOrCreate() */// Load data in from Spanner. See// https://github.com/GoogleCloudDataproc/spark-spanner-connector/blob/main/README.md#properties// for option information.valsingersDF=(spark.read.format("cloud-spanner").option("projectId","PROJECT_ID").option("instanceId","INSTANCE_ID").option("databaseId","DATABASE_ID").option("table","TABLE_NAME").option("enableDataBoost",true).load().cache())singersDF.createOrReplaceTempView("Singers")// Load the Singers table.valresult=spark.sql("SELECT * FROM Singers")result.show()result.printSchema()}}
Replace the following:
PROJECT_ID: Your Google Cloud project ID.
Project IDs are listed in the Project info section on
the Google Cloud console Dashboard.
CONNECTOR_VERSION: Spanner connector version.
Choose the Spanner connector version from the version list in the GitHub
GoogleCloudDataproc/spark-spanner-connector repository.
Run singers.scala with the :load singers.scala command
to create the Spanner Singers table. The output
listing displays examplesfrom the Singers output.
The Spanner connector supports exporting the graph into separate
node and edge
DataFrames
as well as exporting into
GraphFrames
directly.
The following example exports a Spanner into a GraphFrame. It
uses the Python SpannerGraphConnectorclass, included in the
Spanner connector jar, to read the
Spanner Graph.
CONNECTOR_VERSION: Spanner connector version.
Choose the Spanner connector version from the version list in the GitHub
GoogleCloudDataproc/spark-spanner-connector repository.
PROJECT_ID: Your Google Cloud project ID.
Project IDs are listed in the Project info section on
the Google Cloud console Dashboard.
INSTANCE_ID, DATABASE_ID, and TABLE_NAME Insert
the instance, database, and graph IDs.
To export node and edge DataFrames instead of GraphFrames, use load_dfs
instead:
PROJECT_ID: The Google Cloud project ID.
Project IDs are listed in the Project info section on
the Google Cloud console Dashboard.
INSTANCE_ID, DATABASE_ID, and TABLE_NAME Insert
the instance, database, and table IDs.
Clean up
To avoid incurring ongoing charges to your Google Cloud account, you can
stop or
delete your
Managed Service for Apache Spark cluster and
delete your Spanner instance.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026年08月26日 UTC."],[],[]]