hive–数据仓库常用命令
一、Hive实际操作
(1)启动hive
bin/hive
(2)显示数据库
hive> show databases;
(3)使用default数据库
hive> use default;
(4)显示default数据库中的表
hive> show tables;
(5)删除已创建的student表
hive> drop table student;
(6)创建student表, 并声明文件分隔符’\t’
hive> create table student(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
(7)加载/opt/module/datas/student.txt 文件到student数据库表中。
hive> load data local inpath '/opt/module/datas/student.txt' into table student;
(8)Hive查询结果
hive> select * from student;
OK
1001 zhangshan
1002 lishi
1003 zhaoliu
Time taken: 0.266 seconds, Fetched: 3 row(s)
二、 Hive常用交互命令
1.“-e”不进入hive的交互窗口执行sql语句
bin/hive -e "select id from student;"
2.2.“-f”执行脚本中sql语句
(1)在/opt/module/datas目录下创建hivef.sql文件
[hadoop@hadoop003 datas]$ touch hivef.sql
文件中写入正确的sql语句
select *from student;
(2)执行文件中的sql语句
[hadoop@hadoop003 hive]$ bin/hive -f /opt/module/datas/hivef.sql
(3)执行文件中的sql语句并将结果写入文件中
[hadoop@hadoop003 hive]$ bin/hive -f /opt/module/datas/hivef.sql > /opt/module/datas/hive_result.txt
三、创建数据库
1)创建一个数据库,数据库在HDFS上的默认存储路径是/user/hive/warehouse/*.db。
hive (default)> create database db_hive;
2)避免要创建的数据库已经存在错误,增加if not exists判断。(标准写法)
hive (default)> create database db_hive;
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Database db_hive already exists
hive (default)> create database if not exists db_hive;
3)创建一个数据库,指定数据库在HDFS上存放的位置。
hive (default)> create database db_hive2 location '/db_hive2.db';
三、查询数据库
一、显示数据库
1.显示数据库
hive> show databases;
2.过滤显示查询的数据库
hive> show databases like 'db_hive*';
OK
db_hive
db_hive_1
二、查看数据库详情
1.显示数据库信息
hive> desc database db_hive;
OK
db_hive hdfs://hadoop003:9000/user/hive/warehouse/db_hive.db bigdataUSER
2.切换当前数据库
hive (default)> use db_hive;
三、删除数据库
1.删除空数据库
hive>drop database db_hive2;
2.如果删除的数据库不存在,最好采用 if exists判断数据库是否存在
hive> drop database db_hive;
FAILED: SemanticException [Error 10072]: Database does not exist: db_hive
hive> drop database if exists db_hive2;
3.如果数据库不为空,可以采用cascade命令,强制删除
hive> drop database db_hive;
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. InvalidOperationException(message:Database db_hive is not empty. One or more tables exist.)
hive> drop database db_hive cascade;
四、管理表
1、根据查询结果创建表(查询的结果会添加到新创建的表中)
create table student3 as select id, name from student;
2、根据已经存在的表结构创建表
create table student4 like student;
3、查询表的类型
hive (default)> desc formatted student2;
Table Type: MANAGED_TABLE
五、修改表
一、重命名表
1.语法
ALTER TABLE table_name RENAME TO new_table_name;
2.实操案例
hive (default)> alter table dept_partition2 rename to dept_partition3;
二、增加/修改列信息
1.语法
更新列
ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment]
增加列
ALTER TABLE table_name ADD COLUMNS (col_name data_type [COMMENT col_comment], ...)
eg:
(1)查询表结构
hive> desc dept_partition;
(2)添加列
hive (default)> alter table dept_partition add columns(deptdesc string);
(3)查询表结构
hive> desc dept_partition;
(4)更新列
hive (default)> alter table dept_partition change column deptdesc desc int;
(5)查询表结构
hive> desc dept_partition;
六、向表中装载数据(Load)
语法:
1、
`hive> load data [local] inpath '/opt/module/datas/student.txt' [overwrite] into table student`
2、通过查询语句向表中插入数据(Insert)
1.创建一张表
hive (default)> create table student(id int, name string) row format delimited fields terminated by '\t';
2.基本插入数据
hive (default)> insert into table student values(1,'wangwang');
六、数据导出
1.将查询的结果导出到本地
hive (default)> insert overwrite local directory '/opt/module/datas/export/student'
select * from student;
2.将查询的结果格式化导出到本地
hive(default)>insert overwrite local directory '/opt/module/datas/export/student1'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' select * from student;
3.将查询的结果导出到HDFS上(没有local)
hive (default)> insert overwrite directory '/user/bigdata/student2'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
select * from student;
4、Hadoop命令导出到本地
hive (default)> dfs -get /user/hive/warehouse/student/month=201809/student.txt
/opt/module/datas/export/student3.txt;
5、Hive Shell 命令导出到本地
基本语法:(hive -f/-e 执行语句或者脚本 > file)
[hadoop@hadoop003 hive]$ bin/hive -e 'select * from default.student;' /opt/module/datas/export/student4.txt;