【问题标题】:How to get the date from the latest file in the S3 bucket using pyspark如何使用 pyspark 从 S3 存储桶中的最新文件中获取日期
【发布时间】:2021-12-26 19:34:21
【问题描述】:

我得到了在 PySpark 中的要求,我需要从 s3 中的文件中获取最新日期。

如何使用 Pyspark 代码获取日期?

s3文件路径,示例:

"s3://bucketname/folderpath/2021/10/10/file.parquet, s3://bucketname/folderpath/2021/10/08/file.parquet, s3://bucketname/folderpath/2021/10/05/file.parquet, s3://bucketname/folderpath/2021/10/02/file.parquet"

输出:从 S3 获取最新的文件日期路径,并从路径中选择日期并分配给变量。

示例

  • 最新路径:s3://bucketname/folderpath/2021/10/10/file.parquet
  • 日期变量:date = 20211010

【问题讨论】:

  • 你在哪里运行 Spark?数据块?

标签: python amazon-s3 pyspark


【解决方案1】:
file_paths = sc\
               .wholeTextFiles("s3://bucketname/folderpath/*/*/*/*.parquet")\
               .map(lambda x: x[0]).collect()

most_recent_file = max(file_paths)

Date_variable = most_recent_file.split("/")[4] + \
                most_recent_file.split("/")[5] + \
                most_recent_file.split("/")[6]

print(Date_variable)

Out: '20211010'

【讨论】:

    猜你喜欢
    • 2021-10-25
    • 2021-12-23
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多