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

标签 scala hadoop printwriter

我的代码是:

 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/

相关文章:

hadoop - 如何从 .t​​sv 文件加载 Pig 中的数据?

java - PrintWriter 创建空文件

Java 自己制作 System.out

java - 如何将斯坦福NLPCore 的带注释的输出保存到字符串中?

scala - 如何使用 SBT 使用 build.scala 中的 -D 变量?

类似 scala 比较器的序列函数

scala - 如何在Apache Spark作业中执行阻止IO?

scala - hadoop和spark之间的配置问题

hadoop - 格式化名称节点,节点丢失

hadoop - apache nifi - 如何一个接一个地执行进程组