create [external] table if not exists 表名node
(列名数据类型 [comment 本列注释],...)安全
[comment 表注释]session
[partitioned by (列名数据类型 [comment 本列注释],...)]函数
[clustered by(列名,列名,...)]性能
[sorted by (列名 [asc|desc],...)] info num_buckets buckets]spa
[row format row_format][stored as file_format].net
[location hdfs_path]code
[tblproperties (property_name=property_value,...)]orm
[as select_statement]blog
说明:
①external表示建立外部表;hive在建立内部表时,会将数据移动到数据仓库指向的路径;若建立外部表,仅记录数据所在的路径,不对数据的位置作任何改变
②partitioned by表示建立分区表
③clustered by建立分桶表
④sorted by 不经常使用
⑤row format delimited [fields terminated by char] [collection items terminated by char] [map keys terminated by char] [line terminated by char]
⑥stored as 指定文件存储类型(sequencefile二进制文件、textfile文本文件、rcfile列式存储格式)
⑦location 指定表在hdfs上的存储位置
⑧like 容许用户复制现有的表结构,可是不复制数据
⑨as 后跟查询语句,根据查询结果建立表
desc formated 表名; //能够查看分区字段(partition)信息
desc 表名;
external:表示建立外部表,仅记录数据所在路径,不对数据作改变
不写external:表示建立内部表,会将数据移动到数据仓库指向的路径。
一、但愿作数据备份而且不常常改变的数据,存放在外部表能够减小失误操做。
二、数据清洗转换后的中间结果,能够存放在内部表,由于Hive对内部表支持的功能比较全面,方便管理。
三、处理完成的数据因为须要共享,能够存储在外部表,这样可以防止失误操做,增长数据的安全性。
四、在生产中通常建立外部表来存储数据。
修改表名
alter table A_A rename TO AA;
向表中添加列
ALTER TABLE A_A ADD COLUMNS ( a_name STRING COMMENT ' App name ' ,
a_id BIGINT COMMENT ' The current session id');
修改列名
ALTER TABLE 表名 change 原列名 新列名 新类型;
(慎用)删表:
drop table if exists table_name;
(慎用)清空表:
truncate table 表名;
扩展:
在Hive中查看函数功能(好比substr)
desc function extended substr;
查看全部Hive函数
show functions;
杀死进程 -好比有一个进程为 :11245 RunJar
kill -9 11245;
分区表:在必定程度上能够理解为分红文件夹。
Hive中有分区表的概念,咱们能够看到分区具备重要性能优点,分区表能够将数据以一种符合逻辑的方式进行组织,好比分层存储。
二、查询分区表中的数据时,除非where语句中包含分区字段过滤条件来显示数据范围,不然不容许执行。
三、换句话说,就是用户不容许扫描全部的分区。
四、进行这个显示的缘由是,一般分区表都拥有很是大的数据集,并且数据增长迅速。若是没有进行分区限制的查询可能会消耗使人不可接受的巨大资源来处理这个表。
五、分区是hive存放数据的一种方式。将列值做为目录来存放数据,就是一个分区。这样查询时使用分区列进行过滤,只需根据列值直接扫描对应目录下的数据,不扫描其余不关心的分区,快速定位,提升查询效率.
分区表分位静态分区和动态分区
静态分区:手动指定-->编译时期。
动态分区:经过输入数据来进行判断-->SQL语句。
通常按时间来分区:天,小时,分钟。
create table test (name string,age int) partitioned by (country string) row format delimited fields terminated by '\t' lines terminated by '\n' stored as textfile;
向分区中插入数据:
insert into table test partition(country="china") values("zhangsan",1); insert into table test partition(country="usa") values("james",34); insert into table test partition(country="usa") values("tom",2);
查询分区表的数据:
select * from test where country="china";
删除分区:
alter table test drop partition(country="china");
加载数据指定分区:
load data local inpath '/root/Desktop/student.txt' into table test partition(name='zs',age=21);
动态分区的相关配置属性:
set hive.exec.dynamic.partition=true; (可经过这个语句查看:set hive.exec.dynamic.partition;) set hive.exec.dynamic.partition.mode=nonstrict; SET hive.exec.max.dynamic.partitions=100000;(若是自动分区数 大于这个参数,将会报错) SET hive.exec.max.dynamic.partitions.pernode=100000;
显示分区数:
show partitions order_part;
查询分区表中的数据:
select * from user_trade limit 6; //这样会报错,由于没有加分区条件。
查看全部配置
set
严格模式:set hive.mapred.mode=strict;
set hive.mapred.mode=strict; select * from user_trade limit 6; select * from user_trade where dt='2017-01-12';
咱们应该
一、对分区表查询,用where过滤字段用分区字段。
二、禁止用笛卡尔积join查询,join查询语句,不带on条件或者where条件。
三、order by后面用limit。
一、分桶是对列值取hash值的方式将数据放在不一样的文件存储。
二、hive中的每个表、分区均可以进行分桶。
三、由列的hash值除以桶的个数来决定将每条数据具体划分在哪一个桶中。
应用场景:抽样、map-join
分桶我用的比较少,具体内容可自行百度。
参考文献:
https://blog.csdn.net/qq_39783601/article/details/104934245,
https://blog.csdn.net/MrBack/article/details/82379995,
开课吧