

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 查詢 AWS Glue Data Catalog
<a name="querying-glue-catalog"></a>

由於許多 使用 AWS Glue Data Catalog AWS 服務 做為其中央中繼資料儲存庫，因此您可能想要查詢 Data Catalog 中繼資料。若要執行此操作，您可以在 Athena 中使用 SQL 查詢。您可以使用 Athena 查詢 AWS Glue 目錄中繼資料，例如資料庫、資料表、分割區和資料欄。

若要取得 AWS Glue 目錄中繼資料，您可以在 Athena 後端查詢`information_schema`資料庫。本主題中的查詢範例會顯示如何使用 Athena 來查詢常見使用案例的 AWS Glue 目錄中繼資料。

## 考量和限制
<a name="querying-glue-catalog-considerations-limitations"></a>
+ 不是查詢 `information_schema` 資料庫，可使用個別 Apache Hive [DDL 命令](ddl-reference.md)，擷取特定資料庫、資料表、檢視、分割區和資料欄的中繼資料資訊。然而，輸出採用非表格格式。
+ 如果您有少量到中等數量的 AWS Glue 中繼資料，查詢`information_schema`效能最佳。如果您有大量中繼資料，則可能會出現錯誤。
+ 您無法使用 `CREATE VIEW` 在 `information_schema` 資料庫上建立檢視。

