-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.py
266 lines (239 loc) · 9.18 KB
/
consumer.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Import necessary packages
from kafka import KafkaConsumer
from kafka.errors import NoBrokersAvailable
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField, StringType, IntegerType
from pyspark.sql.functions import from_json, col, to_timestamp, date_format, concat_ws, udf
from cassandra.cluster import Cluster
import logging
import sys
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
from pymongo import MongoClient
from datetime import datetime
# Function to encrypt a value (UDF)
def encrypt_value(value):
key = b'YourPassword123@' # Replace with a secure key
cipher = Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend())
encryptor = cipher.encryptor()
# Use PKCS7 padding
padder = padding.PKCS7(algorithms.AES.block_size).padder()
padded_data = padder.update(value.encode()) + padder.finalize()
encrypted_value = encryptor.update(padded_data) + encryptor.finalize()
return encrypted_value
encrypt_udf = udf(lambda value: encrypt_value(value), StringType())
# Define a topic name
TOPIC_NAME = 'user_profiles'
print('Connecting to Kafka: ')
try:
consumer = KafkaConsumer(TOPIC_NAME, bootstrap_servers='localhost:9092')
print('Connection done !')
except NoBrokersAvailable as ne:
logging.error('No brokers available: %s', ne)
sys.exit(1)
# Create a Spark session
spark = SparkSession.builder.appName("KafkaCassandraIntegration").getOrCreate()
# Define the schema
schema = StructType([
StructField("gender", StringType(), True),
StructField("name", StructType([
StructField("title", StringType(), True),
StructField("first", StringType(), True),
StructField("last", StringType(), True)
]), True),
StructField("location", StructType([
StructField("street", StructType([
StructField("number", IntegerType(), True),
StructField("name", StringType(), True)
]), True),
StructField("city", StringType(), True),
StructField("state", StringType(), True),
StructField("country", StringType(), True),
StructField("postcode", StringType(), True),
StructField("coordinates", StructType([
StructField("latitude", StringType(), True),
StructField("longitude", StringType(), True)
]), True),
StructField("timezone", StructType([
StructField("offset", StringType(), True),
StructField("description", StringType(), True)
]), True)
]), True),
StructField("email", StringType(), True),
StructField("login", StructType([
StructField("uuid", StringType(), True),
StructField("username", StringType(), True),
StructField("password", StringType(), True),
StructField("salt", StringType(), True),
StructField("md5", StringType(), True),
StructField("sha1", StringType(), True),
StructField("sha256", StringType(), True)
]), True),
StructField("dob", StructType([
StructField("date", StringType(), True),
StructField("age", IntegerType(), True)
]), True),
StructField("registered", StructType([
StructField("date", StringType(), True),
StructField("age", IntegerType(), True)
]), True),
StructField("phone", StringType(), True),
StructField("cell", StringType(), True),
StructField("id", StructType([
StructField("name", StringType(), True),
StructField("value", StringType(), True)
]), True),
StructField("picture", StructType([
StructField("large", StringType(), True),
StructField("medium", StringType(), True),
StructField("thumbnail", StringType(), True)
]), True),
StructField("nat", StringType(), True)
])
# Connect to Cassandra
cluster = Cluster(['localhost'])
session = cluster.connect()
cassandra_keyspace = 'user_profiles'
# Create keyspace and table if they don't exist
session.execute(f"CREATE KEYSPACE IF NOT EXISTS {cassandra_keyspace} WITH replication = {{'class':'SimpleStrategy', 'replication_factor':1}}")
session.execute(f"USE {cassandra_keyspace}")
# Create the table if it does not exist
session.execute(
"""
CREATE TABLE IF NOT EXISTS users (
gender text,
complete_name text,
complete_address text,
timezone_offset text,
timezone_description text,
email text,
dob_date timestamp,
dob_year text,
dob_month text,
dob_day text,
dob_hours text,
dob_minutes text,
registration_date timestamp,
phone text,
cell text,
id_name text,
id_value text,
picture_thumbnail text,
nat text,
insertion_timestamp timestamp,
PRIMARY KEY (email, insertion_timestamp)
) WITH CLUSTERING ORDER BY (insertion_timestamp DESC);
"""
)
# Create the index if not exists
session.execute(
"""
CREATE INDEX IF NOT EXISTS idx_insertion_timestamp ON users (insertion_timestamp);
"""
)
# Prepare the insert statement
insert_statement = session.prepare(
"""
INSERT INTO users
(gender, complete_name, complete_address, timezone_offset,
timezone_description, email, dob_date, dob_year, dob_month, dob_day,
dob_hours, dob_minutes, registration_date, phone, cell, id_name,
id_value, picture_thumbnail, nat, insertion_timestamp)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, toUnixTimestamp(now()))
"""
)
# Mongo code -------------------------
# MongoDB connection
mongo_client = MongoClient("mongodb://localhost:27017")
mongo_db = mongo_client["user_profiles"]
mongo_collection = mongo_db["users"]
users_count = 0
for message in consumer:
# Convert the bytes object to a string
message_str = message.value.decode('utf-8')
# Create a dataframe
df = spark.createDataFrame([message_str], StringType())
# Parse the JSON data
parsed_df = df.selectExpr("CAST(value AS STRING)") \
.select(from_json("value", schema).alias("data")) \
.select("data.*")
transformed_df = parsed_df.withColumn(
"complete_name",
concat_ws(" ", col("name.first"), col("name.last"))
).withColumn(
"complete_address",
concat_ws(", ",
col("location.street.number").cast("string"),
col("location.street.name"),
col("location.city"),
col("location.state"),
col("location.country"),
col("location.postcode")
)
).withColumn(
"dob_date", to_timestamp(col("dob.date"), "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
).withColumn(
"dob_day", date_format(col("dob_date"), "d")
).withColumn(
"dob_month", date_format(col("dob_date"), "M")
).withColumn(
"dob_year", date_format(col("dob_date"), "y")
).withColumn(
"dob_hours", date_format(col("dob_date"), "H")
).withColumn(
"dob_minutes", date_format(col("dob_date"), "m")
).withColumn(
"registration_date",
to_timestamp(col("registered.date"), "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
).withColumn(
'email', encrypt_udf("email")
).withColumn(
'phone', encrypt_udf("phone")
).withColumn(
'cell', encrypt_udf("cell")
).withColumn(
'complete_name', encrypt_udf("complete_name")
).withColumn(
'complete_address', encrypt_udf("complete_address")
)
# Select the required columns
transformed_df = transformed_df.select(
"gender", "complete_name", "complete_address", "location.timezone.offset",
"location.timezone.description", "email", "dob_date","dob_year", "dob_month",
"dob_day", "dob_hours", "dob_minutes", "registration_date", "phone", "cell",
"id.name", "id.value", "picture.thumbnail", "nat"
)
try:
# Convert the DataFrame to a list of values
values = transformed_df.rdd.map(list).collect()
# Insert the data into the Cassandra table
session.execute(insert_statement, values[0])
# Create a document with the specified fields
age = datetime.now().year - int(values[0][7])
document = {
"gender": values[0][0],
"age": age,
"complete_name": encrypt_value(values[0][1]),
"complete_address": encrypt_value(values[0][2]),
"timezone_offset": values[0][3],
"timezone_description": values[0][4],
"dob_date": values[0][6],
"dob_year": values[0][7],
"dob_month": values[0][8],
"dob_day": values[0][9],
"dob_hours": values[0][10],
"dob_minutes": values[0][11],
"registration_date": values[0][12],
"id_name": values[0][15],
"id_value": values[0][16],
"picture_thumbnail": values[0][17],
"nat": values[0][18],
"insertion_timestamp": datetime.now()
}
# Insert the document into the collection
mongo_collection.insert_one(document)
users_count += 1
print(f"User {users_count} inserted successfully in Cassandra and MongoDB.")
except Exception as e:
print('Error: ',e)