美文网首页
hive 基本操作

hive 基本操作

作者: 持而盈 | 来源:发表于2017-09-24 16:28 被阅读9次

hive添加表

hive的命令就是mysql的命令.

创建数据库

 create database hive;
hive创建的db

创建表

create table students (id string, name string, gender string, age int, score string)
row format delimited   -- 表示以行分隔每条数据
fields terminated by ',';   -- 字段使用逗号分隔.

把数据文件put到表在hdfs中的目录下

创建表&上传数据

查询一下

select max(age) from students;

此时hivq会启动一个mapreduce任务执行结果.

查询hive

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)
分区表目录结构

分区查询语句

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)

相关文章

  • HIVE

    基本操作 hive进入hive模式 exit; 退出hive模式 展示正则匹配表名hive> use ad_s...

  • Hive之同比环比的计算

    Hive系列文章 Hive表的基本操作[http://www.ikeguang.com/?p=1657] Hive...

  • Hive sql常见操作

    基本sql操作 hive表操作 分区操作 Hive内置函数 (1)数学函数 常用的数学函数都有:round、flo...

  • Hive 基本操作

    准备工作 1.检查各项服务是否已正常启动 [x] hdfs启动【start-dfs.sh】 [x] yarn启动【...

  • Hive 基本操作

    数据库基本操作 ( 和MySQL脚本相似 ): 创建删除库操作 创建删除表操作 hive 特点 Hive不支持修改...

  • Hive基本操作

    在所有操作前先启动hdfs、yarn、historyserver。 hive操作 启动hivebin/hive 创...

  • Hive基本操作

    参考:hive 表的常用操作Hive基本操作 1 Thrift Server Thrift 支持多种语言之间的RP...

  • Hive基本操作

    1.使用SQL文件创建一张表: hive -f create_table 2.将外部数据加载到一张表里面: LOA...

  • hive 基本操作

    hive添加表 hive的命令就是mysql的命令. 创建数据库 创建表 把数据文件put到表在hdfs中的目录下...

  • Hive基本操作

    一、DDL 1.1 创建内部表 mytable 1.2 创建外部表 pageview 1.3 创建分区表 invi...

网友评论

      本文标题:hive 基本操作

      本文链接:https://www.haomeiwen.com/subject/pggkextx.html