

# CTAS 查询的示例
<a name="ctas-examples"></a>

使用以下示例创建 CTAS 查询。有关 CTAS 语法的信息，请参阅[CREATE TABLE AS](create-table-as.md)。

本节内容：
+  [Example: Duplicating a table by selecting all columns](#ctas-example-dupe-table) 
+  [Example: Selecting specific columns from one or more tables](#ctas-example-specify-columns) 
+  [Example: Creating an empty copy of an existing table](#ctas-example-empty-table) 
+  [Example: Specifying data storage and compression formats](#ctas-example-compression) 
+  [Example: Writing query results to a different format](#ctas-example-format) 
+  [Example: Creating unpartitioned tables](#ctas-example-unpartitioned) 
+  [Example: Creating partitioned tables](#ctas-example-partitioned) 
+  [Example: Creating bucketed and partitioned tables](#ctas-example-bucketed) 
+  [Example: Creating an Iceberg table with Parquet data](#ctas-example-iceberg-parquet) 
+  [Example: Creating an Iceberg table with Avro data](#ctas-example-iceberg-avro) 
+  [Example: Creating an S3 table using CTAS](#ctas-example-s3-table) 

**Example 示例：通过选择所有列复制表**  
以下示例通过复制表的所有列来创建表：  

```
CREATE TABLE new_table AS 
SELECT * 
FROM old_table;
```
在同一个示例的下列变化中，您的 `SELECT` 语句还包括 `WHERE` 子句。在这种情况下，查询将只从表中选择满足 `WHERE` 子句的行：  

```
CREATE TABLE new_table AS 
SELECT * 
FROM old_table 
WHERE condition;
```

**Example 示例：从一个或多个表选择特定列**  
以下示例创建运行在其他表的一组列上的新查询：  

```
CREATE TABLE new_table AS 
SELECT column_1, column_2, ... column_n 
FROM old_table;
```
同一个示例的此变化从多个表的特定列创建新表：  

```
CREATE TABLE new_table AS
SELECT column_1, column_2, ... column_n 
FROM old_table_1, old_table_2, ... old_table_n;
```

**Example 示例：创建现有表的空副本**  
以下示例使用 `WITH NO DATA` 创建空的新表，该表与原始表具有相同架构：  

```
CREATE TABLE new_table 
AS SELECT * 
FROM old_table
WITH NO DATA;
```

**Example 示例：指定数据存储和压缩格式**  
借助 CTAS，您可以使用一种存储格式的源表创建不同存储格式的另一个表。  
使用 `format` 属性以将 `ORC`、`PARQUET`、`AVRO`、`JSON` 或 `TEXTFILE` 指定为新表的存储格式。  
对于 `PARQUET`、`ORC`、`TEXTFILE` 和 `JSON` 存储格式，使用 `write_compression` 属性指定新表数据的压缩格式。有关每种文件格式支持的压缩格式的信息，请参阅 [在 Athena 中使用压缩](compression-formats.md)。  
以下示例指定表 `new_table` 中的数据以 Parquet 格式存储并使用 Snappy 压缩。Parquet 的默认压缩格式为 `GZIP`。  

```
CREATE TABLE new_table
WITH (
      format = 'Parquet',
      write_compression = 'SNAPPY')
AS SELECT *
FROM old_table;
```
以下示例指定表 `new_table` 中的数据以 ORC 格式存储并使用 Snappy 压缩。ORC 的默认压缩格式为 ZLIB。  

```
CREATE TABLE new_table
WITH (format = 'ORC',
      write_compression = 'SNAPPY')
AS SELECT *
FROM old_table ;
```
以下示例指定表 `new_table` 中的数据以 Textfile 格式存储并使用 Snappy 压缩。Textfile 和 JSON 格式的默认压缩格式都是 GZIP。  

```
CREATE TABLE new_table
WITH (format = 'TEXTFILE',
      write_compression = 'SNAPPY')
AS SELECT *
FROM old_table ;
```

**Example 示例：将查询结果写入不同格式**  
以下 CTAS 查询从 `old_table` 中选择能够以 CSV 或其他格式存储的所有记录，并创建一个新表，其中包含以 ORC 格式保存到 Amazon S3 的基础数据：  

```
CREATE TABLE my_orc_ctas_table
WITH (
      external_location = 's3://amzn-s3-demo-bucket/my_orc_stas_table/',
      format = 'ORC')
AS SELECT * 
FROM old_table;
```

**Example 示例：创建未分区表**  
以下示例创建未分区的表。表数据以不同格式存储。其中一些示例指定了外部位置。  
以下示例创建使用文本文件存储结果的 CTAS 查询：  

```
CREATE TABLE ctas_csv_unpartitioned 
WITH (
     format = 'TEXTFILE', 
     external_location = 's3://amzn-s3-demo-bucket/ctas_csv_unpartitioned/') 
AS SELECT key1, name1, address1, comment1
FROM table1;
```
在以下示例中，结果以 Parquet 格式存储，并使用默认结果位置：  

```
CREATE TABLE ctas_parquet_unpartitioned 
WITH (format = 'PARQUET') 
AS SELECT key1, name1, comment1
FROM table1;
```
在以下查询中，表以 JSON 格式存储，并从原始表的结果中选择特定列：  

```
CREATE TABLE ctas_json_unpartitioned 
WITH (
     format = 'JSON',  
     external_location = 's3://amzn-s3-demo-bucket/ctas_json_unpartitioned/') 
AS SELECT key1, name1, address1, comment1
FROM table1;
```
在下面的示例中，格式为 ORC：  

```
CREATE TABLE ctas_orc_unpartitioned 
WITH (
     format = 'ORC') 
AS SELECT key1, name1, comment1 
FROM table1;
```
在下面的示例中，格式为 Avro：  

```
CREATE TABLE ctas_avro_unpartitioned 
WITH (
     format = 'AVRO', 
     external_location = 's3://amzn-s3-demo-bucket/ctas_avro_unpartitioned/') 
AS SELECT key1, name1, comment1
FROM table1;
```

**Example 示例：创建分区表**  
以下示例显示了对不同存储格式的分区表的 `CREATE TABLE AS SELECT` 查询，在 `WITH` 子句中使用 `partitioned_by` 和其他属性。有关语法，请参阅 [CTAS 表属性](create-table-as.md#ctas-table-properties)。有关为分区选择列的更多信息，请参阅[使用分区和分桶](ctas-partitioning-and-bucketing.md)。  
在 `SELECT` 语句中列列表的结尾列出分区列。您可以按多个列进行分区，并拥有多达 100 个唯一的分区和存储桶组合。例如，如果未指定存储桶，则可以有 100 个分区。

```
CREATE TABLE ctas_csv_partitioned 
WITH (
     format = 'TEXTFILE',  
     external_location = 's3://amzn-s3-demo-bucket/ctas_csv_partitioned/', 
     partitioned_by = ARRAY['key1']) 
AS SELECT name1, address1, comment1, key1
FROM tables1;
```

```
CREATE TABLE ctas_json_partitioned 
WITH (
     format = 'JSON', 
     external_location = 's3://amzn-s3-demo-bucket/ctas_json_partitioned/', 
     partitioned_by = ARRAY['key1']) 
AS select name1, address1, comment1, key1 
FROM table1;
```

**Example 示例：创建分桶和分区表**  
以下示例显示同时使用分区和分桶在 Amazon S3 中存储查询结果的 `CREATE TABLE AS SELECT` 查询。表结果按照不同列分区和分桶。Athena 支持最多 100 个唯一的存储桶和分区组合。例如，如果您创建包含五个存储桶的表，则支持 20 个分区，每个分区包含五个存储桶。有关语法，请参阅 [CTAS 表属性](create-table-as.md#ctas-table-properties)。  
有关为分桶选择列的信息，请参阅[使用分区和分桶](ctas-partitioning-and-bucketing.md)。  

```
CREATE TABLE ctas_avro_bucketed 
WITH (
      format = 'AVRO', 
      external_location = 's3://amzn-s3-demo-bucket/ctas_avro_bucketed/', 
      partitioned_by = ARRAY['nationkey'], 
      bucketed_by = ARRAY['mktsegment'], 
      bucket_count = 3) 
AS SELECT key1, name1, address1, phone1, acctbal, mktsegment, comment1, nationkey 
FROM table1;
```

**Example 示例：使用 Parquet 数据创建 Iceberg 表**  
以下示例将创建一个包含有 Parquet 数据文件的 Iceberg 表。文件按月根据 `table1` 中的 `dt` 列进行分区。该示例更新了表的保留属性，因此默认情况下，表中每个分支上都会保留 10 个快照。过去 7 天内的快照也会保留。有关 Athena 中 Iceberg 表属性的更多信息，请参阅 [指定表属性](querying-iceberg-creating-tables.md#querying-iceberg-table-properties)。  

```
CREATE TABLE ctas_iceberg_parquet
WITH (table_type = 'ICEBERG',
      format = 'PARQUET', 
      location = 's3://amzn-s3-demo-bucket/ctas_iceberg_parquet/', 
      is_external = false,
      partitioning = ARRAY['month(dt)'],
      vacuum_min_snapshots_to_keep = 10,
      vacuum_max_snapshot_age_seconds = 604800
   ) 
AS SELECT key1, name1, dt FROM table1;
```

**Example 示例：使用 Avro 数据创建 Iceberg 表**  
以下示例将创建一个按 `key1` 分区的包含有 Avro 数据文件的 Iceberg 表。  

```
CREATE TABLE ctas_iceberg_avro
WITH ( format = 'AVRO', 
       location = 's3://amzn-s3-demo-bucket/ctas_iceberg_avro/', 
       is_external = false,
       table_type = 'ICEBERG',
       partitioning = ARRAY['key1']) 
AS SELECT key1, name1, date FROM table1;
```

**Example 示例：使用 CTAS 创建 S3 表**  
以下示例使用 CTAS 创建 S3 表。请注意，位置属性将被忽略，且 `table_type` 默认为 `ICEBERG`：  

```
CREATE TABLE "s3tablescatalog/amzn-s3-demo-bucket"."namespace"."s3-table-name"
WITH (
    format = 'PARQUET'
)
AS SELECT *
FROM source_table;
```
您可以使用与常规 Iceberg 表相同的语法来指定所有其他 Iceberg 表属性，例如分区和存储桶。