2013-01-21 19:42:24 -05:00
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
class SparkFiles(object):
|
|
|
|
"""
|
|
|
|
Resolves paths to files added through
|
2013-01-23 13:36:18 -05:00
|
|
|
L{SparkContext.addFile()<pyspark.context.SparkContext.addFile>}.
|
2013-01-21 19:42:24 -05:00
|
|
|
|
|
|
|
SparkFiles contains only classmethods; users should not create SparkFiles
|
|
|
|
instances.
|
|
|
|
"""
|
|
|
|
|
|
|
|
_root_directory = None
|
2013-01-23 13:36:18 -05:00
|
|
|
_is_running_on_worker = False
|
|
|
|
_sc = None
|
2013-01-21 19:42:24 -05:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
raise NotImplementedError("Do not construct SparkFiles objects")
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get(cls, filename):
|
|
|
|
"""
|
2013-01-23 13:36:18 -05:00
|
|
|
Get the absolute path of a file added through C{SparkContext.addFile()}.
|
2013-01-21 19:42:24 -05:00
|
|
|
"""
|
2013-01-23 13:36:18 -05:00
|
|
|
path = os.path.join(SparkFiles.getRootDirectory(), filename)
|
2013-01-21 19:42:24 -05:00
|
|
|
return os.path.abspath(path)
|
2013-01-23 13:36:18 -05:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def getRootDirectory(cls):
|
|
|
|
"""
|
|
|
|
Get the root directory that contains files added through
|
|
|
|
C{SparkContext.addFile()}.
|
|
|
|
"""
|
|
|
|
if cls._is_running_on_worker:
|
|
|
|
return cls._root_directory
|
|
|
|
else:
|
|
|
|
# This will have to change if we support multiple SparkContexts:
|
2013-02-01 14:09:56 -05:00
|
|
|
return cls._sc._jvm.spark.SparkFiles.getRootDirectory()
|