While develloping a SparkStreaming application (python), I'm not completely sure if I understand well how it works. I just have to read a json file stream (poping in a directory) and perform a join operation on each json object and a reference, and then, write it back to text files. here is my code:
config = configparser.ConfigParser()
config.read("config.conf")
def getSparkSessionInstance(sparkConf):
if ("sparkSessionSingletonInstance" not in globals()):
globals()["sparkSessionSingletonInstance"] = SparkSession \
.builder \
.config(conf=sparkConf) \
.getOrCreate()
return globals()["sparkSessionSingletonInstance"]
# Création du contexte
sc = SparkContext()
ssc = StreamingContext(sc, int(config["Variables"]["batch_period_spark"]))
sqlCtxt = getSparkSessionInstance(sc.getConf())
df_ref = sqlCtxt.read.json("file://" + config["Paths"]["path_ref"])
df_ref.createOrReplaceTempView("REF")
df_ref.cache()
output = config["Paths"]["path_DATAs_enri"]
# Fonction de traitement des DATAs
def process(rdd):
if rdd.count() > 0:
#print(rdd.toDebugString)
df_DATAs = sqlCtxt.read.json(rdd)
df_DATAs.createOrReplaceTempView("DATAs")
df_enri=sqlCtxt.sql("SELECT DATAs.*, REF.Name, REF.Mail FROM DATAs, REF WHERE DATAs.ID = REF.ID")
df_enri.createOrReplaceTempView("DATAs_enri")
df_enri.write.mode('append').json("file://" + output)
if(df_enri.count() < df_DATAs.count()):
df_fail = sqlCtxt.sql("SELECT * FROM DATAs WHERE DATAs.ID NOT IN (SELECT ID FROM DATAs_enri)")
df_fail.show()
# Configuration du stream et lancement
files = ssc.textFileStream("file://" + config["Paths"]["path_stream_DATAs"])
files.foreachRDD(process)
print("[GO]")
ssc.start()
ssc.awaitTermination()
Here is my spark config:
spark.master local[*]
spark.executor.memory 3g
spark.driver.memory 3g
spark.python.worker.memory 3g
spark.memory.fraction 0.9
spark.driver.maxResultSize 3g
spark.memory.storageFraction 0.9
spark.eventLog.enabled true
Well, it is working, but I have a question: The process is slow and the process delay is increasing. I am working in local[*], and I am afraid that there is no parrallelism... In the monitoring UI, I only see one executor and one job at a time. Is there any simpler way to do it? Like with the transform function on DStream? Is there a configuration variable I am missing?