简体   繁体   English

如何获取输入文件名作为 AWS Athena 外部表中的列

[英]How to get input file name as column in AWS Athena external tables

I have external tables created in AWS Athena to query S3 data, however, the location path has 1000+ files.我在 AWS Athena 中创建了外部表来查询 S3 数据,但是,位置路径有 1000 多个文件。 So I need the corresponding filename of the record to be displayed as a column in the table.所以我需要将记录的相应文件名显示为表中的一列。

select file_name , col1 from table where file_name = "test20170516"

In short, I need to know INPUT__FILE__NAME(hive) equivalent in AWS Athena Presto or any other ways to achieve the same.简而言之,我需要知道 AWS Athena Presto 中的 INPUT__FILE__NAME(hive) 等效项或任何其他实现相同目标的方法。

您可以使用 $path 伪列执行此操作。

select "$path" from table

If you need just the filename, you can extract it with regeexp_extract() .如果您只需要文件名,可以使用regeexp_extract()提取它。

To use it in Athena on the "$path" you can do something like this:要在"$path" Athena 中使用它,您可以执行以下操作:

SELECT regexp_extract("$path", '[^/]+$') AS filename from table;

If you need the filename without the extension, you can do:如果您需要没有扩展名的文件名,您可以执行以下操作:

SELECT regexp_extract("$path", '[ \w-]+?(?=\.)') AS filename_without_extension from table;

Here is the documentation on Presto Regular Expression Functions这是关于Presto 正则表达式函数的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM