Hive的交互及初步使用

小节

overwrite into 覆盖写入
into 追加
insert overwrite 固定于法 例如: 分桶,多模式插入,导出数据

Hive JDBC服务

前提:假如有三台虚拟机,131,132,133


假设在132节点启动hive
cd  /export/servers/hive-1.1.0-cdh5.14.0
bin/hive --service hiveserver2(前台启动)

cd  /export/servers/hive-1.1.0-cdh5.14.0
nohup bin/hive --service hiveserver2  &(后台启动)

注意:前台和后台区别是  前台的可视化窗口关闭(例如:IDEA),整个服务会关掉.后台启动不会,
在另外两台中任意启动beeline
bin/beeline(进入beeline)
beeline> !connect jdbc:hive2://192.168.10.132:10000

Hive交互shell

cd /export/servers/hive-1.1.0-cdh5.14.0
bin/hive(启动hive)

查看所有的数据库
hive (default)> show databases;

创建一个数据库
hive (default)> create database myhive;
使用该数据库并创建数据库表
hive (default)> use myhive;
hive (myhive)> create table test1(id int,name string);

使用可视化窗口与hive交互

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

Hive命令的交互方式

1.使用hive -e 执行sql语句
hive -e "use myhive;select * from test1;"
2.使用hive 使用 –f  参数通过指定文本文件来执行hql的语句
vim hive.sh
use myhive;select * from test1;(在hive.sh中书写)
hive -f hive.sh(在bin下执行)

Hive基本操作

创建数据库和创建数据库

创建数据库:create database if not exists 库名;
使用数据库:use 库名;
数据库存放位置:默认:/user/hive/warehouse
							手动设置:create database 库名 location "路径";

查看数据库详细信息

查看数据库基本信息:desc database  库名
察看数据库的详细数据:desc extend database  库名

删除数据库

drop  database  库名;(库里是空的,否则不能删除)
drop database  库名 cascade;(强制删除数据库,包含数据库下面的表一起删除)

创建数据库表操作

create 【external 】(外部表还是内部表)table 	表名

创建表并指定字段之间的分隔符

例如:create  table if not exists stu2(id int ,name string)
 row format delimited fields terminated by '\t' ;
 insert into stu2 values (1,"zhangsan");
 insert into stu2 values (2,"lisi");
 insert into stu2 values (3,"wangwu");

根据查询结果创建表

create table stu3 as select * from stu2;(将查询的结果插入到stu3中)

查看数据表的结构

desc formatted  stu2;

创建外部表

create external table techer (t_id string,t_name string) row format delimited fields terminated by '\t';

从本地文件系统向表中加载数据

load data local inpath '本地中文件的路径' into table student;(从Linux本地加载)
load data inpath 'hdfs中文件的路径' into table student;(从hdfs加载)

加载数据并覆盖已有数据

load data local inpath ‘本地中文件的路径' overwrite  into table student;

根据已经存在的表来创建表结构

create table stu4 like stu2;

分区表

创建分区表语法

create table score(s_id string,c_id string, s_score int) partitioned by (month string) row format delimited fields 
terminated by '\t';

加载数据到分区表中

load data local inpath '/export/servers/hivedatas/score.csv' into table score partition (month='201806');

创建一个表带多个分区

create table score2 (s_id string,c_id string, s_score int) partitioned by (year string,month string,day string) row format 
delimited fields terminated by '\t';

加载数据到一个多分区的表中去

load data local inpath '/export/servers/hivedatas/score.csv' into table score2 partition
(year='2018',month='06',day='01');

多分区联合查询使用union all来实现

select * from score where month = '201806' union all select * from score where month = '201806';

查看分区

desc partition 表名

添加一个分区

alter table score add partition(month='201805');

同时添加多个分区

alter table score add partition(month='201804') partition(month = '201803');
注意:添加分区之后就可以在hdfs文件系统当中看到表下面多了一个文件夹

删除分区

alter table score drop partition(month = '201806');

自动修复表

hdfs dfs -mkdir -p /scoredatas/month=201806(创建文件夹)
hdfs dfs -put score.csv /scoredatas/month=201806/(将数据放在分区的文件夹下)
msck repair table  表名;(在hive中)

手动修复表

hdfs dfs -mkdir -p /scoredatas/month=201806(创建文件夹)
hdfs dfs -put score.csv /scoredatas/month=201806/(将数据放在分区的文件夹下)
alter table score4 add partition(month=‘201805’);*(在hive中)

分桶表

开启hive的桶表功能

set hive.enforce.bucketing=true;

设置reduce的个数

set mapreduce.job.reduces=  n(自己设置数量);

创建普通表:

create table course_common (c_id string,c_name string,t_id string) row format delimited fields terminated by ‘\t’;

普通表中加载数据

load data local inpath ‘Linux中文件路径’ into table course_common;

创建桶表

create table course (c_id string,c_name string,t_id string) clustered by(c_id) into 3 buckets row format delimited fields 		
terminated by '\t';

通过insert overwrite给桶表中加载数据

insert overwrite table course select * from course_common cluster by(c_id);

外部表和内部表的区别

外部表因为是指定其他的hdfs路径的数据加载到表当中来,所以hive表会认为自己不完全独占这份数据,
所以删除hive表的时候,数据仍然存放在hdfs当中,不会删掉
内部表将hdfs文本数据移动到表中,如果表被删除,hdfs的数据和表的元数据都会消失

hive表中加载数据

直接向分区表中插入数据

insert into table score3 partition(month ='201807') values ('001','002','100');

通过load方式加载数据

load data local inpath '/export/servers/hivedatas/score.csv' overwrite into table score partition(month='201806');

通过查询方式加载数据

insert overwrite table score4 partition(month = '201806') select s_id,c_id,s_score from score;

多插入模式

create table score_first( s_id string,c_id  string) partitioned by (year string,month string,day string) row format delimited 
fields terminated by ',' ;
create table score_second(c_id string,s_score int) partitioned by (year string,month string,day string) row format 
delimited fields terminated by ',' ;

from score2
insert overwrite table score_first partition (year=2001,month=8,day=21) select s_id,c_id
insert overwrite table score_second partition(year=2000,month=1,day=1)  select c_id,s_score;

创建表时通过location指定加载表的存放路径

create external table score6 (s_id string,c_id string,s_score int) row format delimited fields terminated by '\t' location 
'/myscore6';

数据的导入

1.方式一

create table techer2 like techer;
import table techer2 from '/export/techer';

hive表中数据的导出

1)将查询的结果导出到本地

insert overwrite local directory 'Linux路径' select * from score;

2)将查询的结果格式化导出到本地

insert overwrite local directory '/export/servers/exporthive' row format delimited fields terminated by 	
'\t'  collection items  terminated by '#' select * from student;

3)将查询的结果导出到HDFS上(没有local)

insert overwrite directory '/export/servers/exporthive' row format delimited fields terminated by '\t' 
collection items terminated by '#' select * from score;

4) Hadoop命令导出到本地

hdfs dfs -get /export/servers/exporthive/000000_0 /export/servers/exporthive/local.txt;

5)export导出到HDFS上

export table score to ‘/export/exporthive/score’;

6) sqoop导出

7)清空表数据

truncate table score5;

猜你喜欢

转载自blog.csdn.net/qq_45769990/article/details/109731868