

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

# AWS Glue Data Catalog入門
<a name="start-data-catalog"></a>

 AWS Glue Data Catalog 是您的持久性技術中繼資料存放區。這是一項受管服務，可用來在 AWS 雲端中存放、標註和共用中繼資料。如需詳細資訊，請參閱[AWS Glue Data Catalog](https://docs.aws.amazon.com/glue/latest/dg/components-overview.html#data-catalog-intro)。


|  | 
| --- |
| AWS Glue 主控台和一些使用者介面最近有所更新。 | 

## 概觀
<a name="start-data-catalog-overview"></a>

 您可以使用此教學來建立您的第一個 AWS Glue Data Catalog，而此目錄使用 Amazon S3 儲存貯體作為資料來源。

 在本教學課程中，您將使用 AWS Glue 主控台執行下列操作：

1.  建立 資料庫 

1.  建立資料表 

1.  使用 Amazon S3 儲存貯體作為資料來源 

 在完成這些步驟之後，您將成功地使用 Amazon S3 儲存貯體作為資料來源來填入 AWS Glue Data Catalog。

## 步驟 1：建立資料庫
<a name="start-data-catalog-database"></a>

 若要開始使用，請登入 AWS 管理主控台 並開啟 [AWS Glue主控台](https://console.aws.amazon.com/glue)。

 **使用 AWS Glue 主控台建立資料庫：**

1.  在 AWS Glue 主控台中，從左側選單中，選擇 **Data catalog** (資料目錄) 下的 **Databases** (資料庫)。

1.  選擇**新增資料庫**。

1.  在「建立資料庫」頁面中輸入資料庫的名稱。在**位置 - *選用***區段中，設定 URI 位置以供資料目錄的用戶端使用。如果您不知道此資訊，則可以繼續建立資料庫。

1.  (選用)。輸入資料庫的說明。

1.  選擇**建立資料庫**。

 恭喜您，您成功地使用 AWS Glue 主控台設定了第一個資料庫。您的新資料庫將會顯示在可用資料庫清單中。您可以透過從 **Databases** (資料庫) 儀表板中選擇資料庫的名稱，來編輯資料庫。

 **後續步驟** 

 **建立資料庫的其他方法：**

 您剛剛使用 AWS Glue 主控台建立了資料庫，不過還有其他方法可以建立資料庫：
+ 您可以使用爬蟲程式來自動為您建立資料庫和資料表。要使用爬蟲程式設定資料庫，請參閱[在 AWS Glue 主控台上使用爬蟲程式](https://docs.aws.amazon.com/glue/latest/dg/console-crawlers.html)。
+  您可以使用 CloudFormation 範本。請參閱[使用 AWS Glue Data Catalog 範本建立AWS Glue資源](https://docs.aws.amazon.com/glue/latest/dg/populate-with-cloudformation-templates.html)。
+  您也可以使用 AWS Glue 資料庫 API 操作建立資料庫。

   若要使用 `create` 操作建立資料庫，可透過包括 `DatabaseInput` (必要) 參數來建構請求。

   例如：  
****  
 以下範例說明了如何使用 CLI、Boto3 或 DDL 根據您在教學中使用的 S3 儲存貯體中的相同 flights\$1data.csv 檔案定義資料表。  

  ```
  aws glue create-database --database-input "{\"Name\":\"clidb\"}"                                              
  ```

  ```
  glueClient = boto3.client('glue')
  
  response = glueClient.create_database(
      DatabaseInput={
          'Name': 'boto3db'
      }
  )
  ```

 如需有關資料庫 API 資料類型、結構和操作的詳細資訊，請參閱[資料庫 API](https://docs.aws.amazon.com/glue/latest/dg/aws-glue-api-catalog-databases.html)。

 **後續步驟** 

 在下一節中，您將建立資料表，並將該資料表新增至您的資料庫。

您也可以探索 Data Catalog 的設定和許可。請參閱[使用 AWS Glue 主控台上的 Data Catalog 設定](https://docs.aws.amazon.com/glue/latest/dg/console-data-catalog-settings.html)。

## 步驟 2. 建立資料表
<a name="start-data-catalog-table"></a>

 在此步驟中，可使用 AWS Glue 主控台來建立資料表。

1.  在 AWS Glue 主控台的左側選單中，選擇 **Tables** (資料表)。

1.  選擇 **Add table** (新增資料表)。

1.  在 **Table details** (資料表詳細資訊) 中，輸入資料表的名稱以設定資料表的屬性。

1.  在 **Database** (資料庫) 區段中，從下拉式選單選擇您在步驟 1 中建立的資料庫。

1.  在 **Add a data store** (新增資料存放區) 區段中，預設將選取 **S3** 作為來源類型。

1.  對於 **Data is located in** (資料位置)，選擇 **Specified path in another account** (其他帳戶中的已指定路徑)。

1. 在 **Include path** (包含路徑) 輸入欄位中複製並貼上路徑：

   `s3://crawler-public-us-west-2/flight/2016/csv/`

1.  在 **Data format** (資料格式) 區段中，對於 **Classification** (分類)，選擇 **CSV**，對於 **Delimiter** (分隔符號)，選擇 **comma (,)** (逗號 (，))。選擇**下一步**。

1. 系統會要求您定義結構描述。結構描述定義資料記錄的結構和格式。選擇 **Add column** (新增資料欄)。(如需詳細資訊，請參閱[結構描述登錄檔](https://docs.aws.amazon.com/glue/latest/dg/schema-registry.html#schema-registry-schemas.html)。)

1.  指定資料欄屬性：

   1. 輸入資料欄名稱。

   1. 對於 **Column type** (資料欄類型)，依預設已選取 'string' (字串)。

   1. 對於 **Column number** (欄號)，依預設已選取 '1'。

   1. 選擇**新增**。

1.  系統會要求您新增分割區索引。這是選用的。要略過此步驟，請選擇 **Next** (下一步)。

1.  此時將顯示資料表屬性的摘要。如果一切皆如預期，請選擇**建立**。否則，請選擇 **Back** (返回) 並視需要進行編輯。

 恭喜您，您已成功手動建立資料表並將其關聯到資料庫。您最新新建的資料表將顯示在 Table (表) 儀表板中。對於儀表板，您可以修改和管理所有資料表。

 如需詳細資訊，請參閱[在 AWS Glue 主控台上使用資料表](https://docs.aws.amazon.com/glue/latest/dg/console-tables.html)。

## 後續步驟
<a name="start-data-catalog-next-steps"></a>

 **後續步驟** 

 現在已填入 Data Catalog，您可以開始在 AWS Glue 中編寫任務。請參閱[使用 AWS Glue Studio 建立視覺化 ETL 任務](https://docs.aws.amazon.com/glue/latest/dg/author-job-glue.html)。

 除了使用主控台之外，還有其他方法可以在 Data Catalog 中定義資料表，包括：
+  [建立和執行爬蟲程式](https://docs.aws.amazon.com/glue/latest/dg/add-crawler.html) 
+  [將分類器新增至 AWS Glue 中的編目程式](https://docs.aws.amazon.com/glue/latest/dg/add-classifier.html) 
+  [使用 AWS Glue 資料表 API](https://docs.aws.amazon.com/glue/latest/dg/aws-glue-api-catalog-tables.html) 
+  [使用 AWS Glue Data Catalog 範本](https://docs.aws.amazon.com/glue/latest/dg/populate-with-cloudformation-templates.html) 
+  [移轉 Apache Hive 中繼存放區](https://github.com/aws-samples/aws-glue-samples/tree/master/utilities/Hive_metastore_migration) 
+  [使用 AWS CLI](https://docs.aws.amazon.com/cli/latest/reference/glue/create-table.html) 、Boto3 或資料定義語言 (DDL)   
****  
 以下範例說明了如何使用 CLI、Boto3 或 DDL 根據您在教學中使用的 S3 儲存貯體中的相同 flights\$1data.csv 檔案定義資料表。  
 請參閱如何建構 AWS CLI 命令的文件。CLI 範例包含 'aws glue create-table --table-input' 值的 JSON 語法。  

  ```
  {
          "Name": "flights_data_cli",
          "StorageDescriptor": {
              "Columns": [
                  {
                      "Name": "year",
                      "Type": "bigint"
                  },
                  {
                      "Name": "quarter",
                      "Type": "bigint"
                  }
              ],
              "Location": "s3://crawler-public-us-west-2/flight/2016/csv",
              "InputFormat": "org.apache.hadoop.mapred.TextInputFormat",
              "OutputFormat": "org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat",
              "Compressed": false,
              "NumberOfBuckets": -1,
              "SerdeInfo": {
                  "SerializationLibrary": "org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe",
                  "Parameters": {
                      "field.delim": ",",
                      "serialization.format": ","
                  }
              }
          },
          "PartitionKeys": [
              {
                  "Name": "mon",
                  "Type": "string"
              }
          ],
          "TableType": "EXTERNAL_TABLE",
          "Parameters": {
              "EXTERNAL": "TRUE",
              "classification": "csv",
              "columnsOrdered": "true",
              "compressionType": "none",
              "delimiter": ",",
              "skip.header.line.count": "1",
              "typeOfData": "file"
          }
      }
  ```

  ```
  import boto3
  
  glue_client = boto3.client("glue")
  
  response = glue_client.create_table(
      DatabaseName='sampledb',
      TableInput={
          'Name': 'flights_data_manual',
      'StorageDescriptor': {
        'Columns': [{
          'Name': 'year',
          'Type': 'bigint'
        }, {
          'Name': 'quarter',
          'Type': 'bigint'
        }],
        'Location': 's3://crawler-public-us-west-2/flight/2016/csv',
        'InputFormat': 'org.apache.hadoop.mapred.TextInputFormat',
        'OutputFormat': 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat',
        'Compressed': False,
        'NumberOfBuckets': -1,
        'SerdeInfo': {
          'SerializationLibrary': 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe',
          'Parameters': {
            'field.delim': ',',
            'serialization.format': ','
          }
        },
      },
      'PartitionKeys': [{
        'Name': 'mon',
        'Type': 'string'
      }],
      'TableType': 'EXTERNAL_TABLE',
      'Parameters': {
        'EXTERNAL': 'TRUE',
        'classification': 'csv',
        'columnsOrdered': 'true',
        'compressionType': 'none',
        'delimiter': ',',
        'skip.header.line.count': '1',
        'typeOfData': 'file'
      }
      }
  )
  ```

  ```
  CREATE EXTERNAL TABLE `sampledb`.`flights_data` (
    `year` bigint, 
    `quarter` bigint)
  PARTITIONED BY ( 
    `mon` string)
  ROW FORMAT DELIMITED 
    FIELDS TERMINATED BY ',' 
  STORED AS INPUTFORMAT 
    'org.apache.hadoop.mapred.TextInputFormat' 
  OUTPUTFORMAT 
    'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
  LOCATION
    's3://crawler-public-us-west-2/flight/2016/csv/'
  TBLPROPERTIES (
    'classification'='csv', 
    'columnsOrdered'='true', 
    'compressionType'='none', 
    'delimiter'=',', 
    'skip.header.line.count'='1', 
    'typeOfData'='file')
  ```