我正在尝试使用fromFile,但是我会出错。该文件存在并且可以用sc.textFile打开,但是如果我用相同的路径打开同一个文件,formFile会给出一个错误。这是我的代码:
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
import java.io.File
import scala.io.Source
val filename: String = "file:///data/text.txt"
// this works, no error and the file is opened
val msgRDD = sc.textFile(filename);
// errors with this line, see below. The same file and same path
val lines = Source.fromFile(filename).getLines.toArray下面是错误
名称: java.io.FileInputStream.open(FileInputStream.java:195)消息: java.io.FileNotFoundException :/data/maildir/allen/inbox/1。(没有这样的文件或目录) StackTrace: at java.io.FileInputStream.open0(原生方法) at java.io.FileInputStream.open0 at java.io.FileInputStream.(FileInputStream.java:138) at scala.io.Source$.fromFile(Source.scala:91) at scala.io.Source$。$$$82b5b23cea489b2712a1db46c77e458$$$$w$processEmail(:193) fromFile(Source.scala:76) at scala.io.Source$.fromFile(Source.scala:54)
如何使用formFile解决此错误?
谢谢
发布于 2018-01-25 06:01:28
问题在于文件名格式。在使用file://时,不需要对文件路径使用Source.fromFile。尝试使用以下代码读取
val filename: String = "/data/text.txt"
val lines = Source.fromFile(filename).getLines.toArrayhttps://stackoverflow.com/questions/48436357
复制相似问题