在Amazon Athena中按顺序显示分区

jac*_*ack 6 amazon-athena

我有这个查询:

SHOW PARTITIONS tablename;
Run Code Online (Sandbox Code Playgroud)

结果是:

dt=2018-01-12
dt=2018-01-20
dt=2018-05-21
dt=2018-04-07
dt=2018-01-03
Run Code Online (Sandbox Code Playgroud)

这给出了每个表的分区列表。该表的分区字段dt是日期列。我想查看排序的分区。

该文档没有说明如何执行此操作:https : //docs.aws.amazon.com/athena/latest/ug/show-partitions.html

我尝试通过以下方式添加订单:

SHOW PARTITIONS tablename order by dt;
Run Code Online (Sandbox Code Playgroud)

但是它给出:

亚马逊雅典娜; 状态码:400;错误代码:InvalidRequestException;

Mik*_*eGM 11

AWS 目前(截至2020 年 11 月)支持两个版本的 Athena 引擎。如何选择和排序分区取决于所使用的版本。

版本1:

使用information_schema桌子。假设您有year,month作为分区(使用一个分区键,这当然更简单):

WITH 
 a as (
SELECT partition_number as pn, partition_key as key, partition_value as val
FROM   information_schema.__internal_partitions__
WHERE  table_schema = 'my_database'
       AND table_name = 'my_table'
 )
SELECT 
  year, month
FROM (
    SELECT val as year, pn FROM a WHERE key = 'year'
) y
JOIN (
    SELECT val as month, pn FROM a WHERE key = 'month'
) m ON m.pn = y.pn
ORDER BY year, month
Run Code Online (Sandbox Code Playgroud)

其输出:

  year month
0 2018    10
0 2018    11
0 2018    12
0 2019    01
...
Run Code Online (Sandbox Code Playgroud)

版本2:

使用内置$partitions功能,其中分区显式用作列,并且语法更简单:

  year month
0 2018    10
0 2018    11
0 2018    12
0 2019    01
...
Run Code Online (Sandbox Code Playgroud)
  year month
0 2018    10
0 2018    11
0 2018    12
0 2019    01
...
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅:

https://docs.aws.amazon.com/athena/latest/ug/querying-glue-catalog.html#querying-glue-catalog-listing-partitions


Mar*_*usz 5

我只是遇到了同样的问题,并在information_schema数据库中找到了解决方案。如果您的表仅包含一个分区列,请使用以下查询获取有序列表:

SELECT partition_value
FROM information_schema.__internal_partitions__
WHERE table_schema = '<DB_NAME>'
        AND table_name = '<TABLE_NAME>'
ORDER BY partition_value
Run Code Online (Sandbox Code Playgroud)

  • 另请注意,此 __internal_partitions__ 表已在 [presto 1.9.3](https://github.com/prestodb/presto/issues/9802) 中删除,因此不应在生产中使用。 (2认同)

jen*_*ter 1

SHOW PARTITIONS命令不允许您对结果进行排序,因为该命令不会生成要排序的结果集。该命令仅产生字符串输出。

另一方面,您可以查询分区列,然后按值对结果进行排序。

select distinct dt from tablename order by dt asc;
Run Code Online (Sandbox Code Playgroud)

  • 该解决方案将扫描表中的所有数据,这可能很慢并且非常昂贵。 (3认同)