According to Combining Spark Streaming + MLlib it is possible to make a prediction over a stream of input in spark.
The issue with the given example (which works on my cluster) is that the testData is a given right on the correct format.
I am trying to set up a client <-> server tcp exchange based on strings of data. I can't figure out how to transform the string on the correct format.
while this works :
sep = ";"
str_recue = '0.0;0.1;0.2;0.3;0.4;0.5'
rdd = sc.parallelize([str_recue])
chemin = "hdfs://xx.xx.xx.xx:8020/cart_model_for_cycliste_v2"
model = DecisionTreeClassificationModel.load(chemin)
# travail sur la string
rdd2 = rdd.map( lambda data : data.split(sep))
rdd3 = rdd2.map(lambda tableau: [float(x) for x in tableau])
# création df
cols = ["c1", "c2", "c3", "c4", "c5", "c6"]
fields = [StructField(x, FloatType(), True) for x in cols]
schema = StructType(fields)
df = spark.createDataFrame(rdd3, schema=schema )
# preparation d'une colonne de features
schema = StructType(fields)
assembler = VectorAssembler()
assembler = assembler.setInputCols(cols)
assembler = assembler.setOutputCol("features")
df2 = assembler.transform(df)
model.transform(df2).show()
giving :
+---+---+---+---+---+---+--------------------+-------------+-----------+----------+
| c1| c2| c3| c4| c5| c6| features|rawPrediction|probability|prediction|
+---+---+---+---+---+---+--------------------+-------------+-----------+----------+
|0.0|0.1|0.2|0.3|0.4|0.5|[0.0,0.1000000014...| [0.0,3426.0]| [0.0,1.0]| 1.0|
+---+---+---+---+---+---+--------------------+-------------+-----------+----------+
I can't figure out how to make it works while listening to a socket.
I have my server :
import socket
import random
import time
port = 12003
ip = socket.gethostname()
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind((ip, port))
serversocket.listen(1)
(clientsocket, address) = serversocket.accept()
nb_d_envois = 10
tps_attente = 3
for i in range(nb_d_envois):
time.sleep(tps_attente)
sep = ";"
to_send = '0.0;0.1;0.2;0.3;0.4;0.5'
print(to_send)
clientsocket.send(to_send.encode())
which send a string to my spark Streaming context. What to do next ? This is my question. According to : https://github.com/apache/spark/blob/master/examples/src/main/python/streaming/sql_network_wordcount.py it should be possible to do a [foreach]
So I created a function :
def prevoir(time, rdd):
sep = ";"
chemin = "hdfs://54.37.12.49:8020/cart_model_for_cycliste_v2"
model = DecisionTreeClassificationModel.load(chemin)
# travail sur la string
rdd2 = rdd.map( lambda data : data.split(sep))
rdd3 = rdd2.map(lambda tableau: [float(x) for x in tableau])
# création df
cols = ["c1", "c2", "c3", "c4", "c5", "c6"]
fields = [StructField(x, FloatType(), True) for x in cols]
schema = StructType(fields)
df = spark.createDataFrame(rdd3, schema=schema )
# preparation d'une colonne de features
schema = StructType(fields)
assembler = VectorAssembler()
assembler = assembler.setInputCols(cols)
assembler = assembler.setOutputCol("features")
df2 = assembler.transform(df)
model.transform(df2).show()
and applied it on a streaming context :
ssc = StreamingContext(sc, 5)
dstream = ssc.socketTextStream(listen_to_ip, listen_to_port)
dstream.foreachRDD(prevoir)
but nothing appears (not even the normal time info). There is no errors neither.
My doubts are :
The function is not registered as UDF, so I am suspicious it can be called at all
the loading of the model through hdfs should certainly be done only once and passed as a parameter
the "show" function seems to me not really distributed (but it works when not applied on 'foreachrdd'... => maybe should I saeve something on hdfs ?
Any help welcome...