printwriter只考虑hdfs路径中的一个斜杠而不是双斜杠

7gyucuyw  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(433)

我的代码是:

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}

当我在星火中执行时,我得到了

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路径中只存在一个斜杠。如何解决这个问题?。提前谢谢

klr1opcd

klr1opcd1#

如果你想写信给 hdfs ,不能使用 PrintWriter . printwriter不应该理解网络路径,例如 hdfs:// 或者 ftp:// . 它与本地文件系统一起工作。
你可以写信给 hdfs 通过得到 hdfs 配置窗体spark上下文。

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()

相关问题