第2节 hive基本操作:11、hive当中的分桶表以及修改表删除表数据加载数据导出等
分桶表
将数据按照指定的字段进行分成多个桶中去,说白了就是将数据按照字段进行划分,可以将数据按照字段划分到多个文件当中去
开启hive的桶表功能
set hive.enforce.bucketing=true;
设置reduce的个数
set mapreduce.job.reduces=3;
创建桶表
create table course (c_id string,c_name string,t_id string) clustered by(c_id) into 3 buckets row format delimited fields terminated by '\t';
桶表的数据加载,由于桶表的数据加载通过hdfs dfs -put文件或者通过load data均不好使,只能通过insert overwrite
创建普通表,并通过insert overwrite的方式将普通表的数据通过查询的方式加载到桶表当中去
创建普通表:
create table course_common (c_id string,c_name string,t_id string) row format delimited fields terminated by '\t';
普通表中加载数据
load data local inpath '/export/servers/hivedatas/course.csv' into table course_common;
通过insert overwrite给桶表中加载数据
insert overwrite table course select * from course_common cluster by(c_id);
修改表
表重命名
基本语法:
alter table old_table_name rename to new_table_name;
把表score4修改成score5
alter table score4 rename to score5;
增加/修改列信息
(1)查询表结构
desc score5;
(2)添加列
alter table score5 add columns (mycol string, mysco string);
(3)查询表结构
desc score5;
(4)更新列
alter table score5 change column mysco mysconew int;
(5)查询表结构
desc score5;
删除表
drop table score5;
hive表中加载数据
直接向分区表中插入数据
create table score3 like score;
insert into table score3 partition(month ='201807') values ('001','002','100'); (一般不这么做,插入一条数据就会增加一个小文件)
通过查询插入数据(掌握)
通过load方式加载数据
load data local inpath '/export/servers/hivedatas/score.csv' overwrite into table score partition(month='201806');
通过查询方式加载数据
create table score4 like score;
insert overwrite table score4 partition(month = '201806') select s_id,c_id,s_score from score;
{注意:
1.此处不能使用select * from score,否则报错:Error: Error while compiling statement: FAILED: SemanticException [Error 10044]: Line 1:23 Cannot insert into target table because column number/types are different ''201902'': Table insclause-0 has 3 columns, but query has 4 columns. (state=42000,code=10044)
2.关键字overwrite 必须要有
}
多插入模式(用得不多)
常用于实际生产环境当中,将一张表拆开成两部分或者多部分
给score表加载数据
load data local inpath '/export/servers/hivedatas/score.csv' overwrite into table score partition(month='201806');
创建第一部分表:
create table score_first( s_id string,c_id string) partitioned by (month string) row format delimited fields terminated by '\t' ;
创建第二部分表:
create table score_second(c_id string,s_score int) partitioned by (month string) row format delimited fields terminated by '\t';
分别给第一部分与第二部分表加载数据
from score insert overwrite table score_first partition(month='201806') select s_id,c_id insert overwrite table score_second partition(month = '201806') select c_id,s_score;
查询语句中创建表并加载数据(as select)
将查询的结果保存到一张表当中去
create table score5 as select * from score;
创建表时通过location指定加载数据路径
1)创建表,并指定在hdfs上的位置
create external table score6 (s_id string,c_id string,s_score int) row format delimited fields terminated by '\t' location '/myscore6';
2)上传数据到hdfs上
hdfs dfs -mkdir -p /myscore6
hdfs dfs -put score.csv /myscore6;
3)查询数据
select * from score6;
export导出与import 导入 hive表数据(内部表操作)
create table techer2 like techer;
export table techer to '/export/techer';
import table techer2 from '/export/techer';
hive表中的数据导出(了解)
将hive表中的数据导出到其他任意目录,例如linux本地磁盘,例如hdfs,例如mysql等等
insert导出
1)将查询的结果导出到本地
insert overwrite local directory '/export/servers/exporthive' select * from score;
2)将查询的结果格式化导出到本地
insert overwrite local directory '/export/servers/exporthive' row format delimited fields terminated by '\t' collection items terminated by '#' select * from student;
3)将查询的结果导出到HDFS上(没有local)
insert overwrite directory '/export/servers/exporthive' row format delimited fields terminated by '\t' collection items terminated by[a1] '#' select * from score;
(对于集合类型我们使用#来进行分割,因为这个表里面没有集合类型,所以加不加这个结果都一样)
Hadoop命令导出到本地
dfs -get /export/servers/exporthive/000000_0 /export/servers/exporthive/local.txt;
hive shell 命令导出
基本语法:(hive -f/-e 执行语句或者脚本 > file)
bin/hive -e "select * from myhive.score;" > /export/servers/exporthive/score.txt
export导出到HDFS上
export table score to '/export/exporthive/score';
sqoop导出
后续单独讲。
清空表数据
只能清空管理表,也就是内部表
truncate table score6;
清空外部表会报错(
Error: Error while compiling statement: FAILED: SemanticException [Error 10146]: Cannot truncate non-managed table score5. (state=42000,code=10146)
)
第2节 hive基本操作:11、hive当中的分桶表以及修改表删除表数据加载数据导出等的更多相关文章
- hive -- 分区,分桶(创建,修改,删除)
hive -- 分区,分桶(创建,修改,删除) 分区: 静态创建分区: 1. 数据: john doe 10000.0 mary smith 8000.0 todd jones 7000.0 boss ...
- 大数据学习day26----hive01----1hive的简介 2 hive的安装(hive的两种连接方式,后台启动,标准输出,错误输出)3. 数据库的基本操作 4. 建表(内部表和外部表的创建以及应用场景,数据导入,学生、分数sql练习)5.分区表 6加载数据的方式
1. hive的简介(具体见文档) Hive是分析处理结构化数据的工具 本质:将hive sql转化成MapReduce程序或者spark程序 Hive处理的数据一般存储在HDFS上,其分析数据底 ...
- Hive中数据的加载和导出
原文:http://blog.javachen.com/2014/06/09/hive-data-manipulation-language.html 关于 Hive DML 语法,你可以参考 apa ...
- Hive中数据加载失败:root:supergroup:drwxr-xr-x
Hive中数据加载失败:inode=:root:supergroup:drwxr-xr-x 在执行hive,数据加载的时候,遇到了一个错误,如下图: 在执行程序的过程中,遇到权限问题很正常,背后原理也 ...
- 如何每日增量加载数据到Hive分区表
如何每日增量加载数据到Hive分区表 hadoop hive shell crontab 加载数据 数据加载到Hive分区表(两个分区,日期(20160316)和小时(10))中 每日加载前一天的日志 ...
- 第1节 IMPALA:10、基本查询语法;11、数据加载的4种方式
9.3. 创建数据库表 创建student表 CREATE TABLE IF NOT EXISTS mydb1.student (name STRING, age INT, contact INT ) ...
- Oracle 基本操作--数据类型、修改和删除表、增删改查和复制表
一.Oracle基础数据类型:数据类型: 创建数据表时,设计数据表的结构问题,也就是设计及确定数据表中各个列的数据类型,是数值.字符.日期还是图像等其他类型. 因为只有设计好数据表结构,系统才会在磁盘 ...
- Hive JDBC执行load时无法从本地加载数据
通过hive-jdcv连接hive server,在应用服务端执行以下命令,报错:Hiver Server节点上找不到data.txt load data local inpath '/home/dw ...
- 【hive】 hive 加载数据
1. insert 插入数据 要保证启动了jobhistory 否则会抛出异常 hdfs中查看内容 2. create table 表名字 select 字段... from 表名 hdfs查看数据 ...
随机推荐
- bzoj1007 [HNOI2008]水平可见直线——单调栈
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1007 可以把直线按斜率从小到大排序,用单调栈维护,判断新直线与栈顶的交点和栈顶与它之前直线的 ...
- 小程序-文章:微信小程序常见的UI框架/组件库总结
ylbtech-小程序-文章:微信小程序常见的UI框架/组件库总结 1.返回顶部 1. 想要开发出一套高质量的小程序,运用框架,组件库是省时省力省心必不可少一部分,随着小程序日渐火爆,各种不同类型的小 ...
- 《Deep Learning Face Attributes in the Wild》论文笔记
论文背景: IEEE International Conference on Computer Vision 2015 Ziwei Liu1, Ping Luo1, Xiaogang Wang2, X ...
- 377D
树形dp 就是求每个点到标记的点的最大距离 一个经典模型 有一个巧妙的方法,我们求出以这些关键点的直径,然后每个点到关键点的最大距离就是到直径两端的距离 #include<bits/stdc++ ...
- c语言和c++栈的简单实现以及构造器的原理
也就是训练将原来的c语言 用类表示出来.. 关于构造器: //1与类名相同 没有返回值 被系统生成对象时自动调用,用于初始化 //2 可以有参数 构造器重载 默认参数 //3 重载和默认不能同时存在, ...
- HDU 5912 Fraction (模拟)
题意:给定一个分式,让你化简. 析:从分母开始模拟分数的算法,最后约分. 代码如下: #pragma comment(linker, "/STACK:1024000000,102400000 ...
- Git学习三
一.准备Github远程仓库 1.github官网注册账户 2.ubuntu端创建SSH Key $ssh-keygen -t rsa -C "youremail@example.com&q ...
- 洛谷P2607 [ZJOI2008]骑士(基环树)
传送门 首先这是一个有$n$个点$n$条边的图(据大佬们说这玩意儿叫做基环树?) 不难(完全没有)发现每个连通块里最多只有一个环 那么找到这个环,然后把它断开,再对它的两个端点分别跑树形dp 设$dp ...
- Visual Studio 2015 个版本下载
Visual Studio 2015是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具.代码管控工具.集成开发环境(IDE)等等.所写的目标代码适用于微软支持的所有 ...
- 【小程序】基于.NET CORE2.1 的 微信开放平台 第三方平台开发 教程一 准备工作
微信第三方平台概述 公众平台第三方平台是为了让公众号或小程序运营者,在面向垂直行业需求时,可以一键授权给第三方平台(并且可以同时授权给多家第三方),通过第三方平台来完成业务,开放给所有通过开发者资质认 ...