从多个文件夹中读取 Delta 表

时间:2021-01-13 23:30:27

标签: pyspark databricks azure-data-lake azure-databricks pyspark-dataframes

我正在研究 Databricks。我正在像这样阅读我的增量表:

path = "/root/data/foo/year=2021/"
df = spark.read.format("delta").load(path)

但是在 year=2021 文件夹中,每天都有子文件夹 day=01day=02day=03 等...

例如,如何读取第 4、5、6 天的文件夹?

编辑#1

我正在阅读不同问题的答案,似乎实现这一目标的正确方法是使用应用了分区列的过滤器

edit2

尝试下面的一些答案是我遇到的消息错误(一开始我没有看到):

Databricks Delta does not support multiple input paths in the load() API.
paths: [example_path/year=2021/month=01/day=04/,...]. To build a single DataFrame by loading
multiple paths from the same Delta table, please load the root path of
the Delta table with the corresponding partition filters. If the multiple paths
are from different Delta tables, please use Dataset's union()/unionByName() APIs
to combine the DataFrames generated by separate load() API calls.;

3 个答案:

答案 0 :(得分:1)

用大括号括起来的逗号分隔值列出它们

path = "/root/data/foo/year=2021/{04,05,06}/"

path = "/root/data/foo/year=2021/[04,05,06]/"
path = "/root/data/foo/year=2021/0[4|5|6]/"

答案 1 :(得分:1)

似乎读取分区增量表的更好方法是对分区应用过滤器:

df = spark.read.format("delta").load('/whatever/path')
df2 = df.filter("year = '2021' and month = '01' and day in ('04','05','06')")

答案 2 :(得分:0)

  1. 删除 .format("delta")。来自您的源代码路径

  2. 在 UDF 下面使用

    def fileexists(filepath, FromDay, ToDay): mylist = dbutils.fs.ls(文件路径) maxcount = len(mylist) - 今天 + 1 maxcount1 = maxcount - FromDay 返回 [item[0] for item in mylist][maxcount1:maxcount]

    filepath = 父文件路径 = "/root/data/foo/year=2021/" FromDay = 开始日期文件夹 ToDay = 结束日文件夹

注意:根据您的要求更改功能。

相关问题