前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hive基础操作

Hive基础操作

作者头像
俺也想起舞
发布2019-07-24 14:47:18
5630
发布2019-07-24 14:47:18
举报
文章被收录于专栏:码的一手好代码

hive的常用操作,不是很全的那种

创建数据库
代码语言:javascript
复制
cearte database [if not exists] db.hive;
表的相关操作
代码语言:javascript
复制
-- 创建表
create table [if not exists] student(
    name string,
    age int,
    score int)
row format delimited fileds terminated by '\t';

-- 创建子表——从表中提取出所需要的字段
create table if not exists db_hive.emp_cats
as
select name, age, from student;

-- 清除表数据,但不删除表
truncate table dept_cats;

-- 建立表结构相同的表
create table if not exists student_like
like
test.student;

-- 修改表的名称
alter table dept_like rename to dept_like_rename;

-- 删除表(好像会卡住
drop table if exists dept_like_rename;

-- 创建外部表
-- 外部表在删除时不会删掉内置的数据
create external table if not exists db_hive.emp_external(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int
)
row format delimited fields terminated by '\t'
location '/user/root/hive/warehouse/emp_external';

-- 建立二分区表
set hive.exec.dynamic.partition.mode=nonstrict; # 允许动态分区
-- 静态分区 
create external table if not exists db_hive.emp_partition2(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int
)
PARTITIONED BY (month string, day string)
row format delimited fields terminated by '\t'
location '/user/root/hive/warehouse/emp_external';

load data local inpath '/root/data_emp/emp.txt' into table db_hive.emp_partition2
partition (month='201810', day='21');

SELECT * FROM emp_partition2 where month='201810' and day='21';
-- 动态分区(分区的字段必须在后面)
FROM raw_access_log 
INSERT OVERWRITE TABLE
partitioned_raw_access_log
PARTITION(year,month,day)
SELECT ip.....year,month,day
-- 混合模式
FROM raw_access_log 
INSERT OVERWRITE TABLE
partitioned_raw_access_log
PARTITION(year="2007",month,day)
SELECT ip.....month,day

-- 分桶模式
FROM table_test
CLUSTERED BY (column_name,...) INTO 8 BUCKETS//与分区不同的是列名必须是已经存在的列名

-- 将数据导入分桶表
-- 首先设置reduce数量和Buckets数量一直,即最终决定产生多少个文件
-- hive.enforce.sorting和hive.enforce.bucketing设置为true,免了dirtribute by & sort by
SET hive.enforce.bucketing=true;
SET mapred.reduce.tasks=200;
FROM table_fa
INSERT OVERWRITE TABLE table_son
SELECT ...,request_date
WHERE ...
[DISTRIBUTE BY ...][SORT BY ...]

-- 修复分区
MSCK REPAIR TABLE table_name;
四个by
代码语言:javascript
复制
-- order by 全局排序,仅仅只有一个reducer
	select * from emp order by empno desc;
-- sort by 对每一个reduce内部进行排序,全局结果集不是排序的
	set mapreduce.job.reduces=3;
	select * from emp sort by empno asc;
	insert overwrite local directory '/opt/datas/sortby_res' select * from emp sort by empno asc;
-- distribute by 分区,类似mapreduce中partitioner,对数据分区,
	通常结合sort by使用
	insert overwrite local directory '/opt/datas/distby_res' select * from emp
	distribute by deptno sort by empno asc;
-- cluster by 当distribute by的字段和sort by的字段相同时就可以用cluster by代替
	insert overwrite local directory '/opt/datas/clusterby_res' select * from emp
	cluster by empno asc;
获取表的信息
代码语言:javascript
复制
-- 获取基本信息
desc student;
-- 获取详细信息
desc formatted student
数据导入
将文件信息导入
代码语言:javascript
复制
# 将本地数据信息载入数据表
load data local inpath '/data/student.txt' into table db_hive.student;
# 将HDFS数据载入数据表
load data inpath '/student.txt' into table db_hive.student;
将表中信息筛筛选导入
代码语言:javascript
复制
hive> insert into table test
    > partition (age='25')
    > select id, name, tel
    > from wyp;
数据导出
代码语言:javascript
复制
	# 1.常见导出insert overwrite local directory '/opt/datas/test.txt' select * from emp;
	# 2.定义格式
insert overwrite local directory '/opt/datas/test1.txt' 
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' COLLECTION ITEMS TERMINATED BY '\n'
select * from emp;
	# 3.hive -e "select * from ..." > /opt/datas/xx.txt
	# 4.保存到HDFS上
	insert overwrite directory '' 
		ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' COLLECTION ITEMS TERMINATED BY '\n'
		select * from emp_partition;
	5.sqoop
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 创建数据库
  • 表的相关操作
  • 四个by
  • 获取表的信息
  • 数据导入
    • 将文件信息导入
      • 将表中信息筛筛选导入
      • 数据导出
      相关产品与服务
      数据库
      云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档