ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
``` -- 演示用表people 0: jdbc:hive2://hadoop101:10000> select * from people; +-------------------+---------------------+--------------------+---------------------------+--+ | people.people_id | people.people_name | people.people_age | people.people_start_date | +-------------------+---------------------+--------------------+---------------------------+--+ | 1 | tom | 23 | 2019-03-16 | | 2 | jack | 12 | 2019-03-13 | | 3 | robin | 14 | 2018-08-13 | | 4 | justin | 34 | 2018-10-12 | | 5 | jarry | 24 | 2017-11-11 | | 6 | jasper | 24 | 2017-12-12 | +-------------------+---------------------+--------------------+---------------------------+--+ ``` <br/> **1. CTAS方式建表 - `as select`** * CTAS方式不能创建partition表、external表、bucket表; ```sql -- 将people表的数据复制一份到ctas_people表中 create table if not exists ctas_people as select * from people; 0: jdbc:hive2://hadoop101:10000> select * from ctas_people; +------------------------+--------------------------+-------------------------+--------------------------------+--+ | ctas_people.people_id | ctas_people.people_name | ctas_people.people_age | ctas_people.people_start_date | +------------------------+--------------------------+-------------------------+--------------------------------+--+ | 1 | tom | 23 | 2019-03-16 | | 2 | jack | 12 | 2019-03-13 | | 3 | robin | 14 | 2018-08-13 | | 4 | justin | 34 | 2018-10-12 | | 5 | jarry | 24 | 2017-11-11 | | 6 | jasper | 24 | 2017-12-12 | +------------------------+--------------------------+-------------------------+--------------------------------+--+ ``` 在hdfs中的位置为,我当前使用的数据库是hivebook: ```sql /hivebook.db/ctas_people/000000_0 (000000_0是一个文件) ``` **2. CTE方式建表 - `as as with table_name as as`** ```sql create table if not exists cte_people as with r1 as (select people_name from r2 where people_name="jack"), r2 as (select people_name from people where people_age=12), r3 as (select people_name from people where people_age=14) select * from r1 union all select * from r3; 0: jdbc:hive2://hadoop101:10000> select * from cte_people; +-------------------------+--+ | cte_people.people_name | +-------------------------+--+ | jack | | robin | +-------------------------+--+ ``` 在hdfs中的位置为,我当前使用的数据库是hivebook: ```sql /hivebook.db/cte_people/000000_0 (000000_0是一个文件) ``` **3. `like`方式建表,只复制表结构,不复制数据** ```sql -- 复制people表的结构到like_people表中 create table if not exists like_people like people; 0: jdbc:hive2://hadoop101:10000> select * from like_people; +------------------------+--------------------------+-------------------------+--------------------------------+--+ | like_people.people_id | like_people.people_name | like_people.people_age | like_people.people_start_date | +------------------------+--------------------------+-------------------------+--------------------------------+--+ +------------------------+--------------------------+-------------------------+--------------------------------+--+ ``` 在hdfs中的位置为,我当前使用的数据库是hivebook: ```sql /hivebook.db/like_people ```