gh-yzou commented on code in PR #1985: URL: https://github.com/apache/polaris/pull/1985#discussion_r2180721471
########## integration-tests/src/main/java/org/apache/polaris/service/it/ext/SparkSessionBuilder.java: ########## @@ -0,0 +1,197 @@ +/* + * 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>Example usage: + * + * <pre> + * SparkSession session = SparkSessionBuilder + * .withTestDefaults() + * .addCatalog("catalog1", "org.apache.iceberg.spark.SparkCatalog", endpoints, token) + * .addCatalog("catalog2", "org.apache.polaris.spark.SparkCatalog", endpoints, token) + * .createSession(); + * </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 catalogType, PolarisApiEndpoints endpoints, String token) { + this.catalogs.add( + new CatalogConfig(catalogName, catalogType, endpoints, token, new ArrayList<>())); + return this; + } + + public SparkSessionBuilder withConfig(String key, String value) { + this.additionalConfigs.add(new ConfigPair(key, value)); + return this; + } + + public SparkSession createSession() { Review Comment: Oh, sorry, i don't mean to extend the SparkSession.build function, I was suggesting to rename the function name from createSession() to getOrCreate() -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
