如何在spark Scala中读取s3中的多个目录?

2024-05-02

我在 s3 中有以下格式的目录,

 <base-directory>/users/users=20180303/hour=0/<parquet files>
 <base-directory>/users/users=20180303/hour=1/<parquet files>
 ....
 <base-directory>/users/users=20180302/hour=<0 to 23>/<parquet files>
 <base-directory>/users/users=20180301/hour=<0 to 23>/<parquet files>
 ....
 <base-directory>/users/users=20180228/hour=<0 to 23>/<parquet files>

基本上我在每日目录中有每小时的子目录。

现在我想处理过去 30 天的镶木地板文件。

我尝试过以下,

 val df = sqlContext.read.option("header", "true")
    .parquet(<base-directory> + File.separator + "users" + File.separator)
    .where(col("users").between(startDate, endDate))

其中 endDate 和 startDate 间隔 30 天,格式为 yyyymmdd。

上述解决方案没有给出正确的目录子集。我究竟做错了什么 ?


where函数用于过滤行 in dataframe。你用它来阅读parquet文件来自s3. 所以整个概念都是错误的.

相反,你可以创建 startDate 和 endDate 之间的路径数组并将其传递给sqlContext 读取 api.

从程序上来说,你可以执行如下操作(它们只是伪代码)

val listBuffer = new ListBuffer[String]
for(date <- startDate to endDate)
  listBuffer.append(<base-directory> + File.separator + "users" + File.separator+"users="+date)

val df = sqlContext.read.option("header", "true").parquet(listBuffer: _*)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在spark Scala中读取s3中的多个目录? 的相关文章

随机推荐