-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspark.py
More file actions
163 lines (137 loc) · 5.92 KB
/
spark.py
File metadata and controls
163 lines (137 loc) · 5.92 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import logging
from datetime import datetime
from uuid import uuid4
from cassandra.auth import PlainTextAuthProvider
from cassandra.cluster import Cluster
from pyspark.sql import SparkSession
from pyspark.sql.functions import from_json, col
from pyspark.sql.types import StructType, StructField, StringType, IntegerType
logging.basicConfig(level=logging.INFO)
def create_keyspace(session):
session.execute("""
CREATE KEYSPACE IF NOT EXISTS spark_stream
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}
""")
logging.info("Keyspace created successfully")
def create_table(session):
session.execute("""
CREATE TABLE IF NOT EXISTS spark_stream.users (
id UUID PRIMARY KEY,
first_name TEXT,
last_name TEXT,
gender TEXT,
address TEXT,
username TEXT,
age INT,
pincode TEXT,
email TEXT,
registered_date TEXT,
phone_number TEXT,
image TEXT
);
""")
logging.info("Table created successfully")
def insert_data(session, **kwargs):
logging.info("Inserting data...")
uid = kwargs.get('id', str(uuid4()))
first_name = kwargs.get('first_name')
last_name = kwargs.get('last_name')
gender = kwargs.get('gender')
address = kwargs.get('address')
username = kwargs.get('username')
age = kwargs.get('age')
pincode = kwargs.get('pincode')
email = kwargs.get('email')
registered_date = kwargs.get('registered_date')
phone = kwargs.get('phone_number')
picture = kwargs.get('image')
try:
session.execute("""
INSERT INTO spark_stream.users (
id, first_name, last_name, gender, address, username, age, pincode, email, registered_date, phone, picture
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
""", (uid, first_name, last_name, gender, address, username, age, pincode, email, registered_date, phone, picture))
logging.info(f"Data inserted for {username} with id: {uid}")
except Exception as e:
logging.error(f"Could not insert data due to {e}")
def spark_connection():
try:
spark = SparkSession.builder \
.appName('SparkDataStreaming') \
.config('spark.jars.packages', 'com.datastax.spark:spark-cassandra-connector_2.12:3.5.1,org.apache.spark:spark-sql-kafka-0-10_2.12:3.5.3') \
.getOrCreate()
# .config('spark.cassandra.connection.host', 'localhost') \
# .config("spark.cassandra.connection.port", "9042") \
spark.sparkContext.setLogLevel("ERROR")
logging.info("Spark connection established successfully")
return spark
except Exception as e:
logging.error(f"Could not establish Spark session due to {e}")
return None
def kafka_connection(spark):
try:
#set localhost:9092 if running locally
spark_df = spark.readStream \
.format('kafka') \
.option('kafka.bootstrap.servers', 'localhost:9092') \
.option('subscribe', 'users_created') \
.option('startingOffset', 'earliest') \
.option("failOnDataLoss", "false") \
.load()
logging.info("Kafka DataFrame created successfully")
return spark_df
except Exception as e:
logging.warning(f"Kafka DataFrame could not be created due to {e}")
return None
def selection_from_kafka(spark_df):
schema = StructType([
StructField("id", StringType(), False),
StructField("first_name", StringType(), False),
StructField("last_name", StringType(), False),
StructField("gender", StringType(), False),
StructField("address", StringType(), False),
StructField("username", StringType(), False),
StructField("age", IntegerType(), False),
StructField("pincode", StringType(), False),
StructField("email", StringType(), False),
StructField("registered_date", StringType(), False),
StructField("phone_number", StringType(), False),
StructField("image", StringType(), False),
])
selection = spark_df.selectExpr("CAST(value AS STRING)") \
.select(from_json(col('value'), schema).alias('data')) \
.select("data.*")
return selection
def cassandra_connection():
try:
cluster = Cluster(['localhost'], port = 9045, protocol_version=5, connect_timeout=10)
#localhost 9045 if running locally
session = cluster.connect()
logging.info("Cassandra connection established successfully")
return session
except Exception as e:
logging.error(f"Could not connect to Cassandra cluster due to {e}")
return None
if __name__ == "__main__":
spark_con = spark_connection()
spark_con.conf.set("spark.cassandra.connection.host", "localhost")
spark_con.conf.set("spark.cassandra.connection.port", "9045")
#set the connection to localhost 9045 if running locally
if spark_con:
spark_df = kafka_connection(spark_con)
if spark_df:
selection_df = selection_from_kafka(spark_df)
session = cassandra_connection()
if session:
create_keyspace(session)
create_table(session)
print(selection_df)
# # Start streaming and write to Cassandra
streaming_query = (selection_df.writeStream \
.format("org.apache.spark.sql.cassandra") \
.outputMode("append") \
.option("checkpointLocation", "/tmp/checkpoint") \
.option("keyspace", "spark_stream") \
.option("table", "users") \
.start())
streaming_query.awaitTermination()