1. 查看建表语句,SQL语句展示
hive> show create table 表名 ; -- 关键字'table'需要写,不然报错
hive> show create table sales_info_test ;OKCREATE EXTERNAL TABLE `sales_info_test`( -- 外部表 `c1` bigint COMMENT 'c1的注释', `c2` string COMMENT 'c2的注释')COMMENT '测试表'PARTITIONED BY ( `dt` string) -- 分区字段ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat'OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'LOCATION 'hdfs:....../sales_info_test'TBLPROPERTIES ( 'transient_lastDdlTime'='1583838138')Time taken: 0.024 seconds, Fetched: 16 row(s)
2. 查看表结构,列表展示
hive> desc formatted 表名 ;
hive> desc formatted sales_info_test ;OK# col_name data_type commentc1 bigint c1的注释c2 string c2的注释# Partition Information# col_name data_type commentdt string# Detailed Table Information......# Storage Information......Time taken: 0.058 seconds, Fetched: 35 row(s)hive>
3. 查看表的分区信息
show partitions 表名; -- 如果写'partition'则报错
hive> show partitins sales_info_test;OKdt=2020-03-08dt=2020-03-09
4. 添加分区
alter table 表名 add partition(分区字段 = '分区字段值'); -- 如果写'partitions'则报错
hive> alter table sales_info_test add partition(dt = '2020-03-07');OKTime taken: 0.115 secondshive> show partitions sales_info_test ;OKdt=2020-03-07 -- 新添加的,有分区信息才能查 whete dt = '2020-03-07'的数据dt=2020-03-08dt=2020-03-09Time taken: 0.05 seconds, Fetched: 3 row(s)
5. 添加分区,指定数据的路径
hive> alter table 表名 add partitions(分区字段 = '分区字段值') location ('xxxxxxx') ;
6. 删除数据文件
会把'dt=2020-03-08'这个路径和下面所有的数据文件删除,其他dt路径不受影响
hive> dfs -rm -r hdfs:....../sales_info_test/dt=2020-03-08 ;
7. 创建路径
hive> dfs -mkdir hdfs:....../sales_info_test/dt=2020-03-09 ;
8. 复制数据
使用dfs -cp时 目的路径要先存在,不然报错'No such file or directory',再add partition即能查到数据。'/*'表示源路径下的所有文件。
hive> dfs -cp scr_path/dt=xxxx/* dest_path/dt=xxxx/ ;
9. 移动数据
使用dfs -mv时 目的路径要先存在,不然报错'No such file or directory',再add partition即能查到数据
hive> dfs -mv scr_path/dt=xxxx/* dest_path/dt=xxxx/ ;
10. 刷新分区
hive> msck repair table table_name; -- 'table'关键子需要写
注意:如果是先把分区的数据文件添加到表的location下,再msck能自动把新分区加上。如果是先把分区的数据文件删除后,再msck则不能自动把该分区信息删除,需要使用dorp partition语句。
11. 查看表的路径下每个分区的大小和路径
dfs -du -h
hive> dfs -du -h hdfs:....../sales_info_test ;OK21.6 K hdfs:....../sales_info_test/dt=2020-03-0836.8 K hdfs:....../sales_info_test/dt=2020-03-09
如果想更详细,可以使用'dfs -ls ' 含有数据文件的更新时间。
12. 查看表的数据文件总大小
dfs -du -h -s
hive> dfs -du -h -s hdfs:....../sales_info_test ;OK58.4 K hdfs:....../sales_info_test
13. kill某个任务
$ hadoop job -kill job_id
14. distcp复制数据文件(会启动MapReduce任务去复制)
以下命令是把源表的dt=xxxx下的所有数据文件复制到目的表的dt=xxxx下。即使目的路径(分区路径或表)不存在也不会报错,distcp会自动创建目的路径。distcp有很多配置,比如复制时的最大网络带宽、最大map任务个数、是覆盖还是追加、是否保留源文件时间等等。
$ hadoop distcp -overwrite hdfs:......源表/dt=xxxxhdfs:......目的表/dt=xxxx
注意:千万不要再在末尾添加'/'或者'/*',虽然不报错,但是结果不对,
15. 查看当前在哪个数据库下?
hive> select current_database() ;
16. 查看当前数据库下有哪些带有'xxxxx'的表?
hive> show tables like '*xxxxx*';
注意:单引号需要写,'%content%'无效
历史文章:
MySQL,case when你真的会用吗?附避坑指南
MySQL常用面试题(一)
Mysql 不走索引的情况
Mysql查询年的第多少周
Mysql,replace into,存在则更新,不存在则插入
Mysql修改字段名、字段类型、指定位置添加字段、删除字段、修改表名、表注释
「欢迎关注,一起学习,一起进步」