Hive 数据库的基本操作

数据库的基本操作

1.数据库的增删改查
增:create database [if not exists] myhive;
删: drop database myhive;(数据库内没有表可以删除,有表不能删除)
改:数据库不允许修改
查:show database;
查看详细信息:
desc database myhive;
desc database extended myhive;
数据库的切换:
use myhive (数据库的名字);

数据表的基本操作

创建基本数据表(内部表):
create table tableName(字段名称 字段类型,字段名称 字段类型)

创建外部数据表:
create external table tableName(字段名称 字段类型,字段名称 字段类型)

内部表与外部表的区别
在删除内部表时:内部表删除将表的元数据和数据同时删除
在删除外部表时:外部表的元数据被删除,数据本身不删除

删除表
drop table tablename;

修改表
alter tablename ***

查询表
show tables;
desc tablename;

加载数据
从linux中加载数据到hive
load data local inpath ‘数据路径’ into table 表名;
从hdfs中加载数据到hive并覆盖
load data inpath ‘数据路径’ overwrite into table 表名;

外部表
create external table teacher(
t_id string,t_name string)
row format delimited fifields terminated by ‘\t’;

加载数据
load data local inpath ‘/export/servers/hivedatas/techer .csv’ into table techer ;

在hdfs查看表中的数据
hadoop fs -ls /user/hive/warehouse/myhive.db/teacher

在hive中查询
select * from techer

删除数据表techer
drop table techer

内部表

create table student(t_id string,t_name string) row format delimited fifields terminated by ‘\t’;

加载数据
load data local inpath ‘/export/servers/hive’

在hdfs查看表中的数据
Hadoop fd-ls /user/hive/warehouse/myhive.db/student

删除数据表techer
drop table student;

分区表

企业常见的分区规则:按天进行分区(一天一个分区)

创建分区表的语句
create table score(s_id string,c_id string,s_score int) partitioned by (month sting)row format delimited
fifieldsterminated by ‘\t’;

数据加载

load data local inpath ‘/opt/hive/score.csv’ into table score partition (month=‘201806’);

特别强调:
分区字段绝对不能出现在数据表以有的字段中。

作用:
将数据按区域划分开,查询时不用扫描无关的数据,加快查询速度。

分桶表

开启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 fifields terminated by ‘\t’;
创建基本表
create table course_common (c_id string,c_name string,t_id string) row format delimited fifields terminated by
‘\t’;
基本表添加数据
load data local inpath ‘/export/servers/hivedatas/course.csv’ into table course_common;
在基本表中查询数据插入到分桶表
insert overwrite table course select * from course_common cluster by(c_id);
确认分桶内的数据
[root@node01 hive]# hadoop fs -cat /user/hive/warehouse/course/000000_0 03 英语 03 [root@node01 hive]#
hadoop fs -cat /user/hive/warehouse/course/000001_0 01 语文 02 [root@node01 hive]# hadoop fs -cat
/user/hive/warehouse/course/000002_0 02 数学 01
特别强调:
分桶字段必须是表中的字段。
分桶逻辑:
对分桶字段求哈希值,用哈希值与分桶的数量取余,余几,这个数据就放在那个桶内。

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值