提问者:小点点

如何使用sqlContext加载多个拼花文件?


我试图在spark中加载一个拼花文件目录,但似乎无法正常工作。。。这似乎有效:

val df = sqlContext.load("hdfs://nameservice1/data/rtl/events/stream/loaddate=20151102")

但这不起作用:

val df = sqlContext.load("hdfs://nameservice1/data/rtl/events/stream/loaddate=201511*")

它给了我这个错误:

java.io.FileNotFoundException: File does not exist: hdfs://nameservice1/data/rtl/events/stream/loaddate=201511*

如何让它使用通配符?


共2个答案

匿名用户

您可以使用文件系统列表状态读入文件或文件夹列表。然后浏览您要阅读的文件/文件夹。使用reduce with union将所有文件缩减为一个rdd。

获取文件/文件夹:

val fs = FileSystem.get(new Configuration())
val status = fs.listStatus(new Path(YOUR_HDFS_PATH))

读取数据:

val parquetFiles= status .map(folder => {
    sqlContext.read.parquet(folder.getPath.toString)
  })

将数据合并到单个 rdd 中:

val mergedFile= parquetFiles.reduce((x, y) => x.unionAll(y))

你也可以看看我过去关于同一主题的帖子。

目录中的Spark Scala列表文件夹

Spark/Scala flatten和flatMap无法在DataFrame上工作

匿名用户

如果提供的路径是分区目录,请在数据源的选项中设置“BasPath”,指定表的根目录,如果有多个根目录,请分别加载,然后合并。

比如:

basePath="hdfs://nameservice1/data/rtl/events/stream"

sparkSession.read.option("basePath", basePath).parquet(basePath + "loaddate=201511*")