更新时间:2023-03-17 GMT+08:00

Hive SQL

Hive SQL支持Hive-3.1.0版本中的所有特性,详情请参见https://cwiki.apache.org/confluence/display/hive/languagemanual

系统提供的扩展Hive语句如表1所示。

表1 扩展Hive语句

扩展语法

语法说明

语法示例

示例说明

CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name (col_name data_type [COMMENT col_comment], ...) [ROW FORMAT row_format] [STORED AS file_format] | STORED BY 'storage.handler.class.name' [WITH SERDEPROPERTIES (...) ] ...... [TBLPROPERTIES ("groupId"=" group1 ","locatorId"="locator1")] ...;

创建一个hive表,并指定表数据文件分布的locator信息。详细说明请参见使用HDFS Colocation存储Hive表

CREATE TABLE tab1 (id INT, name STRING) row format delimited fields terminated by '\t' stored as RCFILE TBLPROPERTIES("groupId"=" group1 ","locatorId"="locator1");

创建表tab1,并指定tab1的表数据分布在locator1节点上。

CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name (col_name data_type [COMMENT col_comment], ...) [ROW FORMAT row_format] [STORED AS file_format] | STORED BY 'storage.handler.class.name' [WITH SERDEPROPERTIES (...) ] ... [TBLPROPERTIES ('column.encode.columns'='col_name1,col_name2'| 'column.encode.indices'='col_id1,col_id2', 'column.encode.classname'='encode_classname')]...;

创建一个hive表,并指定表的加密列和加密算法。详细说明请参见使用Hive列加密功能

create table encode_test(id INT, name STRING, phone STRING, address STRING) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' WITH SERDEPROPERTIES ('column.encode.indices'='2,3', 'column.encode.classname'='org.apache.hadoop.hive.serde2.SMS4Rewriter') STORED AS TEXTFILE;

创建表encode_test,并指定插入数据时对第2、3列加密,加密算法类为org.apache.hadoop.hive.serde2.SMS4Rewriter。

REMOVE TABLE hbase_tablename [WHERE where_condition];

删除hive on hbase表中符合条件的数据。详细说明请参见删除Hive on HBase表中的单行记录

remove table hbase_table1 where id = 1;

删除表中符合条件“id =1”的数据。

CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name (col_name data_type [COMMENT col_comment], ...) [ROW FORMAT row_format] STORED AS inputformat 'org.apache.hadoop.hive.contrib.fileformat.SpecifiedDelimiterInputFormat' outputformat 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat';

创建hive表,并设定表可以指定自定义行分隔符。详细说明请参见自定义行分隔符

create table blu(time string, num string, msg string) row format delimited fields terminated by ',' stored as inputformat 'org.apache.hadoop.hive.contrib.fileformat.SpecifiedDelimiterInputFormat' outputformat 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat';

创建表blu,指定inputformat为SpecifiedDelimiterInputFormat,以便查询时可以指定表的查询行分隔符。