hive添加表
hive的命令就是mysql的命令.
创建数据库
create database hive;
![](https://img.haomeiwen.com/i3648853/82b2ccb34dda50f9.png)
创建表
create table students (id string, name string, gender string, age int, score string)
row format delimited -- 表示以行分隔每条数据
fields terminated by ','; -- 字段使用逗号分隔.
把数据文件put到表在hdfs中的目录下
![](https://img.haomeiwen.com/i3648853/059b1a8c345967c6.png)
查询一下
select max(age) from students;
此时hivq会启动一个mapreduce任务执行结果.
![](https://img.haomeiwen.com/i3648853/bc51111d311ab1ee.png)
hive-server
把hive发布为服务, 可远程访问
启动hive-server
zb@ubuntu:~/programs/hive-1.2.2$ bin/hiveserver2
连接hiveserver
bin/beeline
beeline> !connect jdbc:hive2://localhost:10000
默认用户名是 hostname 密码为空
创建外部表
建表
create external table ext_students (id string, name string, gender string, age int, score string)
row format delimited
fields terminated by ','
location '/hive/students';
load 加载数据
0: jdbc:hive2://ubuntu:10000> load data local inpath '/home/zb/tmp/students.txt' into table ext_students;
创建分区表
不同的分区数据放在不同的目录中.
建表
0: jdbc:hive2://ubuntu:10000> create table globalstudents (id string, name string, gender string, age int, score string)
0: jdbc:hive2://ubuntu:10000> partitioned by (country string)
0: jdbc:hive2://ubuntu:10000> row format delimited
0: jdbc:hive2://ubuntu:10000> fields terminated by ',';
load 数据
0: jdbc:hive2://ubuntu:10000> load data local inpath '/home/zb/tmp/students.txt' into table globalstudents partition(country='china');
INFO : Loading data to table default.globalstudents partition (country=china) from file:/home/zb/tmp/students.txt
INFO : Partition default.globalstudents{country=china} stats: [numFiles=1, numRows=0, totalSize=526, rawDataSize=0]
No rows affected (1.127 seconds)
0: jdbc:hive2://ubuntu:10000>
0: jdbc:hive2://ubuntu:10000> load data local inpath '/home/zb/tmp/students.txt' into table globalstudents partition(country='japan');
INFO : Loading data to table default.globalstudents partition (country=japan) from file:/home/zb/tmp/students.txt
INFO : Partition default.globalstudents{country=japan} stats: [numFiles=1, numRows=0, totalSize=526, rawDataSize=0]
No rows affected (0.819 seconds)
![](https://img.haomeiwen.com/i3648853/e8348d3b86b018db.png)
分区查询语句
0: jdbc:hive2://ubuntu:10000> select * from globalstudents where country='china';
+--------------------+----------------------+------------------------+---------------------+-----------------------+-------------------------+--+
| globalstudents.id | globalstudents.name | globalstudents.gender | globalstudents.age | globalstudents.score | globalstudents.country |
+--------------------+----------------------+------------------------+---------------------+-----------------------+-------------------------+--+
| 95001 | 李勇 | 男 | 20 | CS | china |
| 95002 | 刘晨 | 女 | 19 | IS | china |
| 95003 | 王敏 | 女 | 22 | MA | china |
| 95004 | 张立 | 男 | 19 | IS | china |
| 95005 | 刘刚 | 男 | 18 | MA | china |
| 95006 | 孙庆 | 男 | 23 | CS | china |
| 95007 | 易思玲 | 女 | 19 | MA | china |
| 95008 | 李娜 | 女 | 18 | CS | china |
| 95009 | 梦圆圆 | 女 | 18 | MA | china |
| 95010 | 孔小涛 | 男 | 19 | CS | china |
| 95011 | 包小柏 | 男 | 18 | MA | china |
| 95012 | 孙花 | 女 | 20 | CS | china |
| 95013 | 冯伟 | 男 | 21 | CS | china |
| 95014 | 王小丽 | 女 | 19 | CS | china |
| 95015 | 王君 | 男 | 18 | MA | china |
| 95016 | 钱国 | 男 | 21 | MA | china |
| 95017 | 王风娟 | 女 | 18 | IS | china |
| 95018 | 王一 | 女 | 19 | IS | china |
| 95019 | 邢小丽 | 女 | 19 | IS | china |
| 95020 | 赵钱 | 男 | 21 | IS | china |
| 95021 | 周二 | 男 | 17 | MA | china |
| 95022 | 郑明 | 男 | 20 | MA | china |
+--------------------+----------------------+------------------------+---------------------+-----------------------+-------------------------+--+
alter table 修改分区
增加分区
0: jdbc:hive2://ubuntu:10000> alter table globalstudents add partition (country='america');
No rows affected (0.3 seconds)
删除分区
0: jdbc:hive2://ubuntu:10000> alter table globalstudents drop partition (country='america');
INFO : Dropped the partition country=america
No rows affected (0.412 seconds)
0: jdbc:hive2://ubuntu:10000> show partitions globalstudents;
+----------------+--+
| partition |
+----------------+--+
| country=china |
| country=japan |
+----------------+--+
2 rows selected (0.203 seconds)
网友评论