I am new to Apache Spark Streaming. I am developing a spark streaming application to find the shortest path and sending the path again back to the client. I have written code for taking data and processing it with a function but I have an issue how can I send my results again back to the client Here is my code:
import networkx as nx
from pyspark import SparkConf,SparkContext
from pyspark.streaming import StreamingContext
TCP_IP = "127.0.0.1"
TCP_PORT = 5000
# Creating a Spark Configuration
conf=SparkConf()
conf.setAppName('ShortestPathApp')
sc= SparkContext(conf)
ssc= StreamingContext(sc,2)
def shortestPath(line):
# get the values from rdd
vehicleId = line[0]
source = line[1]
destination = line[2]
deadline = line[3]
# find shortest path
shortest = nx.dijkstra_path(G, source, destination)
# receive from Socket
dataStream =ssc.socketTextStream(TCP_IP,TCP_PORT)
vehicle_data = dataStream.map(lambda line: line.split(" "))
vehicle_data.foreachRDD(lambda rdd: rdd.foreach(shortestPath))
ssc.start()
ssc.awaitTermination()
How can I send the data back to the client
Use StreamingContext
to push output data back to destination as a stream. you can create it as below. Add method to get singleton SparkSession instance
# Lazily instantiated global instance of SparkSession
def getSparkSessionInstance(sparkConf):
if ("sparkSessionSingletonInstance" not in globals()):
globals()["sparkSessionSingletonInstance"] = SparkSession \
.builder \
.config(conf=sparkConf) \
.getOrCreate()
return globals()["sparkSessionSingletonInstance"]
sparkSess = getSparkSessionInstance(rdd.context.getConf())
vehicle_data_df = sparkSess.createDataFrame(vehicle_data)
vehicle_data_df.writeStream\
.format("socket")\
.option("host",TCP_OUTPUT_IP) //Output socket IP address
.option("port",TCP_OUTPUT_PORT) //Output socket port
.outputMode('append')\
.start()\
.awaitTermination()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.