查询 Amazon Glue Data Catalog
由于许多 Amazon Web Services 都将 Amazon Glue Data Catalog 用作其中央元数据存储库,因此您可能需要查询 Data Catalog 元数据。为此,您可以在 Athena 中使用 SQL 查询。您可以使用 Athena 查询 Amazon Glue 目录元数据,如数据库、表、分区和列等。
要获取 Amazon Glue Catalog 元数据,请查询 Athena 后端上的 information_schema 数据库。本主题中的示例查询显示如何使用 Athena 查询 Amazon Glue Catalog 元数据以搜索常用案例。
注意事项和限制
-
您可以不查询
information_schema数据库,而是使用单独的 Apache Hive DDL 命令从 Athena 中提取特定数据库、表、视图、分区和列的元数据信息。但输出将为非表格格式。 -
如果您的 Amazon Glue 元数据较少或中等,则查询
information_schema的性能会最佳。如果您有大量的元数据,则可能会出现错误。 -
不能使用
CREATE VIEW在information_schema数据库上创建视图。
列出数据库并搜索指定的数据库
本节中的示例演示如何按模式名称列出元数据中的数据库。
例 – 列出数据库
以下示例查询列出 information_schema.schemata 表中的数据库。
SELECT schema_name FROM information_schema.schemata LIMIT 10;
下表显示了示例结果。
| 6 | alb-databas1 |
| 7 | alb_original_cust |
| 8 | alblogsdatabase |
| 9 | athena_db_test |
| 10 | athena_ddl_db |
例 – 搜索指定的数据库
在以下示例查询中,rdspostgresql 是示例数据库。
SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'rdspostgresql'
下表显示了示例结果。
| schema_name | |
|---|---|
| 1 | rdspostgresql |
列出指定数据库中的表并按名称搜索表
要列出表的元数据,您可以按表架构或表名进行查询。
例 – 按架构列出表
以下查询列出了使用 rdspostgresql 表架构的表。
SELECT table_schema, table_name, table_type FROM information_schema.tables WHERE table_schema = 'rdspostgresql'
下图显示了一个示例结果。
| table_schema | table_name | table_type | |
|---|---|---|---|
| 1 | rdspostgresql | rdspostgresqldb1_public_account | BASE TABLE |
例 – 按名称搜索表
以下查询获取表 athena1 的元数据信息。
SELECT table_schema, table_name, table_type FROM information_schema.tables WHERE table_name = 'athena1'
下图显示了一个示例结果。
| table_schema | table_name | table_type | |
|---|---|---|---|
| 1 | 默认值 | athena1 | BASE TABLE |
列出特定表的分区
您可以使用 SHOW PARTITIONS 以列出指定表的分区,如以下示例所示。table_name
SHOW PARTITIONS cloudtrail_logs_test2
您还可以使用 $partitions 元数据查询列出特定表的分区号和分区值。
例 – 使用 $partitions 语法查询表的分区
以下示例查询使用 $partitions 语法列出了表 cloudtrail_logs_test2 的分区。
SELECT * FROM default."cloudtrail_logs_test2$partitions" ORDER BY partition_number
下表显示了示例结果。
| table_catalog | table_schema | table_name | 年份 | 月份 | 天 | |
|---|---|---|---|---|---|---|
| 1 | awsdatacatalog | default | cloudtrail_logs_test2 | 2020 | 08 | 10 |
| 2 | awsdatacatalog | default | cloudtrail_logs_test2 | 2020 | 08 | 11 |
| 3 | awsdatacatalog | default | cloudtrail_logs_test2 | 2020 | 08 | 12 |
列出所有表的所有列
您可以列出 AwsDataCatalog 中所有表的所有列,或者 AwsDataCatalog 中特定数据库中所有表的所有列。
-
要列出
AwsDataCatalog中所有数据库的所有列,请使用查询SELECT * FROM information_schema.columns。 -
要将结果限制在特定数据库中,请使用
WHERE子句中的table_schema='。database_name'
例 — 列出特定数据库中所有表的所有列
以下示例查询列出了数据库 webdata 中所有表的所有列。
SELECT * FROM information_schema.columns WHERE table_schema = 'webdata'
列出或搜索指定表或视图的列
您可以列出表的所有列、视图的所有列,或者在指定的数据库和表中按名称搜索列。
要列出列,请使用 SELECT * 查询。在 FROM 子句中,指定 information_schema.columns。在 WHERE 子句中,使用 table_schema=' 以指定数据库,使用 database_name'table_name =
' 指定包含要列出的列的表或视图。table_name'
例 – 列出指定表的所有列
以下示例查询列出了 rdspostgresqldb1_public_account 表的所有列。
SELECT * FROM information_schema.columns WHERE table_schema = 'rdspostgresql' AND table_name = 'rdspostgresqldb1_public_account'
下表显示了示例结果。
| table_catalog | table_schema | table_name | column_name | ordinal_position | column_default | is_nullable | data_type | Comment | extra_info | |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | awsdatacatalog | rdspostgresql | rdspostgresqldb1_public_account | password | 1 | 是 | varchar | |||
| 2 | awsdatacatalog | rdspostgresql | rdspostgresqldb1_public_account | user_id | 2 | 是 | integer | |||
| 3 | awsdatacatalog | rdspostgresql | rdspostgresqldb1_public_account | created_on | 3 | 是 | timestamp | |||
| 4 | awsdatacatalog | rdspostgresql | rdspostgresqldb1_public_account | last_login | 4 | 是 | timestamp | |||
| 5 | awsdatacatalog | rdspostgresql | rdspostgresqldb1_public_account | 5 | 是 | varchar | ||||
| 6 | awsdatacatalog | rdspostgresql | rdspostgresqldb1_public_account | username | 6 | 是 | varchar |
例 – 列出指定视图的列
以下示例查询列出了 default 数据库中用于 arrayview 视图的所有列。
SELECT * FROM information_schema.columns WHERE table_schema = 'default' AND table_name = 'arrayview'
下表显示了示例结果。
| table_catalog | table_schema | table_name | column_name | ordinal_position | column_default | is_nullable | data_type | Comment | extra_info | |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | awsdatacatalog | default | arrayview | searchdate | 1 | 是 | varchar | |||
| 2 | awsdatacatalog | default | arrayview | sid | 2 | 是 | varchar | |||
| 3 | awsdatacatalog | default | arrayview | btid | 3 | 是 | varchar | |||
| 4 | awsdatacatalog | default | arrayview | p | 4 | 是 | varchar | |||
| 5 | awsdatacatalog | default | arrayview | infantprice | 5 | 是 | varchar | |||
| 6 | awsdatacatalog | default | arrayview | sump | 6 | 是 | varchar | |||
| 7 | awsdatacatalog | default | arrayview | journeymaparray | 7 | 是 | array(varchar) |
例 – 在指定数据库和表中按名称搜索列
以下示例查询在 default 数据库的 arrayview 视图中搜索 sid 列的元数据。
SELECT * FROM information_schema.columns WHERE table_schema = 'default' AND table_name = 'arrayview' AND column_name='sid'
下图显示了一个示例结果。
| table_catalog | table_schema | table_name | column_name | ordinal_position | column_default | is_nullable | data_type | Comment | extra_info | |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | awsdatacatalog | default | arrayview | sid | 2 | 是 | varchar |