[英]How to Read Filename from S3 using AWS Glue ETL Tools
I have a some files in S3 that look like this (all in the same path):我在 S3 中有一些看起来像这样的文件(都在同一路径中):
group1_20210415.csv
group2_20210415.csv
group1_20210416.csv
group2_20210416.csv
The schema for each file is rather simple:每个文件的架构相当简单:
group1_name, group1_id
group2_name, group2_id
I want to be able to query these names and ids from S3 with Athena, and use AWS Glue to crawl that S3 location when new files are present.我希望能够使用 Athena 从 S3 查询这些名称和 ID,并在存在新文件时使用 AWS Glue 抓取该 S3 位置。
Specifically, I want to have a table in Athena with schema:具体来说,我想在 Athena 中有一个带有架构的表:
group1_name, group1_id, group2_name, group2_id, hit_date
My intuition says to use AWS Glue PySpark to combine the data in the S3 files into a single DataFrame, which is simple enough.我的直觉是使用 AWS Glue PySpark 将 S3 文件中的数据组合成一个 DataFrame,这很简单。 However, the date for each file exists in the file name itself not in the data.
但是,每个文件的日期存在于文件名本身而不是数据中。
Is there a way to extract the 'date' part of the filename and use that as a column in the AWS Glue PySpark DataFrame?有没有办法提取文件名的“日期”部分并将其用作 AWS Glue PySpark DataFrame 中的列? If not, does anyone's intuition suggest an alternative method?
如果没有,是否有人的直觉提出了另一种方法?
You could try this: data_frame.withColumn("input_file", input_file_name())
You can then transform this column to extract the date.你可以试试这个:
data_frame.withColumn("input_file", input_file_name())
然后你可以转换这个列来提取日期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.