PySpark - SparkFiles


In Apache Spark, you can upload your files using sc.addFile (sc is your default SparkContext) and get the path on a worker using SparkFiles.get. Thus, SparkFiles resolve the paths to files added through SparkContext.addFile().

SparkFiles contain the following classmethods −

  • get(filename)
  • getrootdirectory()

Let us understand them in detail.


It specifies the path of the file that is added through SparkContext.addFile().


It specifies the path to the root directory, which contains the file that is added through the SparkContext.addFile().
from pyspark import SparkContext
from pyspark import SparkFiles
finddistance = "/home/hadoop/examples_pyspark/finddistance.R"
finddistancename = "finddistance.R"
sc = SparkContext("local", "SparkFile App")
print "Absolute Path -> %s" % SparkFiles.get(finddistancename)

Command − The command is as follows −


Output − The output for the above command is −

Absolute Path ->