-
Notifications
You must be signed in to change notification settings - Fork 357
Add a Spark session builder for the tests #1985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
213 changes: 213 additions & 0 deletions
213
integration-tests/src/main/java/org/apache/polaris/service/it/ext/SparkSessionBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.polaris.service.it.ext; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import java.net.URI; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import org.apache.polaris.service.it.env.PolarisApiEndpoints; | ||
| import org.apache.spark.sql.SparkSession; | ||
|
|
||
| /** | ||
| * A fluent builder for configuring SparkSession instances with Polaris catalogs. | ||
| * | ||
| * <p>This builder creates a SparkSession with sensible test defaults and allows easy configuration | ||
| * of multiple Polaris catalogs. The resulting SparkSession will be configured for local execution | ||
| * with S3Mock support for testing. | ||
| * | ||
| * <p>Example usage: | ||
| * | ||
| * <pre> | ||
| * SparkSession session = SparkSessionBuilder | ||
| * .buildWithTestDefaults() | ||
| * .withExtensions("org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions") | ||
| * .withWarehouse(warehouseUri) | ||
| * .addCatalog("catalog1", "org.apache.iceberg.spark.SparkCatalog", endpoints, token) | ||
| * .addCatalog("catalog2", "org.apache.polaris.spark.SparkCatalog", endpoints, token) | ||
| * .createSession(); | ||
| * </pre> | ||
| * | ||
| * <p>The final SparkSession will be configured with: | ||
| * | ||
| * <ul> | ||
| * <li>Local master execution (local[1]) | ||
| * <li>Disabled Spark UI for clean test output | ||
| * <li>S3A filesystem with mock credentials (foo/bar) | ||
| * <li>Multiple Polaris catalogs with REST endpoints | ||
| * <li>Iceberg extensions for table format support | ||
| * <li>Custom warehouse directory location | ||
| * </ul> | ||
| * | ||
| * <p>Each catalog will be configured as: | ||
| * | ||
| * <pre> | ||
| * spark.sql.catalog.{catalogName} = {catalogType} | ||
| * spark.sql.catalog.{catalogName}.type = rest | ||
| * spark.sql.catalog.{catalogName}.uri = {polarisEndpoint} | ||
| * spark.sql.catalog.{catalogName}.token = {authToken} | ||
| * spark.sql.catalog.{catalogName}.warehouse = {catalogName} | ||
| * spark.sql.catalog.{catalogName}.scope = PRINCIPAL_ROLE:ALL | ||
| * </pre> | ||
| */ | ||
| public class SparkSessionBuilder { | ||
| private final SparkSession.Builder builder; | ||
| private final List<CatalogConfig> catalogs = new ArrayList<>(); | ||
| private final List<ConfigPair> additionalConfigs = new ArrayList<>(); | ||
|
|
||
| private String extensions = "org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions"; | ||
| private URI warehouseDir; | ||
|
|
||
| private SparkSessionBuilder(SparkSession.Builder builder) { | ||
| this.builder = builder; | ||
| } | ||
|
|
||
| /** | ||
| * Create a SparkSessionBuilder with common test defaults | ||
| * | ||
| * @return new builder instance with test defaults | ||
| */ | ||
| public static SparkSessionBuilder buildWithTestDefaults() { | ||
| // local master | ||
| var builder = SparkSession.builder(); | ||
| builder.master(String.format("local[%d]", 1)); | ||
| // disable UI | ||
| builder.config("spark.ui.showConsoleProgress", "false"); | ||
| builder.config("spark.ui.enabled", "false"); | ||
|
|
||
| var sparkSessionbuilder = new SparkSessionBuilder(builder); | ||
| sparkSessionbuilder.withS3MockContainer(); | ||
| return sparkSessionbuilder; | ||
| } | ||
|
|
||
| private void withS3MockContainer() { | ||
| withConfig("spark.hadoop.fs.s3.impl", "org.apache.hadoop.fs.s3a.S3AFileSystem") | ||
| .withConfig( | ||
| "spark.hadoop.fs.s3.aws.credentials.provider", | ||
| "org.apache.hadoop.fs.s3.TemporaryAWSCredentialsProvider") | ||
| .withConfig("spark.hadoop.fs.s3.access.key", "foo") | ||
| .withConfig("spark.hadoop.fs.s3.secret.key", "bar"); | ||
| } | ||
|
|
||
| public SparkSessionBuilder withWarehouse(URI warehouseDir) { | ||
| this.warehouseDir = warehouseDir; | ||
| return this; | ||
| } | ||
|
|
||
| public SparkSessionBuilder withExtensions(String extensions) { | ||
| this.extensions = extensions; | ||
| return this; | ||
| } | ||
|
|
||
| public SparkSessionBuilder addCatalog( | ||
| String catalogName, String catalogImplClass, PolarisApiEndpoints endpoints, String token) { | ||
| this.catalogs.add(new CatalogConfig(catalogName, catalogImplClass, endpoints, token)); | ||
| return this; | ||
| } | ||
|
|
||
| public SparkSessionBuilder withConfig(String key, String value) { | ||
| this.additionalConfigs.add(new ConfigPair(key, value)); | ||
| return this; | ||
| } | ||
|
|
||
| public SparkSession getOrCreate() { | ||
| if (extensions != null) { | ||
| builder.config("spark.sql.extensions", extensions); | ||
| } | ||
|
|
||
| if (warehouseDir != null) { | ||
| builder.config("spark.sql.warehouse.dir", warehouseDir.toString()); | ||
| } | ||
|
|
||
| // Apply catalog configurations | ||
| applyCatalogConfigurations(); | ||
|
|
||
| // Apply additional configurations | ||
| applyAdditionalConfigurations(); | ||
|
|
||
| return builder.getOrCreate(); | ||
| } | ||
|
|
||
| private void applyCatalogConfigurations() { | ||
| for (CatalogConfig catalog : catalogs) { | ||
| applySingleCatalogConfig(catalog); | ||
| } | ||
| } | ||
|
|
||
| private void applySingleCatalogConfig(CatalogConfig catalog) { | ||
| // Basic catalog configuration | ||
| builder | ||
| .config( | ||
| String.format("spark.sql.catalog.%s", catalog.catalogName), catalog.catalogImplClass) | ||
| .config(String.format("spark.sql.catalog.%s.type", catalog.catalogName), "rest") | ||
| .config( | ||
| String.format("spark.sql.catalog.%s.warehouse", catalog.catalogName), | ||
| catalog.catalogName) | ||
| .config( | ||
| String.format("spark.sql.catalog.%s.scope", catalog.catalogName), "PRINCIPAL_ROLE:ALL"); | ||
|
|
||
| // Add endpoint configuration | ||
| Preconditions.checkNotNull(catalog.endpoints, "endpoints is required"); | ||
| builder | ||
| .config( | ||
| String.format("spark.sql.catalog.%s.uri", catalog.catalogName), | ||
| catalog.endpoints.catalogApiEndpoint().toString()) | ||
| .config( | ||
| String.format("spark.sql.catalog.%s.header.realm", catalog.catalogName), | ||
| catalog.endpoints.realmId()); | ||
|
|
||
| // Add token configuration | ||
| if (catalog.token != null) { | ||
| builder.config( | ||
| String.format("spark.sql.catalog.%s.token", catalog.catalogName), catalog.token); | ||
| } | ||
| } | ||
|
|
||
| private void applyAdditionalConfigurations() { | ||
| for (ConfigPair config : additionalConfigs) { | ||
| builder.config(config.key, config.value); | ||
| } | ||
| } | ||
|
|
||
| private static class ConfigPair { | ||
| final String key; | ||
| final String value; | ||
|
|
||
| ConfigPair(String key, String value) { | ||
| this.key = key; | ||
| this.value = value; | ||
| } | ||
| } | ||
|
|
||
| /** Configuration for a single catalog. */ | ||
| private static class CatalogConfig { | ||
| final String catalogName; | ||
| final String catalogImplClass; | ||
| final PolarisApiEndpoints endpoints; | ||
| final String token; | ||
|
|
||
| CatalogConfig( | ||
| String catalogName, String catalogImplClass, PolarisApiEndpoints endpoints, String token) { | ||
| this.catalogName = catalogName; | ||
| this.catalogImplClass = catalogImplClass; | ||
| this.endpoints = endpoints; | ||
| this.token = token; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be also useful to have an example about the actual sparkSession build result with in the comment also
like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you elaborate it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, i just mean in the comment also add description about how the final effect will look like to help illustrate the usage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add more descriptions as the Java class doc.