hive常用命令和外部写建表语句执行报错的解决方法

如果建表语句中有类型关键字,在建表是会报NoViableAltException错误,需要在关键字上加``(这个是ESC底下那个键)

本人常用的hive命令:

  1.不用启动hive就能运行建表语句

hive -f xx.sql ;

 注意:建表语句如果是外部编译器编写的话要更改编码格式为

要不会爆这个错误

  2.添加列

alter table dev_odb.cac_am_aa_maint_hist add columns (increment_id bigint comment 'fsdf');

 3.获取建表语句

show create table xx;

4.查看表有哪些分区

show partitions xx表;

5.hive模糊搜索表

 show tables like '*name*';

6.查看表结构信息

desc formatted table_name;
 desc table_name;

7.根据分区查看数据

select * from xx表 where partition_name='xx';

8.更改表的注释

ALTER TABLE 表名 SET TBLPROPERTIES('comment' = '(旧)担保合同基本信息表');

 更改列的注释,这个语句同时能更改列名,列类型,和注释(在添加新列的时候需要先添加列,再添加列的注释)

alter table xx表 change column 列名 新的列名 新的列类型 comment '新的注释';
    eg:alter table ll change column o oo int comment '的';

更改列的位置

alter table 表名 change column 列名 列名 类型 after 列名;
eg:alter table pppp change column b b string after aaa; 把b列放在aaa之后

9.把hive数据查到下载到本地服务中

insert overwrite local directory '/root/a' select * from a;

不加local是把查到的数据放到hdfs文件目录中

10.加载数据到hive表中

允许动态分区:要先设置set hive.exec.dynamic.partition=true;

允许所有的分区列都是动态分区列:set hive.exec.dynamic.partition.mode=nonstrict;

load data (local) inpath '数据文件的位置' (overwrite) into table xx表;
insert overwrite table xx表 select * from xxx表;//把xxx表中的数据加载到xx表中
分区值一个是动态插入,一个固定的值插入
--动态分区 分区值根据sql值来插入
insert overwrite table x表 partition(分区名) select xxx表中的列名 as 分区名 from xxx表;//把xxx表中的数据加载到xx表分区中
--静态分区 
insert overwrite table x表 partition(分区名或分区名='固定的值') select * from xxx表;//把xxx表中的数据加载到xx表分区中

如果select * from table xx表 的数据为空,则x表分区也不会创建。
动态分区中,*表的字段个数是xxx表字段个数的+1个,因为分区算成一个列

例子可以看:https://blog.csdn.net/qq_26442553/article/details/80382174

11.在hive中查看hdfs文件信息

dfs -ls /root/a;

12.不启动hive执行hiveql

hive -e "select * from 表名";

13.把hive表查询的数据放到另一张hive表中

create table xx as select * form xxx;//把xxx表复制到xx表中

创建一个表结构相同的表

CREATE TABLE xx表 like xxx表;

14.更改表的列名/把某一列改到第一列上

alter table 表名 change column 列名 列名 要更改的列类型;//更改列的类型
    eg:alter table a change column test test bigint;
alter table 表名 change column 列名 列名 列名的类型 first;//把当前列放到第一个
    eg:alter table test.a change column test test bigint first;//test.a是因为我use test库

15.删除表的分区

alter table xx表 drop if exists partition (分区名='条件');

16.查看hive表的分区信息

show partitions xx表;

17.添加表的分区

alter table my_partition_test_table  add partition (分区名='值',分区名='值');--分区名要为表已经有的分区

18Hive解析json视图中的属性

select
  from ccs_event ta    ----css_event
       lateral view json_tuple(ta.data_json,'sessionHash') tb as sessionHash

19.explain关键字可以更加详细的列举出代码的执行过程

 explain select * from 表名 where etl_dt='20181128';

20.把hive执行的结果保存到本地(做etl的时候真不怎么用)

不启动hive时

(1)在bash中直接通过hive -e命令,并用 > 输出把执行结果输出到指定文件

hive -e "select * from 表名" > /tmp/result.txt(Linux路径)

(2)在bash中直接通过hive -f命令,执行sql文件中一条或者多的sql语句。并用 > 输出流把执行结果输出到制定文件

hive -f /tmp/test/aa.sql(a.sql里面是要执行的sql语句,最好写全路径) > result.txt

(3)在bash中直接通过hive -S命令,静默执行,与oracle中sqlplus -S 相同的作用

hive -S  -e "select * from test.wai1"

启动hive

(3)在hive中输入hive-sql语句,通过使用INSERT OVERWRITE LOCAL DIRECTORY把结果保存到本地或者HDFS文件系统

insert overwrite local directory "/tmp/result "  select * from 表名;
insert overwrite directory 'hdfs://主机名:端口名/user/hdfs路径' select * from 表名;

注意这样产生的文件为两个

21启动hive后执行linux命令

hive>!Linux命令;

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值