-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_spark_connection.py
30 lines (26 loc) · 1.2 KB
/
test_spark_connection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import logging
from pyspark.sql import SparkSession
def create_spark_connection():
s_conn = None
spark_master_ip = 'localhost' # Replace with the actual Spark Master container IP address
try:
s_conn = SparkSession.builder \
.appName('SparkDataStreaming') \
.master(f'spark://{spark_master_ip}:7077') \
.config('spark.jars.packages', "com.datastax.spark:spark-cassandra-connector_2.12:3.4.1,"
"org.apache.spark:spark-sql-kafka-0-10_2.12:3.4.1,"
"org.apache.kafka:kafka-clients:3.3.2") \
.config('spark.cassandra.connection.host', '127.0.0.1') \
.getOrCreate()
s_conn.sparkContext.setLogLevel("ERROR")
logging.info("Spark connection created successfully!")
except Exception as e:
logging.error(f"Couldn't create the spark session due to exception {e}")
return s_conn
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
spark_conn = create_spark_connection()
if spark_conn:
logging.info("Spark session created successfully!")
else:
logging.error("Failed to create Spark session.")