2013-07-16 20:21:33 -04:00
|
|
|
#
|
|
|
|
# Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
# contributor license agreements. See the NOTICE file distributed with
|
|
|
|
# this work for additional information regarding copyright ownership.
|
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
# (the "License"); you may not use this file except in compliance with
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#
|
|
|
|
|
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()
|