mysql分库分区导入hdfs,Hive学习-Hive基本操作(建库、建表、分区表、写数据)

hive简单认识Hive是建立在HDFS之上的数据仓库,所以Hive的数据全部存储在HDFS上。

Hive的数据分为两部分,一部分是存在HDFS上的具体数据,一部分是描述这些具体数据的元数据信息,一般Hive的元数据存在MySQL上。

Hive是类SQL语法的数据查询、计算、分析工具,执行引擎默认的是MapReduce,可以设置为Spark、Tez。

Hive分内部表和外部表,外部表在建表的同时指定一个指向实际数据的路径(LOCATION),Hive 创建内部表时,会将数据移动到数据仓库指向的路径;若创建外部表,仅记录数据所在的路径,不对数据的位置做任何改变。在删除表的时候,内部表的元数据和数 据会被一起删除,而外部表只删除元数据,不删除数据。

1、进入hive命令行$cd  $HIVE_HOME/bin$./hive

注意:CRT软件进入到hive命令行模式的时候默认不支持回退修改命令,需要修改CRT的会话选项:

492574cf7c48

492574cf7c48

2、创建数据库hive> create  database  IF NOT EXISTS  test  COMMENT '测试数据库'  LOCATION '/ruozedata' WITH DBPROPERTIES ('creater'='liuzd','date'='20180605');IF NOT EXISTS:如果不存在则创建

COMMENT:添加注释

LOCATION:指定hdfs存放路径

WITH DBPROPERTIES:添加自定义属性

3、查询数据库信息hive> desc database  extended test;

492574cf7c48

4、删除数据库hive> drop database test CASCADE;cascade:级联删除库下的表,默认会提示报错(值为RESTRICT),生产不建议使用

5、修改数据库hive> alter database test set location '/ruozedata';

492574cf7c48注意:根据hive版本不同,hive支持的SQL也不一样,官网内容如下:

492574cf7c48

6、数据库切换hive> use test;

492574cf7c48

7、常用数据类型int:整型

bigint:长整型

float:浮点型

double:双精度

string:字符串

8、创建表hive> CREATE EXTERNAL TABLE ruozedata_person

> (id int comment 'this is id', name string comment 'this id name' )

> comment 'this is ruozedata_person'

> ROW FORMAT DELIMITED

> FIELDS TERMINATED BY '\t'

> location  '/user/hive/warehouse/test.db/emp';EXTERNAL:创建外部表的关键字,默认是内部表

comment:添加注释,跟在字段后就是字段的注释,跟在表后就是表的注释

ROW FORMAT DELIMITED  FIELDS TERMINATED BY '\t':指定加载数据的列分隔符为制表符

location:指定表数据存放路径

like方式创建表,复制表结构hive> CREATE external table ruozedata_emp2 like emp location '/user/hive/warehouse/test.db/emp';

select方式创建表,可以顺带复制数据hive> create table emp2 as select * from emp;

9、查看表结构hive> desc formatted ruozedata_person;

492574cf7c48

10、修改表

修改表名:hive> alter table ruozedata_person rename to person;

hive> show tables

修改字段:hive> CREATE TABLE test_change (a int, b int, c int);

// First change column a's name to a1.

hive> ALTER TABLE test_change CHANGE a a1 INT;

// Next change column a1's name to a2, its data type to string, and put it after column b.

hive> ALTER TABLE test_change CHANGE a1 a2 STRING AFTER b;

// The new table's structure is:  b int, a2 string, c int.

// Then change column c's name to c1, and put it as the first column.

hive> ALTER TABLE test_change CHANGE c c1 INT FIRST;

// The new table's structure is:  c1 int, b int, a2 string.

// Add a comment to column a1

hive> ALTER TABLE test_change CHANGE a1 a1 INT COMMENT 'this is column a1';

11、删除表hive> drop table person;

12、加载数据hive> LOAD DATA LOCAL INPATH '/home/hadoop/emp.txt' OVERWRITE INTO TABLE emp;LOCAL:指定文件为本地文件,默认为HDFS文件系统

OVERWRITE:覆盖写入到表中,默认追加

13、insert方式插入数据(追加)hive> insert into emp2 select * from emp;

492574cf7c48

14、insert方式插入数据(覆盖)hive> insert overwrite table emp2 select * from emp;

492574cf7c48

注意:emp2和emp结构要一致,字段顺序要一致。

15、insert  into valueshive> insert into a(id,name) values(1,'ruoze');

注意:insert into  values的方式不是直接写数据到原表上,而是新建临时表存储数据,然后把数据cp一份到目标表的路径里。

16、数据查询

group by

求每个部门的平均工资大于2000的部门hive> select deptno, avg(salary)  from emp group by deptno having avg(salary)>2000;

case when thenhive> select ename,salary,

> case

> when salary>1 and salary<=1000 then 'lower'

> when salary>1000 and salary<=2000 then 'middle'

> when salary>2000 and salary<=4000 then 'high'

> else 'highest'

> end

> from emp;

492574cf7c48

join查询

inner join==join

492574cf7c48

full join

492574cf7c48

left join

492574cf7c48

right join

492574cf7c48

17、分区表(静态分区)hive> create table emp_patition(

> empno                  int,

> ename                  string  ,

> job                    string ,

> mgr                    int      ,

> hiredate                string    ,

> salary                  double    ,

> comm                    double

> )

> partitioned by (deptno int)

> row format delimited fields terminated by '/t';

hive> alter table emp_patition rename to emp_partition;

insert into select方式插入分区表数据

492574cf7c48

查看分区表HDFS存储

492574cf7c48

insert  overwrite table方式插入数据

492574cf7c48

492574cf7c48

18、分区表(动态分区)hive> create table emp_patition2(

> empno                   int,

> ename                   string  ,

> job                     string ,

> mgr                     int      ,

> hiredate                string    ,

> salary                  double    ,

> comm                    double

> )

> partitioned by (deptno int)

> row format delimited fields terminated by '/t';

hive> alter table emp_patition rename to emp_partition;

注意:insert into=insert into table,动态分区需要设置参数set hive.exec.dynamic.partition.mode=nonstrict,否则插入报错。动态分区和静态分区没有任何区别,除了以上设置和数据的录入方式以外。

492574cf7c48

492574cf7c48

注意:动态分区要求分区字段的位置要放在select最后一个,如果是多级分区则按照顺序放到最后,不要求名称一致。

492574cf7c48

19、多级分区表hive> create table emp_mulit_partition(

> empno                  int,

> ename                  string)

> partitioned  by (deptno int,job string)

> row format delimited fields terminated by '/t';

492574cf7c48

492574cf7c48

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值