2016-08-12 110 views
0

我的代码是:PrintWriter的是只考虑一个斜杠,而不是在HDFS路径双斜杠

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路径。如何解决这个问题?在此先感谢

+0

'PrintWriter'不应该理解像'hdfs://'或'ftp://'那样的网络路径。它的工作与本地文件系统。而在本地文件系统路径中格式为“a/b/c”。 –

回答

1

如果要写入hdfs,则不能使用PrintWriter。 PrintWriter不应该理解网络路径,例如hdfs://ftp://。它适用于本地文件系统。

您可以通过获取hdfs配置形式的spark上下文来致信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() 
+0

非常感谢Sarvesh。 – Sibikrish

相关问题