**Topics**
+ [考量和限制](#querying-glue-catalog-considerations-limitations)
+ [列出資料庫並搜尋指定的資料庫](querying-glue-catalog-querying-available-databases-including-rdbms.md)
+ [列出指定資料庫中的資料表，並依名稱搜尋資料表](querying-glue-catalog-listing-tables.md)
+ [列出特定資料表的分割區](querying-glue-catalog-listing-partitions.md)
+ [列示或搜尋指定的資料表或檢視的資料欄](querying-glue-catalog-listing-columns.md)
+ [列出特定資料表共有的資料欄](querying-glue-catalog-listing-columns-in-common.md)
+ [列出所有資料表的所有資料欄](querying-glue-catalog-listing-all-columns-for-all-tables.md)

# 列出資料庫並搜尋指定的資料庫
<a name="querying-glue-catalog-querying-available-databases-including-rdbms"></a>

本節中的範例顯示如何依結構描述名稱列出中繼資料中的資料庫。

**Example – 列出資料庫**  
以下範例查詢會列出 `information_schema.schemata` 資料表中的數據庫。  

```
SELECT schema_name
FROM   information_schema.schemata
LIMIT  10;
```
下列資料表會顯示範例結果。  


****  

|  |  | 
| --- |--- |
| 6 | alb-databas1 | 
| 7 | alb\$1original\$1cust | 
| 8 | alblogsdatabase | 
| 9 | athena\$1db\$1test | 
| 10 | athena\$1ddl\$1db | 

**Example – 搜尋指定資料庫**  
在下列範例查詢中，`rdspostgresql` 是範例資料庫。  

```
SELECT schema_name
FROM   information_schema.schemata
WHERE  schema_name = 'rdspostgresql'
```
下列資料表會顯示範例結果。  


****  

|  | schema\$1name | 
| --- | --- | 
| 1 | rdspostgresql | 

# 列出指定資料庫中的資料表，並依名稱搜尋資料表
<a name="querying-glue-catalog-listing-tables"></a>

若要列出資料表的中繼資料，您可以透過資料表模式或資料表名稱查詢。

**Example – 依結構描述列出資料表**  
下列查詢會列出使用 `rdspostgresql` 資料表模式的資料表。  

```
SELECT table_schema,
       table_name,
       table_type
FROM   information_schema.tables
WHERE  table_schema = 'rdspostgresql'
```
下列資料表會顯示範例結果。  


****  

|  | table\$1schema | table\$1name | table\$1type | 
| --- | --- | --- | --- | 
| 1 | rdspostgresql | rdspostgresqldb1\$1public\$1account | BASE TABLE | 

**Example – 依名稱搜尋資料表**  
下列查詢包含 `athena1` 資料表的中繼資料資訊。  

```
SELECT table_schema,
       table_name,
       table_type
FROM   information_schema.tables
WHERE  table_name = 'athena1'
```
下列資料表會顯示範例結果。  


****  

|  | table\$1schema | table\$1name | table\$1type | 
| --- | --- | --- | --- | 
| 1 | 預設 | athena1 | BASE TABLE | 

# 列出特定資料表的分割區
<a name="querying-glue-catalog-listing-partitions"></a>

您可以如下列範例所示，使用 `SHOW PARTITIONS table_name` 列出指定資料表的分割區。

```
SHOW PARTITIONS cloudtrail_logs_test2
```

您可以使用 `$partitions` 中繼資料查詢，列出特定資料表的分割區編號和分割區值。

**Example – 使用 \$1partitions 語法查詢資料表的分割區**  
下列範例查詢會使用 `$partitions` 語法，列出資料表 `cloudtrail_logs_test2` 的分割區。  

```
SELECT * FROM default."cloudtrail_logs_test2$partitions" ORDER BY partition_number
```
下列資料表會顯示範例結果。  


****  

|  | table\$1catalog | table\$1schema | table\$1name | 年 | 月 | 天 | 
| --- | --- | --- | --- | --- | --- | --- | 
| 1 | awsdatacatalog | 預設 | cloudtrail\$1logs\$1test2 | 2020 | 08 | 10 | 
| 2 | awsdatacatalog | 預設 | cloudtrail\$1logs\$1test2 | 2020 | 08 | 11 | 
| 3 | awsdatacatalog | 預設 | cloudtrail\$1logs\$1test2 | 2020 | 08 | 12 | 

# 列示或搜尋指定的資料表或檢視的資料欄
<a name="querying-glue-catalog-listing-columns"></a>

您可以列出資料表的所有資料欄、檢視的所有資料欄，或在指定的資料庫和資料表中依名稱搜尋欄。

若要列出資料欄，請使用 `SELECT *` 查詢。在 `FROM` 子句中，指定 `information_schema.columns`。在 `WHERE` 子句中，使用 `table_schema='database_name'` 來指定資料庫，使用 `table_name = 'table_name'` 以指定具有您想要列出之資料欄的資料表或檢視。

**Example – 列出指定資料表的所有資料欄**  
下面的範例查詢會列出資料表 `rdspostgresqldb1_public_account` 的所有資料欄。  

```
SELECT *
FROM   information_schema.columns
WHERE  table_schema = 'rdspostgresql'
       AND table_name = 'rdspostgresqldb1_public_account'
```
下列資料表會顯示範例結果。  


****  

|  | table\$1catalog | table\$1schema | table\$1name | column\$1name | ordinal\$1position | column\$1default | is\$1nullable | data\$1type | comment | extra\$1info | 
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | 
| 1 | awsdatacatalog | rdspostgresql | rdspostgresqldb1\$1public\$1account | password | 1 |  | 是 | varchar |  |  | 
| 2 | awsdatacatalog | rdspostgresql | rdspostgresqldb1\$1public\$1account | user\$1id | 2 |  | 是 | integer |  |  | 
| 3 | awsdatacatalog | rdspostgresql | rdspostgresqldb1\$1public\$1account | created\$1on | 3 |  | 是 | timestamp |  |  | 
| 4 | awsdatacatalog | rdspostgresql | rdspostgresqldb1\$1public\$1account | last\$1login | 4 |  | 是 | timestamp |  |  | 
| 5 | awsdatacatalog | rdspostgresql | rdspostgresqldb1\$1public\$1account | email | 5 |  | 是 | varchar |  |  | 
| 6 | awsdatacatalog | rdspostgresql | rdspostgresqldb1\$1public\$1account | username | 6 |  | 是 | varchar |  |  | 

**Example – 列出指定檢視的資料欄**  
下列範例查詢會針對檢視 `default` 列出 `arrayview` 資料庫中的所有資料欄。  

```
SELECT *
FROM   information_schema.columns
WHERE  table_schema = 'default'
       AND table_name = 'arrayview'
```
下列資料表會顯示範例結果。  


****  

|  | table\$1catalog | table\$1schema | table\$1name | column\$1name | ordinal\$1position | column\$1default | is\$1nullable | data\$1type | comment | extra\$1info | 
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | 
| 1 | awsdatacatalog | 預設 | arrayview | searchdate | 1 |  | 是 | varchar |  |  | 
| 2 | awsdatacatalog | 預設 | arrayview | sid | 2 |  | 是 | varchar |  |  | 
| 3 | awsdatacatalog | 預設 | arrayview | btid | 3 |  | 是 | varchar |  |  | 
| 4 | awsdatacatalog | 預設 | arrayview | p | 4 |  | 是 | varchar |  |  | 
| 5 | awsdatacatalog | 預設 | arrayview | infantprice | 5 |  | 是 | varchar |  |  | 
| 6 | awsdatacatalog | 預設 | arrayview | sump | 6 |  | 是 | varchar |  |  | 
| 7 | awsdatacatalog | 預設 | arrayview | journeymaparray | 7 |  | 是 | array(varchar) |  |  | 

**Example – 在指定的資料庫與資料表中依名稱搜尋資料欄**  
下列範例查詢會在 `sid` 資料庫的 `arrayview` 檢視中搜尋 `default` 資料欄的中繼資料。  

```
SELECT *
FROM   information_schema.columns
WHERE  table_schema = 'default'
       AND table_name = 'arrayview' 
       AND column_name='sid'
```
下列資料表會顯示範例結果。  


****  

|  | table\$1catalog | table\$1schema | table\$1name | column\$1name | ordinal\$1position | column\$1default | is\$1nullable | data\$1type | comment | extra\$1info | 
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | 
| 1 | awsdatacatalog | 預設 | arrayview | sid | 2 |  | 是 | varchar |  |  | 

# 列出特定資料表共有的資料欄
<a name="querying-glue-catalog-listing-columns-in-common"></a>

可列出資料庫中的特定資料表的共同資料欄。
+ 使用語法 `SELECT column_name FROM information_schema.columns`。
+ 對於 `WHERE` 子句，請使用語法 `WHERE table_name IN ('table1', 'table2')`。

**Example – 列出同一資料庫中兩個表的共同資料欄**  
下列範例查詢會列出資料表 `table1` 和 `table2` 的共同資料欄。  

```
SELECT column_name
FROM information_schema.columns
WHERE table_name IN ('table1', 'table2')
GROUP BY column_name
HAVING COUNT(*) > 1;
```

# 列出所有資料表的所有資料欄
<a name="querying-glue-catalog-listing-all-columns-for-all-tables"></a>

您可以列出 `AwsDataCatalog` 中所有資料表的所有資料欄，或者 `AwsDataCatalog` 中特定資料庫的所有資料表。
+ 若要列出 `AwsDataCatalog` 中所有資料庫的所有資料欄，請使用查詢 `SELECT * FROM information_schema.columns`。
+ 若要將結果限制在特定資料庫，請在 `WHERE` 子句中使用 `table_schema='database_name'`。

**Example - 列出特定資料庫中所有資料表的所有資料欄**  
下面的範例查詢會列出資料庫 `webdata` 中所有資料表的所有資料欄。  

```
SELECT * FROM information_schema.columns WHERE table_schema = 'webdata'            
```