|
| 1 | +import pytest |
| 2 | +import os |
| 3 | +from pyspark.sql import SparkSession |
| 4 | + |
| 5 | +CATALOG_URL = "http://localhost:8080" |
| 6 | +WAREHOUSE_ID = "test_db" |
| 7 | +AWS_REGION = os.getenv("AWS_REGION") |
| 8 | +AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID") |
| 9 | +AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY") |
| 10 | + |
| 11 | +@pytest.fixture() |
| 12 | +def rest_spark_session(): |
| 13 | + spark_session = spark_session_factory(config_type='file_catalog', app_name='FileCatalogTest') |
| 14 | + yield spark_session |
| 15 | + spark_session.stop() |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture() |
| 19 | +def s3_spark_session(): |
| 20 | + spark_session = spark_session_factory(config_type='s3_catalog', app_name='S3CatalogTest') |
| 21 | + yield spark_session |
| 22 | + spark_session.stop() |
| 23 | + |
| 24 | + |
| 25 | +def spark_session_factory( |
| 26 | + config_type: str, |
| 27 | + app_name: str, |
| 28 | + config_overrides: dict = None |
| 29 | + ): |
| 30 | + """ |
| 31 | + The actual function that builds the SparkSession. |
| 32 | +
|
| 33 | + Args: |
| 34 | + config_type (str): The type of configuration to use. |
| 35 | + Expected values: 'file_catalog' or 's3_catalog'. |
| 36 | + app_name (str): The name for the Spark application. |
| 37 | + config_overrides (dict): A dictionary of Spark configs to add or |
| 38 | + override the base configuration. |
| 39 | +
|
| 40 | + Returns: |
| 41 | + SparkSession: An initialized SparkSession object. |
| 42 | + """ |
| 43 | + print(f"\nBuilding Spark session with config_type='{config_type}'...") |
| 44 | + builder = SparkSession.builder.appName(app_name) |
| 45 | + |
| 46 | + # --- Configuration Type 1: REST Client with SimpleAWSCredentialsProvider --- |
| 47 | + if config_type == 'file_catalog': |
| 48 | + builder.config("spark.driver.memory", "15g") \ |
| 49 | + .config("spark.jars.packages", "org.apache.iceberg:iceberg-spark-runtime-3.5_2.12:1.9.1") \ |
| 50 | + .config("spark.driver.extraJavaOptions", "-Dlog4j.configurationFile=log4j2.properties") \ |
| 51 | + .config("spark.hadoop.fs.s3a.path.style.access", "true") \ |
| 52 | + .config("spark.hadoop.fs.s3a.change.detection.mode", "error") \ |
| 53 | + .config("spark.hadoop.fs.s3a.change.detection.version.required", "false") \ |
| 54 | + .config("spark.hadoop.fs.s3a.multiobjectdelete.enable", "true") \ |
| 55 | + .config("spark.hadoop.fs.s3a.impl", "org.apache.iceberg.hadoop.HadoopFileIO") \ |
| 56 | + .config("spark.hadoop.fs.s3a.aws.credentials.provider", "org.apache.hadoop.fs.s3a.SimpleAWSCredentialsProvider") \ |
| 57 | + .config("spark.sql.extensions", "org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions") \ |
| 58 | + .config("spark.sql.catalog.rest", "org.apache.iceberg.spark.SparkCatalog") \ |
| 59 | + .config("spark.sql.catalog.rest.catalog-impl", "org.apache.iceberg.rest.RESTCatalog") \ |
| 60 | + .config("spark.sql.catalog.rest.io-impl", "org.apache.iceberg.hadoop.HadoopFileIO") \ |
| 61 | + .config("spark.sql.catalog.rest.uri", CATALOG_URL) \ |
| 62 | + .config("spark.sql.catalog.rest.warehouse", WAREHOUSE_ID) \ |
| 63 | + .config("spark.sql.defaultCatalog", "rest") |
| 64 | + |
| 65 | + # --- Configuration Type 2: Direct S3 Access with Explicit Keys --- |
| 66 | + elif config_type == 's3_catalog': |
| 67 | + builder.config("spark.driver.memory", "15g") \ |
| 68 | + .config("spark.jars.packages", "org.apache.iceberg:iceberg-spark-runtime-3.5_2.12:1.9.1,org.apache.hadoop:hadoop-aws:3.3.4") \ |
| 69 | + .config("spark.hadoop.fs.s3a.impl", "org.apache.hadoop.fs.s3a.S3AFileSystem") \ |
| 70 | + .config("spark.hadoop.fs.s3a.path.style.access", "true") \ |
| 71 | + .config("spark.sql.extensions", "org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions") \ |
| 72 | + .config("spark.sql.catalog.rest", "org.apache.iceberg.spark.SparkCatalog") \ |
| 73 | + .config("spark.sql.catalog.rest.catalog-impl", "org.apache.iceberg.rest.RESTCatalog") \ |
| 74 | + .config("spark.sql.catalog.rest.io-impl", "org.apache.iceberg.hadoop.HadoopFileIO") \ |
| 75 | + .config("spark.sql.catalog.rest.uri", CATALOG_URL) \ |
| 76 | + .config("spark.sql.catalog.rest.warehouse", WAREHOUSE_ID) \ |
| 77 | + .config("spark.sql.defaultCatalog", "rest") |
| 78 | + else: |
| 79 | + raise ValueError(f"Unknown config_type: '{config_type}'. Expected 'file_catalog' or 's3_catalog'.") |
| 80 | + |
| 81 | + # Apply any specific overrides for the test |
| 82 | + if config_overrides: |
| 83 | + for key, value in config_overrides.items(): |
| 84 | + builder.config(key, value) |
| 85 | + |
| 86 | + spark_session = builder.getOrCreate() |
| 87 | + return spark_session |
0 commit comments