我是靠谱客的博主 爱笑小海豚,这篇文章主要介绍Hive语法小释阅读本文你可以获取:,现在分享给大家,希望可以做个参考。

阅读本文你可以获取:

 1.数据库的查询

 2.hive表的基本操作(建表三种常用方式、删除表、修改表、加载数据、内外表转换、添加分区、复制数据)

 3.SQL到HiveQL的的一些不同点

1.   基本操作

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
--查询数据库 Show databases; --筛选数据库: Show databases like 'h.*'; --修改数据库指定位置: Create database test_database Location '/my/file'; --显示数据库文件所在目录语句: Describe database test_database; --删除数据库: Drop database if exist test_database cascade;--hive 不允许用户删除一个包含有表的数据库,所以可在最后加cascade即级联删除。 --显示具体表的信息: Describe extended/formatted(扩展/格式化) test_database.test;--实际情况用formatted更多,可读性更强 --查看表分区: Show partitions table ; Show partitions table partitons(column ='分区名');--查看指定分区 --基本表操作 --建表语句示例: --hive创建表主要有三种方式, --第一种直接使用create table命令, --第二种使用create table ... as select...(会产生数据)。 --第三种使用create table tablename like exist_tablename命令。不会拷贝数据 use rpt; drop table if exists test; create external table test(--external建立外部表,如果建内部表则不加此关键字 testid string comment '分流id', group_id string comment '用户组id' )comment '测试表' partitioned by (ds string) stored as orc; --表添加注释: Create database test_database comment 'This is a test database!'; --删除表: DROP TABLE test1; --修改表结构: DESC student1;--查看表结构 ALTER TABLE student1 ADD COLUMNS (address STRING,grade STRING); --修改表名: ALTER TABLE student1 RENAME TO student3; --创建和已知表相同结构的表: CREATE TABLE copy_student1 LIKE student1; --导入外部文件数据: --加载数据到student1表中 LOAD DATA LOCAL INPATH '/home/hadoop/data/student1.txt' INTO TABLE student1; --加载hdfs中的文件: LOAD DATA INPATH '/user/hive/student1.txt' INTO TABLE copy_student1; --修改列名: alter table tablename change column c_Old c_New int comment 'XXXXXX' after 某列;--某列之后 来自 <https://blog.csdn.net/scgaliguodong123_/article/details/46941419> --增加列: Alter table tablename add columns ( Id int comment'id', Name string comment 'name' ) --删除或者替换列:(会替换表中所有列) Alter table tablename replace columns ( Id int, Name string ); --内部表转外部表 alter table tablename set TBLPROPERTIES ('EXTERNAL' = 'TRUE'); --外部表转内部表 alter table table_name set TBLPROPERTIES('EXTERNAL'='FALSE'); 来自 <https://blog.csdn.net/scgaliguodong123_/article/details/46941419> --添加/删除分区语句 Alter table test add partition(ds='2018-06-02')--已有分区的表 alter table test drop if exist partition(ds='2018-06-01') ;--删除表分区 --复制表数据: INSERT OVERWRITE TABLE copy_student2 SELECT * FROM student1; --多表同时复制: hive> FROM student1 > INSERT OVERWRITE TABLE copy_student3 > SELECT * > INSERT OVERWRITE TABLE copy_student4 > SELECT *;

2.SQLHiveQL的转换

来自 <https://blog.csdn.net/hguisu/article/details/7256833> 

1、Hive不支持等值连接 

 SQL中对两表内联可以写成:

复制代码
1
2
3
4
5
6
select * from dual a,dual b where a.key = b.key; Hive中应为 select * from dual a join dual b on a.key = b.key; 而不是传统的格式: SELECT t1.a1 as c1, t2.b1 as c2FROM t1, t2 WHERE t1.a2 = t2.b2

2、分号字符

复制代码
1
2
3
4
5
6
分号是SQL语句结束标记,在HiveQL中也是,但是在HiveQL中,对分号的识别没有那么智慧,例如: select concat(key,concat(';',key)) from dual; 但HiveQL在解析语句时提示: FAILED: Parse Error: line 0:-1 mismatched input '<EOF>' expecting ) in function specification 解决的办法是,使用分号的八进制的ASCII码进行转义,那么上述语句应写成: select concat(key,concat('73',key)) from dual;

3、IS [NOT] NULL

SQL中null代表空值, 值得警惕的是, 在HiveQL中String类型的字段若是空(empty)字符串, 即长度为0, 那么对它进行IS NULL的判断结果是False.

4、Hive不支持将数据插入现有的表或分区中,仅支持覆盖重写整个表,示例如下:

复制代码
1
2
INSERT OVERWRITE TABLE t1 SELECT * FROM t2;

5.hive不支持INSERT INTO, UPDATE, DELETE操作

其中 INSERT INTO syntax is only available starting in version 0.8。

INSERT INTO就是在表或分区中追加数据。

 

转载于:https://www.cnblogs.com/littlewu/p/9242049.html

最后

以上就是爱笑小海豚最近收集整理的关于Hive语法小释阅读本文你可以获取:的全部内容,更多相关Hive语法小释阅读本文你可以获取内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(91)

评论列表共有 0 条评论

立即
投稿
返回
顶部