
# CREATE TABLE
![](https://support.huaweicloud.com/cmpntguide-lts-mrs/public_sys-resources/note_3.0-zh-cn.png)
MRS 3.6.0-LTS.1版本简化了Hudi建表的相关参数，详情请参考[Hudi创建表参数简化指导](https://support.huaweicloud.com/cmpntguide-lts-mrs/mrs_01_300859.html)章节。
#### 命令功能
CREATE TABLE命令通过指定带有表属性的字段列表来创建Hudi Table。
#### 命令格式
**CREATE TABLE** *\[ IF NOT EXISTS\] \[database_name.\]table_name*
*\[ (columnTypeList)\]*
**USING hudi**
*\[ COMMENT table_comment \]*
*\[ LOCATION location_path \]*
*\[ OPTIONS (options_list) \]*
#### 参数描述
表1CREATE TABLE参数描述 
| **参数**         | **描述**                            |
|:---|:---|
| database_name  | Database名称，由字母、数字和下划线（_）组成。       |
| table_name     | Database中的表名，由字母、数字和下划线（_）组成。     |
| columnTypeList | 以逗号分隔的带数据类型的列表。列名由字母、数字和下划线（_）组成。 |
| using          | 参数hudi，定义和创建Hudi table。           |
| table_comment  | 表的描述信息。                           |
| location_path  | HDFS路径，指定该路径Hudi表会创建为外表。          |
| options_list   | Hudi table属性列表。                   |
   
表2CREATE TABLE Options描述 
| **参数**          | **描述**                                                                                                                                                                                 |
|:---|:---|
| primaryKey      | 主键名，多个字段用逗号分隔，该字段为必填字段。                                                                                                                                                                |
| type            | 表类型。"cow"表示 COPY-ON-WRITE表，"mor"表示MERGE-ON-READ表。未指定type的话，默认值为"cow"。                                                                                                                  |
| preCombineField | 表的Pre-Combine字段，该字段为必填字段，用于标识相同主键的两条数据先后顺序。配合主键使用，实现数据按主键合并。                                                                                                                           |
| payloadClass    | 使用preCombineField字段进行数据过滤的逻辑，默认使用DefaultHoodieRecordPayload，同时也提供了多种预置Payload供用户使用，如OverwriteNonDefaultsWithLatestAvroPayload、OverwriteWithLatestAvroPayload及EmptyHoodieRecordPayload。 |
| useCache        | 是否在Spark中缓存表的relation，无需用户配置。为支持SparkSQL中对COW表增量视图查询，默认将COW表中该值置为false。                                                                                                                |
   
#### 示例
- **创建非分区表**
  ```
  create table if not exists hudi_table0 (
  id int,
  name string,
  price double
  ) using hudi
  options (
  type = 'cow',
  primaryKey = 'id',
  preCombineField = 'price'
  );
  ```
  

- **创建分区表**
  ```
  create table if not exists hudi_table_p0 (
  id bigint,
  name string,
  ts bigint,
  dt string,
  hh string
  ) using hudi
  options (
  type = 'cow',
  primaryKey = 'id',
  preCombineField = 'ts'
  )
  partitioned by (dt, hh);
  ```
  
- **在指定路径下创建表**
  ```
  create table if not exists h3(
  id bigint,
  name string,
  price double
  ) using hudi
  options (
  primaryKey = 'id',
  preCombineField = 'price'
  )
  location '/path/to/hudi/h3';
  ```
  
 
- **创建表指定表属性**
  ```
  create table if not exists h3(
  id bigint,
  name string,
  price double
  ) using hudi
  options (
  primaryKey = 'id',
  type = 'mor',
  hoodie.cleaner.fileversions.retained = '20',
  hoodie.keep.max.commits = '20'
  );
  ```
  
#### 注意事项
- Hudi当前只有int、bigint、float、double、decimal、string、date、timestamp、boolean、binary类型支持设置默认值。
- Hudi表必须指定primaryKey与preCombineField。
- 在指定路径下创建表时，如果路径下已存在Hudi表，则建表时不需要指定列。
- MRS 3.5.0-LTS及之后版本：Hudi的列名建议统一使用小写（保证各个引擎之间的大小写兼容性），且不能以数字开头。
 
#### 系统响应
Table创建成功，创建成功的消息将被记录在系统日志中。
