The Interactive Shell provides the main REPL functionality with Spark integration, command processing, and session management. It serves as the foundation for the spark-shell command-line tool.
The primary entry point for the Spark REPL application, providing global access to the interpreter instance.
/**
* Entry point object for the Spark REPL application
*/
object Main extends Logging {
/**
* Application entry point that starts the REPL
* @param args Command line arguments passed to the REPL
*/
def main(args: Array[String]): Unit
/**
* Gets the current interpreter instance
* @return Current SparkILoop instance
*/
def interp: SparkILoop
/**
* Sets the interpreter instance
* @param i SparkILoop instance to set
*/
def interp_=(i: SparkILoop): Unit
}Usage Examples:
import org.apache.spark.repl.Main
// Start REPL with default settings
Main.main(Array())
// Start with specific arguments
Main.main(Array("-master", "local[*]", "-i", "init.scala"))
// Access current interpreter
val currentRepl = Main.interpThe main interactive loop class that provides the read-eval-print functionality with Spark-specific features.
/**
* The Scala interactive shell with Spark integration
* @param in0 Optional input reader for commands
* @param out Output writer for results
* @param master Optional Spark master URL
*/
@DeveloperApi
class SparkILoop(
private val in0: Option[BufferedReader] = None,
protected val out: JPrintWriter = new JPrintWriter(Console.out, true),
val master: Option[String] = None
) extends AnyRef with LoopCommands with SparkILoopInit with Logging {
/**
* Constructor with BufferedReader and JPrintWriter
*/
def this(in0: BufferedReader, out: JPrintWriter) =
this(Some(in0), out, None)
/**
* Constructor with BufferedReader, JPrintWriter, and master URL
*/
def this(in0: BufferedReader, out: JPrintWriter, master: String) =
this(Some(in0), out, Some(master))
/**
* Default constructor
*/
def this() = this(None, new JPrintWriter(Console.out, true), None)
}Core Properties:
/**
* The active Spark context for the REPL session
*/
@DeveloperApi
var sparkContext: SparkContext
/**
* Sets the REPL prompt string
* @param prompt New prompt string to display
*/
@DeveloperApi
def setPrompt(prompt: String): Unit
/**
* Gets the current prompt string
* @return Current prompt string
*/
@DeveloperApi
def prompt: String
/**
* Gets the list of available REPL commands
* @return List of LoopCommand instances
*/
@DeveloperApi
def commands: List[LoopCommand]Spark Integration Methods:
/**
* Creates and initializes a SparkSession for the REPL
* @return Configured SparkSession instance
*/
@DeveloperApi
def createSparkSession(): SparkSession
/**
* Gets array of JAR files added to the classpath
* @return Array of JAR file paths
*/
@DeveloperApi
def getAddedJars(): Array[String]Main Processing Method:
/**
* Processes command line arguments and starts the REPL loop
* @param args Command line arguments
* @return Boolean indicating successful completion
*/
def process(args: Array[String]): BooleanUsage Examples:
import org.apache.spark.repl.SparkILoop
import java.io.{BufferedReader, StringReader}
import scala.tools.nsc.interpreter.JPrintWriter
// Create REPL with default settings
val repl = new SparkILoop()
repl.process(Array("-master", "local[*]"))
// Create REPL with custom input/output
val input = new BufferedReader(new StringReader("val x = 1\n:quit\n"))
val output = new JPrintWriter(System.out, true)
val customRepl = new SparkILoop(input, output)
customRepl.process(Array())
// Access Spark integration
val repl2 = new SparkILoop()
repl2.createSparkSession()
val jars = repl2.getAddedJars()
println(s"Added JARs: ${jars.mkString(", ")}")REPL initialization functionality including welcome messages and Spark context setup.
/**
* Trait providing asynchronous initialization for the REPL
*/
trait SparkILoopInit {
/**
* Display the welcome message to users
*/
def printWelcome(): Unit
/**
* Initialize Spark context and session
*/
def initializeSpark(): Unit
}Command line argument parsing with Spark-specific options.
/**
* Command-line parser with Spark-specific options
* @param args List of command line arguments
* @param error Error handling function
*/
@DeveloperApi
class SparkCommandLine(
args: List[String],
error: String => Unit = System.err.println
) {
/**
* Constructor with default error handling
*/
def this(args: List[String]) = this(args, System.err.println)
/**
* Constructor with custom settings
*/
def this(args: List[String], settings: Settings) = this(args, System.err.println)
}Usage Examples:
import org.apache.spark.repl.SparkCommandLine
// Parse command line arguments
val cmdLine = new SparkCommandLine(List("-master", "local[*]", "-i", "init.scala"))
// With custom error handling
val cmdLine2 = new SparkCommandLine(
List("-master", "spark://localhost:7077"),
(error: String) => println(s"Error: $error")
)import org.apache.spark.repl.SparkILoop
import org.apache.spark.SparkConf
// Create REPL with specific Spark configuration
val repl = new SparkILoop()
val success = repl.process(Array(
"-master", "local[4]",
"-conf", "spark.app.name=MyREPL",
"-i", "init.scala"
))
if (success) {
println("REPL started successfully")
}import org.apache.spark.repl.Main
// Start REPL in background
Main.main(Array("-master", "local[*]"))
// Access the running REPL instance
val runningRepl = Main.interp
if (runningRepl != null) {
val context = runningRepl.sparkContext
val jars = runningRepl.getAddedJars()
println(s"Spark Context: ${context.appName}")
println(s"Added JARs: ${jars.length}")
}