-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(airflow): create SparkSqlOnK8SOperator to execute Spark SQL comm…
…ands (#109)
- Loading branch information
1 parent
8ae31ca
commit d178937
Showing
4 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,50 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
|
||
from pyspark.sql import SparkSession | ||
|
||
logger = logging.getLogger("SparkSqlOnK8s") | ||
|
||
|
||
def read_sql_file(file_path): | ||
""" | ||
Reads an SQL file and splits commands by semicolon ";". | ||
""" | ||
with open(file_path) as f: | ||
file_content = f.read() | ||
# Split commands by semicolon while ignoring empty lines | ||
return [command.strip() for command in file_content.split(";") if command.strip()] | ||
|
||
|
||
def execute_sql_commands(spark_session: SparkSession, commands): | ||
""" | ||
Executes a list of SQL commands on a Spark session. | ||
""" | ||
for command in commands: | ||
try: | ||
logger.info(f"Executing: {command}") | ||
spark_session.sql(command).show(truncate=False) | ||
except Exception: | ||
logger.exception(f"Error executing SQL command: {command}") | ||
|
||
|
||
if __name__ == "__main__": | ||
import sys | ||
|
||
if len(sys.argv) != 2: | ||
logger.error("Usage: spark-submit run_sql_commands.py <path_to_sql_file>") | ||
sys.exit(1) | ||
|
||
sql_file_path = sys.argv[1] | ||
|
||
_spark_session = SparkSession.builder.getOrCreate() | ||
|
||
# Read SQL commands from file | ||
sql_commands = read_sql_file(sql_file_path) | ||
|
||
# Execute each SQL command | ||
execute_sql_commands(_spark_session, sql_commands) | ||
|
||
# Stop SparkSession | ||
_spark_session.stop() |
This file contains 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 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 |
---|---|---|
@@ -1,5 +1,18 @@ | ||
from __future__ import annotations | ||
|
||
import subprocess | ||
import sys | ||
|
||
import pytest | ||
|
||
PYTHON_313_OR_ABOVE = sys.version_info.major == 3 and sys.version_info.minor >= 13 | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def root_path() -> str: | ||
return ( | ||
subprocess.Popen(["git", "rev-parse", "--show-toplevel"], stdout=subprocess.PIPE) | ||
.communicate()[0] | ||
.rstrip() | ||
.decode("utf-8") | ||
) |