Hive操作官网
1. DML操作
2. DDL操作
基本命令
显示数据库
1. show databases
创建数据库
2. create database hive_test;
使用数据库
3. use hive_test;
创建表,行字段按照'\t'进行分割
4. create table student(id int , name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' ;
查看表
5. show tables;
查看表结构 等价于 DESCRIBE invites;
6. desc student ;
查看表详细信息,非格式化
7. desc extended student;
格式化查看表信息
8. desc formatted student ;
加载本地数据文件到hive_test表 追加到student表中
9. load data local inpath '/opt/data/studentinfo' into table hive_test.student ;
加载本地数据文件到hive_test库中student表中(复写)
10. load data local inpath '/opt/data/studentinfo' overwrite into table hive_test.student ;
查看hive sql可以用到的函数
11. show functions;
查看 函数的描述
12. desc function upper
查看函数的描述(包括使用案例)
13. desc function extended upper ;
显示以s结尾的表名
14. SHOW TABLES '.*s';
从HDFS文件或者目录导入数据到表中
15. LOAD DATA INPATH '/user/myname/kv2.txt' OVERWRITE INTO TABLE invites PARTITION (ds='2008-08-15');
修改或删除表
student 表中增加sex 列
1. ALTER TABLE student ADD COLUMNS (sex INT);
student 表中增加address 列,并给列添加注释
2. ALTER TABLE student ADD COLUMNS (address INT COMMENT 'address a comment');
student 表中增加score 列,替换原有sex address 列,并给列添加注释
REPLACE COLUMNS 至替换table schema 不改变表数据
3. ALTER TABLE student REPLACE COLUMNS (id INT, name STRING, score INT COMMENT 'score replaces sex address ');
只保留表的第一列,删除其他列
4. ALTER TABLE invites REPLACE COLUMNS (foo INT COMMENT 'only keep the first column');
删除表
5. drop table invites ;
配置Hive查询结果显示表头信息
hive-site.xml文件中添加如下配置信息:
<property>
<name>hive.cli.print.header</name>
<value>true</value>
<description>Whether to print the names of the columns in query output.</description>
</property>
<property>
<name>hive.cli.print.current.db</name>
<value>true</value>
<description>Whether to include the current database in the Hive prompt.</description>
</property>