Hive 常用命令和语句
示例数据库为 db_hive
1、 创建表 create-table.sql
create table if not exists db_hive.tb_user
(
id int,
username string comment '用户名',
age int comment '年龄',
address string comment '地址'
)
comment '用户表'
row format delimited fields terminated by ','
stored as textfile
location '/user/hive/warehouse/db_hive.db/db_user'
2、执行创建表
hive -f 'create-table.sql'
3、加载数据到 tb_user 表中
数据文件 /root/files/tb_user.txt
1001,Logan,16,shenzhen
1002,Herry potter,12,Magic school
1003,孙悟空,500,花果山
Hive交互式命令行执行命令 load data local inpath '/root/files/tb_user.txt' into table db_hive.tb_user;
如下所示:
hive (db_hive)> load data local inpath '/root/files/tb_user.txt' into table db_hive.tb_user;
如果要覆盖旧数据,可以加 overwrite,如下所示
hive (db_hive)> load data local inpath '/root/files/tb_user.txt' overwrite into table db_hive.tb_user;
4、查询数据
hive -e "select id,username from db_hive.tb_user"
5、根据已有表创建只有部分字段的子表
create table if not exists db_hive.tb_user_sub
as
select id,username from db_hive.tb_user;
6、 like 创建表
create table if not exists db_hive.tb_user_like like db_hive.tb_user;
插入数据
insert into table db_hive.tb_user_like select * from db_hive.tb_user;
7、重命名表
alter table tb_user_like rename to tb_user_rename ;
8、 创建外部表,删除时只删除元数据,不会删除表数据
create external table if not exists db_hive.tb_ext(id string);
9、创建分区表
create table if not exists db_hive.tb_logs(
ip string,
text string,
log_time string
)
partitioned by (month string)
row format delimited fields terminated by "\t";
数据文件 /root/files/tb_logs.txt
192.168.32.100 login 20190429072650
192.168.32.100 order 20190429072730
192.168.32.101 browse 20190429072812
载入数据
load data local inpath '/root/files/tb_logs.txt' into table db_hive.tb_logs partition (month = '')
查询数据
select ip,text,log_time from tb_logs where month = '';
10、手工创建分区数据及修复分区表
创建分区目录
hdfs dfs -mkdir -p /user/hive/warehouse/db_hive.db/tb_logs/month=201905
上传数据文件到分区目录下
hdfs dfs -put /root/files/tb_logs.txt /user/hive/warehouse/db_hive.db/tb_logs/month=201905
此时执行查询
select count(distinct ip) from db_hive.tb_logs where month = '';
查询结果为0。
【原因】:数据并未添加到分区中,查看配置的MySQL元数据信息
mysql> use hive_metastore;
mysql> select * from PARTITIONS;
示例配置的Hive元数据存放为MySQL数据库中的 hive_metastore 数据库
查询分区表 PARTITIONS 中的数据,发现只有一条记录,如下所示:
+---------+-------------+------------------+--------------+-------+--------+
| PART_ID | CREATE_TIME | LAST_ACCESS_TIME | PART_NAME | SD_ID | TBL_ID |
+---------+-------------+------------------+--------------+-------+--------+
| 1 | 1556494255 | 0 | month=201904 | 29 | 28 |
+---------+-------------+------------------+--------------+-------+--------+
【修复方法一】直接执行修复命令
msck repair table tb_logs
此时分区表中的数据如下:
+---------+-------------+------------------+--------------+-------+--------+
| PART_ID | CREATE_TIME | LAST_ACCESS_TIME | PART_NAME | SD_ID | TBL_ID |
+---------+-------------+------------------+--------------+-------+--------+
| 1 | 1556494255 | 0 | month=201904 | 29 | 28 |
| 2 | 1556495227 | 0 | month=201905 | 30 | 28 |
+---------+-------------+------------------+--------------+-------+--------+
执行查询命令
select count(distinct ip) from db_hive.tb_logs where month = '';
返回结果为2,数据已正常加入分区。
【修复方法二】 使用增加分区命令
操作步骤:创建新分区目录并上传数据文件,命令如下:
hive (db_hive)> dfs -mkdir -p /user/hive/warehouse/db_hive.db/tb_logs/month=201906;
hive (db_hive)> dfs -put /root/files/tb_logs.txt /user/hive/warehouse/db_hive.db/tb_logs/month=201906;
执行增加分区命令
alter table tb_logs add partition(month = '');
查询数据,测试结果正常。
此时元数据分区表中数据如下:
+---------+-------------+------------------+--------------+-------+--------+
| PART_ID | CREATE_TIME | LAST_ACCESS_TIME | PART_NAME | SD_ID | TBL_ID |
+---------+-------------+------------------+--------------+-------+--------+
| 1 | 1556494255 | 0 | month=201904 | 29 | 28 |
| 2 | 1556495227 | 0 | month=201905 | 30 | 28 |
| 3 | 1556495635 | 0 | month=201906 | 31 | 28 |
+---------+-------------+------------------+--------------+-------+--------+
查看表分区命令
show partitions db_hive.tb_logs;
11、 导出表数据
export table db_hive.tb_logs to '/user/hive/warehouse/export/db_hive/tb_logs';
12、 导入表数据
创建表
create table tb_logs_like like tb_logs;
导入数据
import table tb_logs_like from '/user/hive/warehouse/export/db_hive/tb_logs';
13、导出数据到本地文件
insert overwrite local directory '/root/files/hive_out'
row format delimited fields terminated by '\t' collection items terminated by '\n'
select * from db_hive.tb_logs;
Hive 常用命令和语句
.
Hive 常用命令和语句的更多相关文章
- 大数据-Hive 常用命令
Hive 启动 ~$ hive 退出 hive>quit; --退出hive or hive> exit; --exit会影响之前的使用,所以需要下一句kill掉hadoop的进程 > ...
- MySQL常用命令和语句
1.常用SQL语句 1)常用函数/*type可取值为:MICROSECONDSECONDMINUTEHOURDAYWEEKMONTHQUARTERYEARSECOND_MICROSECONDMINUT ...
- HIVE常用命令之MSCK REPAIR TABLE
MSCK REPAIR TABLE命令主要是用来解决通过hdfs dfs -put或者hdfs api写入hive分区表的数据在hive中无法被查询到的问题.我们知道hive有个服务叫metastor ...
- Hive 常用命令
1.hive模糊搜索表 show tables like '*name*'; 2.查看表结构信息 desc formatted table_name; desc table_name; 3.查看分 ...
- MySQL常用命令与语句
目录 Shell命令 查看系统信息 查看系统变量 设置系统变量 数据库操作 查看表信息 修改表语句 操作表 操作索引 操作约束 操作列 查询常用语句 Shell命令 mysql -uroot -p12 ...
- Hive常用命令
本位为转载,原地址为:http://www.cnblogs.com/BlueBreeze/p/4232421.html #创建新表 hive> CREATE TABLE t_hive (a in ...
- 【mysql】工作中mysql常用命令及语句
1.查看mysql版本号 MySQL [release_test_oa]> select version(); +------------+ | version() | +----------- ...
- Hive记录-Hive常用命令操作
1.hive支持四种数据模型 • external table ---外部表:Hive中的外部表和表很类似,但是其数据不是放在自己表所属的目录中,而是存放到别处,这样的好处是如果你要删除这个外部表,该 ...
- Hive常用命令及作用
1-创建表 -- 内部表 create table aa(col1 string,col2 int) partitioned by(statdate int) ROW FORMAT DELIMITED ...
随机推荐
- JS之scrollTop、offsetHeight和offsetTop等属性用法详解和拖拽div
标题中的几个相关相关属性在网页中有这大量的应用,尤其是在运动框架中,但是由于有些属性相互之间的概念比较混杂或者浏览器兼容性问题,导致掌握起来比较有难度,下面就介绍一下相关属性的用法.先来看一张比较经典 ...
- 21-----BBS论坛
BBS论坛(二十一) 21.1.编辑轮播图功能完成 (1)cms_banners.html 把属性绑定到<tr>上面,方便找到各属性的值 <tbody> {% for bann ...
- java——数组栈 ArrayStack
栈的应用: undo操作-编辑器 系统调用栈-操作系统 括号匹配-编译器 以下是动态数组实现的数组栈: 定义动态数组: package Date_pacage; public class Array& ...
- oracle merge into与sqlserver merge into 比较
merge into: 在两个表之间,根据与源表联接的结果,对目标表执行插入.更新或删除操作. Oracle在9i引入了merge into命令,SQL Server 2008也引入merge int ...
- STL effectiv 条款7 复习 未完成
该条款很好的解决了 容器中存放指针释放的问题,for_each函数调用可以参考自己的前面的博客 为什么会有这个条款呢 很多时候 我们将new过的指针存放到容器中,我们有时候错误的认为 容器对象析构 ...
- maya 安装失败/出错/卸载 2018/2017/2016/2015/2013/2012
AUTO Uninstaller 更新下载地址 1.选择软件 2.选择版本 3.点击[开始卸载]
- Clouder Manager安装时出现please remove the following file: /etc/cloudera-scm-server/db.properties问题解决(图文详解)
问题详情 bigdata@ubuntucmbigdata1:/opt/softwares/cm-$ sudo ./cloudera-manager-installer.bin This install ...
- Flyweight_pattern--reference
http://en.wikipedia.org/wiki/Flyweight_pattern In computer programming, flyweight is a software desi ...
- PHPcms的安装步骤
http://www.phpcms.cn 制作良好习惯: 1.备份 2.随时保存 3.注释 在自己的PHP环境里 简历一个文件夹 比如命名为YiNong 访问安装地址,http://localhost ...
- Spring Boot2中Spring Security导致Eureka注册失败
将Spring Boot升级到2.0,Spring Cloud升级到Finchley.M8时,Eureka注册就报错了 Eureka Server配置: server.port=9011 spring ...