如何在通过--files提供给Spark时重命名文件



在这里和这里引用,我希望我能够通过使用octhorpe来更改Spark中引用文件的名称——也就是说,如果我调用spark-submit --files local-file-name.json#spark-file-name.json,那么我应该能够将该文件引用为spark-file-name.json。然而,情况似乎并非如此:

$ cat ../differentDirectory/local-file-name.json
{
"name": "Adam",
"age": 25
}
$ cat testing1.py
import os
import json
import time
from pyspark import SparkFiles, SparkContext
print(os.getcwd())
print(os.listdir('.'))
sc = SparkContext('local', 'App For Testing --files upload')
print(SparkFiles.getRootDirectory())
print(os.listdir(SparkFiles.getRootDirectory()))
print(json.load(open(SparkFiles.get('local-file-name.json'))))
$ spark-submit --files ../differentDirectory/local-file-name.json testing1.py
20/08/06 17:05:00 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
/private/tmp/sparkSubmitTesting
['testing.py']
...
/private/var/folders/0q/qw3xxl5x2yx1rf1nncl6s4rw2yzhgq/T/spark-2d052f27-59da-463a-9ddf-edd05108c19a/userFiles-5fec4b39-90e3-4402-a644-0c5314c1d0a5
[u'local-file-name.json']
{u'age': 25, u'name': u'Adam'}
...
$ cat testing2.py
import os
import json
import time
from pyspark import SparkFiles, SparkContext
print(os.getcwd())
print(os.listdir('.'))
sc = SparkContext('local', 'App For Testing --files upload')
print(SparkFiles.getRootDirectory())
print(os.listdir(SparkFiles.getRootDirectory()))
print(json.load(open(SparkFiles.get('spark-file-name.json'))))
$ spark-submit --files ../differentDirectory/local-file-name.json#spark-file-name.json testing2.py
20/08/06 17:07:35 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
/private/tmp/sparkSubmitTesting
['testing.py']
...
20/08/06 17:07:38 ERROR SparkContext: Error initializing SparkContext.
java.io.FileNotFoundException: File file:/private/tmp/differentDirectory/local-file-name.json#spark-file-name.json does not exist
at org.apache.hadoop.fs.RawLocalFileSystem.deprecatedGetFileStatus(RawLocalFileSystem.java:611)
at org.apache.hadoop.fs.RawLocalFileSystem.getFileLinkStatusInternal(RawLocalFileSystem.java:824)
at org.apache.hadoop.fs.RawLocalFileSystem.getFileStatus(RawLocalFileSystem.java:601)
at org.apache.hadoop.fs.FilterFileSystem.getFileStatus(FilterFileSystem.java:421)
at org.apache.spark.SparkContext.addFile(SparkContext.scala:1544)
at org.apache.spark.SparkContext.addFile(SparkContext.scala:1508)
at org.apache.spark.SparkContext$$anonfun$13.apply(SparkContext.scala:462)
at org.apache.spark.SparkContext$$anonfun$13.apply(SparkContext.scala:462)
at scala.collection.immutable.List.foreach(List.scala:392)
at org.apache.spark.SparkContext.<init>(SparkContext.scala:462)
at org.apache.spark.api.java.JavaSparkContext.<init>(JavaSparkContext.scala:58)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:247)
at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
at py4j.Gateway.invoke(Gateway.java:238)
at py4j.commands.ConstructorCommand.invokeConstructor(ConstructorCommand.java:80)
at py4j.commands.ConstructorCommand.execute(ConstructorCommand.java:69)
at py4j.GatewayConnection.run(GatewayConnection.java:238)
at java.lang.Thread.run(Thread.java:748)

我尝试过用反斜杠转义包装文件路径的#(即---files ../differentDirectory/local-file-name.json#spark-file-name.json(引号,并显式地在file://前面加上反斜杠,但在所有情况下,我都会得到相同的错误(File <path, including fragment> does not exist(或Expected scheme-specific part at index 5

MacOS,Spark v2.4.5

一位同事指出,这种重命名行为依赖于YARN,而YARN在本地运行时是不存在的,因此该功能在我的设置中不起作用。

最新更新