hive表信息查询、查看表结构、表操作等

转载:http://blog.csdn.net/lskyne/article/details/38427895

问题导读:
1.如何查看hive表结构?
2.如何查看表结构信息?
3.如何查看分区信息?
4.哪个命令可以模糊搜索表

1.hive模糊搜索表

[html]  view plain  copy
  1. show tables like '*name*';  

2.查看表结构信息

[html]  view plain  copy
  1. desc formatted table_name;  
  2. desc table_name;  

3.查看分区信息

[html]  view plain  copy
  1. show partitions table_name;  

4.根据分区查询数据

[html]  view plain  copy
  1. select table_coulm from table_name where partition_name = '2014-02-25';  

5.查看hdfs文件信息

[html]  view plain  copy
  1. dfs -ls /user/hive/warehouse/table02;  

6.从文件加载数据进表(OVERWRITE覆盖,追加不需要OVERWRITE关键字)

[html]  view plain  copy
  1. LOAD DATA LOCAL INPATH 'dim_csl_rule_config.txt' OVERWRITE into table dim.dim_csl_rule_config;  
  2.  --从查询语句给table插入数据  
  3. INSERT OVERWRITE TABLE test_h02_click_log PARTITION(dt) select *   
  4. from stage.s_h02_click_log where dt='2014-01-22' limit 100;  

7.导出数据到文件


hive> insert overwrite local directory '/opt/xuzhiguo' select * from pdca_factory_t where factory_id<4;
Query ID = root_20171228114351_ab465584-9575-4ec2-a6ad-6b7e6ecf227d
Total jobs = 1
Launching Job 1 out of 1
In order to change the average load for a reducer (in bytes):
  set hive.exec.reducers.bytes.per.reducer=<number>
In order to limit the maximum number of reducers:
  set hive.exec.reducers.max=<number>
In order to set a constant number of reducers:
  set mapreduce.job.reduces=<number>
Starting Spark Job = d4aa5361-e1d3-4b0a-802a-62a3c8124a13

Query Hive on Spark job[0] stages:
0

Status: Running (Hive on Spark job[0])
Job Progress Format
CurrentTime StageId_StageAttemptId: SucceededTasksCount(+RunningTasksCount-FailedTasksCount)/TotalTasksCount [StageCost]
2017-12-28 11:44:13,438 Stage-0_0: 0(+1)/1
2017-12-28 11:44:16,547 Stage-0_0: 0(+1)/1
2017-12-28 11:44:19,602 Stage-0_0: 0(+1)/1
2017-12-28 11:44:22,660 Stage-0_0: 0(+1)/1
2017-12-28 11:44:25,692 Stage-0_0: 0(+1)/1
2017-12-28 11:44:28,724 Stage-0_0: 0(+1)/1
2017-12-28 11:44:29,741 Stage-0_0: 1/1 Finished
Status: Finished successfully in 30.46 seconds
Moving data to local directory /opt/xuzhiguo
OK
Time taken: 39.248 seconds
hive> 


[html]  view plain  copy
  1. insert overwrite directory '/tmp/csl_rule_cfg' select a.* from dim.dim_csl_rule_config a;  
  2.  hive -e "select day_id,pv,uv,ip_count,click_next_count,second_bounce_rate,return_visit,pg_type   
[html]  view plain  copy
  1. from tmp.tmp_h02_click_log_baitiao_ag_sum where day_id in ('2014-03-06','2014-03-07','2014-03-08','2014-03-09','2014-03-10');">   
[html]  view plain  copy
  1. /home/jrjt/testan/baitiao.dat;  

8.自定义udf函数

  1.继承UDF类
  2.重写evaluate方法
  3.把项目打成jar包
  4.hive中执行命令add jar /home/jrjt/dwetl/PUB/UDF/udf/GetProperty.jar;
  5.创建函数create temporary function get_pro as 'jd.Get_Property'//jd.jd.Get_Property为类路径;

9.查询显示列名 及 行转列显示 

[html]  view plain  copy
  1. set hive.cli.print.header=true; // 打印列名  
  2. set hive.cli.print.row.to.vertical=true; // 开启行转列功能, 前提必须开启打印列名功能  
  3. set hive.cli.print.row.to.vertical.num=1; // 设置每行显示的列数  

10.查看表文件大小,下载文件到某个目录,显示多少行到某个文件

[html]  view plain  copy
  1. dfs -du hdfs://BJYZH3-HD-JRJT-4137.jd.com:54310/user/jrjt/warehouse/stage.db/s_h02_click_log;  
  2. dfs -get /user/jrjt/warehouse/ods.db/o_h02_click_log_i_new/dt=2014-01-21/000212_0 /home/jrjt/testan/;  
  3. head -n 1000 文件名 > 文件名  

11.杀死某个任务  不在hive shell中执行

[html]  view plain  copy
  1. hadoop job -kill job_201403041453_58315  

12.hive-wui路径

    http://172.17.41.38/jobtracker.jsp

13.删除分区

[html]  view plain  copy
  1. alter table tmp_h02_click_log_baitiao drop partition(dt='2014-03-01');  
  2. alter table d_h02_click_log_basic_d_fact drop partition(dt='2014-01-17');  

14.hive命令行操作15.hive上操作hadoop文件基本命令

    查看文件大小

[html]  view plain  copy
  1. dfs -du /user/jrjt/warehouse/tmp.db/tmp_h02_click_log/dt=2014-02-15;  

    删除文件

