HIVE CREATE TABLE(一)

来源:互联网 发布:github php开源项目 编辑:程序博客网 时间:2024/05/20 22:41

前言

工作中用到hive比较多,但是总是感觉自己对hive的了解不够全面,仅限于常用的功能,对于hive缺乏全面的了解,造成在有新的业务需求时总是用常用的方法来解决问题,本次利用闲暇时间对hive的官方文档进行学习总结。关注那些平时用到但是没有深入了解的东西,提升自己对hive的认知。

本篇大概介绍hive建表语句,关于建表语句的详细解读,会在后边进行介绍。

hive的几种建表语句。

1.CREATE TABLE

CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name    -- (Note: TEMPORARY available in Hive 0.14.0 and later)  [(col_name data_type [COMMENT col_comment], ... [constraint_specification])]  [COMMENT table_comment]  [PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]  [CLUSTERED BY (col_name, col_name, ...) [SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]  [SKEWED BY (col_name, col_name, ...)                  -- (Note: Available in Hive 0.10.0 and later)]     ON ((col_value, col_value, ...), (col_value, col_value, ...), ...)     [STORED AS DIRECTORIES]  [   [ROW FORMAT row_format]    [STORED AS file_format]     | STORED BY 'storage.handler.class.name' [WITH SERDEPROPERTIES (...)]  -- (Note: Available in Hive 0.6.0 and later)  ]  [LOCATION hdfs_path]  [TBLPROPERTIES (property_name=property_value, ...)]   -- (Note: Available in Hive 0.6.0 and later)  [AS select_statement];   -- (Note: Available in Hive 0.5.0 and later; not supported for external tables)

最常用的建表语句,关于建表语句中的每个属性在后边说明。

2.Create Table As Select (CTAS)

如下例子

CREATE TABLE new_key_value_store   ROW FORMAT SERDE "org.apache.hadoop.hive.serde2.columnar.ColumnarSerDe"   STORED AS RCFile   ASSELECT (key % 1024) new_key, concat(key, value) key_value_pairFROM key_value_storeSORT BY new_key, key_value_pair;

新建的目标表可以使用一些表的属性,比如说serde和storage format。并且若是select的字段进行了操作后没有取别名,表中的名字会被定义为_col0, _col1,_col2这种形式,若是没有指定serde和storage format则会使用select 中表的serde和storage format
使用这种建表语句,目标表必须服从这些条件,因此使用这种方式需要根据需求:
1.目标表不能是分区表
2.目标表不能是外部表
3.The target table cannot be a list bucketing table.目标表不能使用CLUSTERED BY子句

3.Create Table Like

CREATE TABLE empty_key_value_storeLIKE key_value_store;

创建一个LIKE表的复制,但是不含有数据。

本节主要介绍了HIVE的三种建表方式,下边介绍hive建表语句中所涉及中的一些关键语句的功能。

参考文献

1.hive官方文档

原创粉丝点击