Access OSS Tables from EMR Serverless Spark
Connect EMR Serverless Spark to OSS Tables through the Apache Iceberg REST Catalog, and use Spark SQL to create tables, write data, and run queries.
How it works
When EMR Serverless Spark accesses OSS Tables, metadata and data files travel through separate paths:
Metadata path: Spark sends catalog operations to
https://<REGION>-internal.oss-tables.aliyuncs.com/icebergthrough the Iceberg REST Catalog and uses Signature Version 4 (SigV4) for authentication. This path handles namespace creation, table creation, and snapshot management.Data path: Iceberg S3FileIO reads and writes Parquet data files through
https://oss-<REGION>-internal.aliyuncs.com.
The paths are independent. If you can create a table but cannot write data, the REST Catalog path is working. Check the S3FileIO endpoint, credentials, and checksum settings.
Prerequisites
Before you begin, make sure that you have:
An EMR Serverless Spark workspace in the same region as the Table Bucket
A Table Bucket in OSS Tables, with its name, region, and Alibaba Cloud account ID available
An OSS bucket for the dependency JAR files, and permission for the workspace execution role to read the bucket
A RAM user with permissions to access OSS Tables, and its AccessKey ID and AccessKey secret
Do not use the AccessKey pair of an Alibaba Cloud account. The following configuration stores the AccessKey pair in the SQL session's Spark configuration. Use a RAM user whose permissions are limited to the required OSS Tables operations. After verification, delete the session or remove the credentials.
Step 1: Prepare the dependency JAR files
This configuration was verified with EMR Serverless Spark engine version esr-4.9.1 (Spark 3.5.2 and Scala 2.12) and Iceberg 1.10.1. Download the JAR files and upload them to an OSS bucket that the EMR Serverless Spark workspace can read:
|
JAR file |
Description |
|
Provides the Iceberg integration for Spark. |
|
|
Provides S3FileIO, SigV4 signing, and AWS SDK dependencies. |
Example OSS object paths:
oss://<OSS_BUCKET>/oss-tables/iceberg-spark-runtime-3.5_2.12-1.10.1.jar
oss://<OSS_BUCKET>/oss-tables/iceberg-aws-bundle-1.10.1.jar
Use the same Iceberg version for both JAR files. If the engine uses a different Spark or Scala version, select the Iceberg Spark Runtime package that matches the engine version.
Step 2: Create and configure a SQL session
Log on to the E-MapReduce console and open the target EMR Serverless Spark workspace.
In the left-side navigation pane, choose Sessions. On the SQL Session tab, click Create SQL Session.
Configure Name, Resource Queue, and Engine Version. In this example, select
esr-4.9.1as the engine version.-
Under Custom Configuration, paste the following content into Spark Configuration and replace the placeholders.
spark.jars oss://<OSS_BUCKET>/oss-tables/iceberg-spark-runtime-3.5_2.12-1.10.1.jar,oss://<OSS_BUCKET>/oss-tables/iceberg-aws-bundle-1.10.1.jar spark.driver.userClassPathFirst true spark.executor.userClassPathFirst true spark.sql.extensions org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions spark.sql.catalog.oss_tables org.apache.iceberg.spark.SparkCatalog spark.sql.catalog.oss_tables.catalog-impl org.apache.iceberg.rest.RESTCatalog spark.sql.catalog.oss_tables.uri https://<REGION>-internal.oss-tables.aliyuncs.com/iceberg spark.sql.catalog.oss_tables.warehouse acs:osstables:<REGION>:<ACCOUNT_ID>:bucket/<TABLE_BUCKET_NAME> spark.sql.catalog.oss_tables.rest.sigv4-enabled true spark.sql.catalog.oss_tables.rest.signing-region <REGION> spark.sql.catalog.oss_tables.rest.signing-name osstables spark.sql.catalog.oss_tables.io-impl org.apache.iceberg.aws.s3.S3FileIO spark.sql.catalog.oss_tables.s3.endpoint https://oss-<REGION>-internal.aliyuncs.com spark.driver.extraJavaOptions -Daws.accessKeyId=<ACCESS_KEY_ID> -Daws.secretAccessKey=<ACCESS_KEY_SECRET> -Daws.region=<REGION> -Daws.requestChecksumCalculation=when_required -Daws.responseChecksumValidation=when_required spark.executor.extraJavaOptions -Daws.accessKeyId=<ACCESS_KEY_ID> -Daws.secretAccessKey=<ACCESS_KEY_SECRET> -Daws.region=<REGION> -Daws.requestChecksumCalculation=when_required -Daws.responseChecksumValidation=when_required
Replace the placeholders as follows:
|
Placeholder |
Description |
|
|
The name of the OSS bucket that stores the two dependency JAR files. |
|
|
The region of the Table Bucket, such as |
|
|
The ID of the Alibaba Cloud account that owns the Table Bucket. |
|
|
The name of the Table Bucket. |
|
|
The AccessKey ID of the RAM user that can access OSS Tables. |
|
|
The AccessKey secret that corresponds to the AccessKey ID. |
Do not configure spark.sql.catalog.oss_tables.credential. Otherwise, Iceberg sends an OAuth2 token request and returns HTTP 405. Set io-impl to org.apache.iceberg.aws.s3.S3FileIO. Do not use org.apache.iceberg.hadoop.HadoopFileIO.
After you finish the configuration, click Create. When the SQL session reaches Running, you can use the session. Restart the session after you change Spark Configuration so that the new configuration takes effect.
For information about other SQL session parameters, see Manage SQL sessions.
Step 3: Verify access with Spark SQL
In the left-side navigation pane of the EMR Serverless Spark workspace, choose Development.
Create a SparkSQL job and select the running SQL session.
-
Run the following SQL statements to create a namespace and table, and then insert, update, delete, and query data.
CREATE NAMESPACE IF NOT EXISTS oss_tables.emr_oss_tables_demo; CREATE TABLE IF NOT EXISTS oss_tables.emr_oss_tables_demo.events ( id BIGINT, category STRING, amount DECIMAL(10, 2) ) USING iceberg; INSERT INTO oss_tables.emr_oss_tables_demo.events VALUES (1, 'alpha', 10.50), (2, 'beta', 20.00), (3, 'alpha', 7.25); UPDATE oss_tables.emr_oss_tables_demo.events SET amount = 22.50 WHERE id = 2; DELETE FROM oss_tables.emr_oss_tables_demo.events WHERE id = 3; SELECT COUNT(*) AS row_count, SUM(amount) AS total_amount, MAX(amount) AS max_amount FROM oss_tables.emr_oss_tables_demo.events;
Expected result:
row_count total_amount max_amount
2 33.00 22.50
This result confirms that REST Catalog communication, SigV4 authentication, and S3FileIO data access are working. For instructions on creating a SparkSQL job, see SparkSQL development.
Troubleshooting
|
Error or symptom |
Cause |
Solution |
|
|
The REST Catalog SigV4 signer cannot obtain credentials. |
Make sure that the |
|
HTTP 405, with |
The |
Remove |
|
|
The external Iceberg JAR files conflict with the Iceberg classes built into the engine. |
Set |
|
Namespace and table creation succeed, but data writes fail with a connection error |
The S3FileIO endpoint is incorrect, or its region does not match the Table Bucket region. |
Set |
|
|
The default streaming checksum encoding of AWS SDK v2 is incompatible with the OSS S3-compatible API. |
Make sure that the |
Clean up resources
If you no longer need the resources created for this procedure, remove them to avoid unnecessary charges:
Delete the test table, and then delete the namespace. If the Table Bucket is used only for this test, delete the Table Bucket. For instructions, see OSS tables.
Stop and delete the SQL session in the EMR Serverless Spark workspace to remove the configured AccessKey pair.
Delete the SparkSQL test job if you no longer need it.
If the two Iceberg JAR files are used only for this test, delete the corresponding objects from the OSS bucket.
If the EMR Serverless Spark workspace is used only for this test, make sure that no jobs are running and delete the workspace.