[html]  view plain  copy
  1. dfs -rm /user/jrjt/warehouse/tmp.db/tmp_h02_click_log/dt=2014-02-15;  

16.插入数据sql、导出数据sql
    1.insert 语法格式为:

    基本的插入语法:

[html]  view plain  copy
  1. INSERT OVERWRITE TABLE tablename [PARTITON(partcol1=val1,partclo2=val2)]select_statement FROM from_statement  
  2. insert overwrite table test_insert select * from test_table;  

    对多个表进行插入操作:

[html]  view plain  copy
  1. FROM fromstatte  
  2. INSERT OVERWRITE TABLE tablename1 [PARTITON(partcol1=val1,partclo2=val2)]select_statement1  
  3. INSERT OVERWRITE TABLE tablename2 [PARTITON(partcol1=val1,partclo2=val2)]select_statement2  
  4.   
  5. from test_table                       
  6. insert overwrite table test_insert1   
  7. select key  
  8. insert overwrite table test_insert2  
  9. select value;  

    insert的时候,from子句即可以放在select 子句后面,也可以放在 insert子句前面。

    hive不支持用insert语句一条一条的进行插入操作,也不支持update操作。数据是以load的方式加载到建立好的表中。数据一旦导入就不可以修改。

    2.通过查询将数据保存到filesystem

[html]  view plain  copy
  1. INSERT OVERWRITE [LOCAL] DIRECTORY directory SELECT.... FROM .....  
    导入数据到本地目录:

[html]  view plain  copy
  1. insert overwrite local directory '/home/zhangxin/hive' select * from test_insert1;  

    产生的文件会覆盖指定目录中的其他文件,即将目录中已经存在的文件进行删除。


    导出数据到HDFS中:

[html]  view plain  copy
  1. insert overwrite directory '/user/zhangxin/export_test' select value from test_table;  

    同一个查询结果可以同时插入到多个表或者多个目录中:

[html]  view plain  copy
  1. from test_insert1  
  2. insert overwrite local directory '/home/zhangxin/hive' select *   
  3. insert overwrite directory '/user/zhangxin/export_test' select value;  

17.mapjoin的使用 应用场景:1.关联操作中有一张表非常小 2.不等值的链接操作

[html]  view plain  copy
  1. select /*+ mapjoin(A)*/ f.a,f.b from A t join B f  on ( f.a=t.a and f.ftime=20110802)   

18.perl启动任务

[html]  view plain  copy
  1. perl /home/jrjt/dwetl/APP/APP/A_H02_CLICK_LOG_CREDIT_USER/bin/a_h02_click_log_credit_user.pl   
  2. APP_A_H02_CLICK_LOG_CREDIT_USER_20140215.dir >& /home/jrjt/dwetl/LOG/APP/20140306/  
[html]  view plain  copy
  1. <span style="white-space:pre;">                     </span> a_h02_click_log_credit_user.pl.4.log  

19.查看perl进程

[html]  view plain  copy
  1. ps -ef|grep perl  

20.hive命令移动表数据到另外一张表目录下并添加分区

[html]  view plain  copy
  1. dfs -cp /user/jrjt/warehouse/tmp.db/tmp_h02_click_log/dt=2014-02-18 /user/jrjt/warehouse/ods.db/o_h02_click_log/;  
  2. dfs -cp /user/jrjt/warehouse/tmp.db/tmp_h02_click_log_baitiao/* /user/jrjt/warehouse/dw.db/  
[html]  view plain  copy
  1. <span style="white-space:pre;">                         </span>d_h02_click_log_baitiao_basic_d_fact/;--复制所有分区数据  
  2.  alter table d_h02_click_log_baitiao_basic_d_fact add partition(dt='2014-03-11')   
[html]  view plain  copy
  1. <span style="white-space:pre;"> </span>location '/user/jrjt/warehouse/dw.db/d_h02_click_log_baitiao_basic_d_fact/dt=2014-03-11';  

21.导出白条数据

[html]  view plain  copy
  1. hive -e "select day_id,pv,uv,ip_count,click_next_count,second_bounce_rate,return_visit,pg_type   
[html]  view plain  copy
  1. from tmp.tmp_h02_click_log_baitiao_ag_sum where day_id like '2014-03%';"> /home/jrjt/testan/baitiao.xlsx;  

22.hive修改表名

[html]  view plain  copy
  1. ALTER TABLE o_h02_click_log_i RENAME TO o_h02_click_log_i_bk;  

23.hive复制表结构

[html]  view plain  copy
  1. CREATE TABLE d_h02_click_log_baitiao_ag_sum LIKE tmp.tmp_h02_click_log_baitiao_ag_sum;  

24.hive官网网址

    https://cwiki.apache.org/conflue ... ionandConfiguration

   http://www.360doc.com/content/12/0111/11/7362_178698714.shtml

25.hive添加字段

[html]  view plain  copy
  1. alter table tmp_h02_click_log_baitiao_ag_sum   
[html]  view plain  copy
  1. <span style="white-space:pre;"> </span>add columns(current_session_timelenth_count bigint comment '页面停留总时长');  
  2.  ALTER TABLE tmp_h02_click_log_baitiao CHANGE current_session_timelenth   
[html]  view plain  copy
  1. <span style="white-space:pre;"> </span>current_session_timelenth bigint comment '当前会话停留时间';  

26.hive开启简单模式不启用mr

[html]  view plain  copy
  1. set hive.fetch.task.conversion=more;  

27.以json格式输出执行语句会读取的input table和input partition信息

[html]  view plain  copy
  1. Explain dependency query  
  • 11
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值