
# CREATE TABLE AS SELECT
#### 命令功能
CREATE TABLE As SELECT命令通过指定带有表属性的字段列表来创建Hudi Table。在使用由DLI提供的元数据服务时仅可创建外表，即需要通过LOCATION指定表路径。
#### 命令格式
**CREATE TABLE** *\[ IF NOT EXISTS\] \[database_name.\]table_name*
**USING hudi**
*\[ COMMENT table_comment \]*
*\[ LOCATION location_path \]*
*\[ OPTIONS (options_list) \]*
*\[ AS query_statement \]*
#### 参数描述
表1CREATE TABLE As SELECT参数描述 
| **参数**          | **描述**                        |
|:---|:---|
| database_name   | Database名称，由字母、数字和下划线（_）组成。   |
| table_name      | Database中的表名，由字母、数字和下划线（_）组成。 |
| using           | 参数hudi，定义和创建Hudi table。       |
| table_comment   | 表的描述信息。                       |
| location_path   | OBS路径，指定该路径Hudi表会创建为外表。       |
| options_list    | Hudi table属性列表。               |
| query_statement | select查询表达式                   |
   
#### 示例
- 创建分区表
  ```
  create table h2 using hudi
  options (type = 'cow', primaryKey = 'id', preCombineField = 'dt')
  partitioned by (dt)
  as
  select 1 as id, 'a1' as name, 10 as price, 1000 as dt;
  ```
  
- 创建非分区表
  ```
  create table h3 using hudi
  options (type = 'cow', primaryKey = 'id', preCombineField = 'dt')
  as
  select 1 as id, 'a1' as name, 10 as price, 1000 as dt;
  从parquet表加载数据到hudi表
  # 创建parquet表
  create table parquet_mngd using parquet options(path=’obs://bucket/path/parquet_dataset/*.parquet’);
  # CTAS创建hudi表
  create table hudi_tbl using hudi location 'obs://bucket/path/hudi_tbl/' options (
  type = 'cow',
  primaryKey = 'id',
  preCombineField = 'ts'
  )
  partitioned by (datestr) as select * from parquet_mngd;
  ```
  
 
#### 注意事项
为了更好的加载数据性能，CTAS使用bulk insert作为写入方式。
#### 权限需求
由DLI提供的元数据服务
- SQL权限：
  
  | **database** | **table**  |
  |:---|:---|
  | CREATE_TABLE | 来源表：SELECT |
     
  
- 细粒度权限：dli:table:createTable, dli:table:select
由LakeFormation提供的元数据服务，权限配置详见LakeFormation文档。
#### 系统响应
Table创建成功，创建的Hudi表可以进入DLI控制台，在左侧菜单栏选择"数据管理"-\>"库表管理"，随后筛选数据库并点击名称，进入表列表查询。
