我的代码是:
val df = sqlContext.read
.format("com.databricks.spark.xml")
.option("rowTag", header)
.load("/input/du3_init.dat")
val dfCI2 = df.select("CI2")
dfCI2.printSchema()
val path="hdfs://nameservice/user/CI2_Schema"
new PrintWriter(path) { write(dfCI2.schema.treeString);close}
当我在 spark 中执行时,我得到了
Exception in thread "main" java.io.FileNotFoundException: hdfs:/nameservice/user/CI2_Schema (No such file or directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:110)
异常中显示的 hdfs 路径中只有一个斜杠。如何解决这个问题?提前致谢
最佳答案
如果要写入hdfs
,则不能使用PrintWriter
。 PrintWriter 不应该理解诸如带有 hdfs://
或 ftp://
的网络路径。它适用于本地文件系统。
您可以通过从 spark 上下文获取 hdfs
配置来写入 hdfs
。
import org.apache.hadoop.fs.FileSystem
import java.io.BufferedOutputStream
val hdfsConf = sparkContext.hadoopConfiguration
val fileSystem: FileSystem = FileSystem.get(hdfsConf)
val filePath = "hdfs://nameservice1/user/dhdpbankcrtbtch/CIW2_Schema"
val hdfsFileOS: FSDataOutputStream = fileSystem.create(new Path(filePath));
// create a buffered output stream using the FSDataOutputStream
val bos = new BufferedOutputStream(hdfsFileOS)
bos.write(dfCIW2.schema.treeString.toBytes("utf-8"))
bos.close()
关于scala - printwriter 在 hdfs 路径中只考虑一个斜杠而不是双斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38917115/