Class Receiver<T>
Object
org.apache.spark.streaming.receiver.Receiver<T>
- All Implemented Interfaces:
Serializable
:: DeveloperApi ::
Abstract class of a receiver that can be run on worker nodes to receive external data. A
custom receiver can be defined by defining the functions
onStart() and onStop(). onStart()
should define the setup steps necessary to start receiving data,
and onStop() should define the cleanup steps necessary to stop receiving data.
Exceptions while receiving can be handled either by restarting the receiver with restart(...)
or stopped completely by stop(...).
A custom receiver in Scala would look like this.
class MyReceiver(storageLevel: StorageLevel) extends NetworkReceiver[String](storageLevel) {
def onStart() {
// Setup stuff (start threads, open sockets, etc.) to start receiving data.
// Must start new thread to receive data, as onStart() must be non-blocking.
// Call store(...) in those threads to store received data into Spark's memory.
// Call stop(...), restart(...) or reportError(...) on any thread based on how
// different errors need to be handled.
// See corresponding method documentation for more details
}
def onStop() {
// Cleanup stuff (stop threads, close sockets, etc.) to stop receiving data.
}
}
A custom receiver in Java would look like this.
class MyReceiver extends Receiver<String> {
public MyReceiver(StorageLevel storageLevel) {
super(storageLevel);
}
public void onStart() {
// Setup stuff (start threads, open sockets, etc.) to start receiving data.
// Must start new thread to receive data, as onStart() must be non-blocking.
// Call store(...) in those threads to store received data into Spark's memory.
// Call stop(...), restart(...) or reportError(...) on any thread based on how
// different errors need to be handled.
// See corresponding method documentation for more details
}
public void onStop() {
// Cleanup stuff (stop threads, close sockets, etc.) to stop receiving data.
}
}
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanCheck if the receiver has started or not.booleanCheck if receiver has been marked for stopping.abstract voidonStart()This method is called by the system when the receiver is started.abstract voidonStop()This method is called by the system when the receiver is stopped.scala.Option<String>Override this to specify a preferred location (hostname).voidreportError(String message, Throwable throwable) Report exceptions in receiving data.voidRestart the receiver.voidRestart the receiver.voidRestart the receiver.voidStop the receiver completely.voidStop the receiver completely due to an exceptionvoidstore(ByteBuffer bytes) Store the bytes of received data as a data block into Spark's memory.voidstore(ByteBuffer bytes, Object metadata) Store the bytes of received data as a data block into Spark's memory.voidStore an iterator of received data as a data block into Spark's memory.voidStore an iterator of received data as a data block into Spark's memory.voidStore an iterator of received data as a data block into Spark's memory.voidStore an iterator of received data as a data block into Spark's memory.voidStore an ArrayBuffer of received data as a data block into Spark's memory.voidStore an ArrayBuffer of received data as a data block into Spark's memory.voidStore a single item of received data to Spark's memory.intstreamId()Get the unique identifier the receiver input stream that this receiver is associated with.
-
Constructor Details
-
Receiver
-
-
Method Details
-
isStarted
public boolean isStarted()Check if the receiver has started or not. -
isStopped
public boolean isStopped()Check if receiver has been marked for stopping. Use this to identify when the receiving of data should be stopped.- Returns:
- (undocumented)
-
onStart
public abstract void onStart()This method is called by the system when the receiver is started. This function must initialize all resources (threads, buffers, etc.) necessary for receiving data. This function must be non-blocking, so receiving the data must occur on a different thread. Received data can be stored with Spark by callingstore(data).If there are errors in threads started here, then following options can be done (i)
reportError(...)can be called to report the error to the driver. The receiving of data will continue uninterrupted. (ii)stop(...)can be called to stop receiving data. This will callonStop()to clear up all resources allocated (threads, buffers, etc.) duringonStart(). (iii)restart(...)can be called to restart the receiver. This will callonStop()immediately, and thenonStart()after a delay. -
onStop
public abstract void onStop()This method is called by the system when the receiver is stopped. All resources (threads, buffers, etc.) set up inonStart()must be cleaned up in this method. -
preferredLocation
Override this to specify a preferred location (hostname). -
reportError
Report exceptions in receiving data. -
restart
Restart the receiver. This method schedules the restart and returns immediately. The stopping and subsequent starting of the receiver (by callingonStop()andonStart()) is performed asynchronously in a background thread. The delay between the stopping and the starting is defined by the Spark configurationspark.streaming.receiverRestartDelay. Themessagewill be reported to the driver.- Parameters:
message- (undocumented)
-
restart
Restart the receiver. This method schedules the restart and returns immediately. The stopping and subsequent starting of the receiver (by callingonStop()andonStart()) is performed asynchronously in a background thread. The delay between the stopping and the starting is defined by the Spark configurationspark.streaming.receiverRestartDelay. Themessageandexceptionwill be reported to the driver.- Parameters:
message- (undocumented)error- (undocumented)
-
restart
Restart the receiver. This method schedules the restart and returns immediately. The stopping and subsequent starting of the receiver (by callingonStop()andonStart()) is performed asynchronously in a background thread.- Parameters:
message- (undocumented)error- (undocumented)millisecond- (undocumented)
-
stop
Stop the receiver completely. -
stop
Stop the receiver completely due to an exception -
storageLevel
-
store
Store a single item of received data to Spark's memory. These single items will be aggregated together into data blocks before being pushed into Spark's memory.- Parameters:
dataItem- (undocumented)
-
store
Store an ArrayBuffer of received data as a data block into Spark's memory. -
store
Store an ArrayBuffer of received data as a data block into Spark's memory. The metadata will be associated with this block of data for being used in the corresponding InputDStream.- Parameters:
dataBuffer- (undocumented)metadata- (undocumented)
-
store
Store an iterator of received data as a data block into Spark's memory. -
store
Store an iterator of received data as a data block into Spark's memory. The metadata will be associated with this block of data for being used in the corresponding InputDStream.- Parameters:
dataIterator- (undocumented)metadata- (undocumented)
-
store
Store an iterator of received data as a data block into Spark's memory. -
store
Store an iterator of received data as a data block into Spark's memory. The metadata will be associated with this block of data for being used in the corresponding InputDStream.- Parameters:
dataIterator- (undocumented)metadata- (undocumented)
-
store
Store the bytes of received data as a data block into Spark's memory. Note that the data in the ByteBuffer must be serialized using the same serializer that Spark is configured to use.- Parameters:
bytes- (undocumented)
-
store
Store the bytes of received data as a data block into Spark's memory. The metadata will be associated with this block of data for being used in the corresponding InputDStream.- Parameters:
bytes- (undocumented)metadata- (undocumented)
-
streamId
public int streamId()Get the unique identifier the receiver input stream that this receiver is associated with.- Returns:
- (undocumented)
-