Hive操作

Hive数据库操作

  • 数据库(Database):表的集合,HDFS中表现为一个文件夹
    默认在hive.metastore.warehouse.dir属性目录下
  • 如果没有指定数据库,默认使用default数据库
  • Hive数据库操作:同MySQL一样
#启动hive
hive
#创建hivetest库
create database hivetest;
#查看数据库
show database;
#切换到hivetest数据库
use hivetest;

在这里插入图片描述

Hive表级操作

  • 数据表(Tables)
    • 分为内部表和外部表
      默认内部表,会默认在指定的存储空间中建立对应文件夹,只要把文件放入,表就可以读取到数据(需要和表结构匹配)
    • 内部表(管理表)
      HDFS中为所属数据库目录下的子文件夹
      数据完全由Hive管理,删除表(元数据)会删除数据
    • 外部表(External Tables)
      数据保存在指定位置的HDFS路径中
      Hive不完全管理数据,删除表(元数据)不会删除数据
  • Hive建表语句
    在这里插入图片描述
#创建employee表
create table employee(
 name string,
 address array<string>,
 personalInfo array<string>,
 technol map<string,int>,
 jobs map<string,string>)
 row format delimited 
 #如何分隔列(字段)
 fields terminated by '|'
 #如何分隔集合和映射
 collection items terminated by ','
 map keys terminated by ':'
 lines terminated by '\n';

在这里插入图片描述

# 将本地文件employee.txt数据传输到hdfs中新建的hivetest库中的employee表
hadoop fs -put employee.txt /opt/hive/warehouse/hivetest.db/employee

在这里插入图片描述

#查询employee表中数据
select * from employee;

在这里插入图片描述

#查看表结构
desc employee;

在这里插入图片描述

#修改表结构
alter table employee change personalInfo `info` struct<gender:string,age:int>;

在这里插入图片描述

#创建emp_id表
create external table emp_id(
name string,
id int,
address array<string>,
personalInfo struct<sex:string,age:int>,
technol map<string,int>,
job map<string,string>)
clustered by(id) into 3 buckets
row format delimited
#如何分隔列(字段)
fields terminated by '|'
#如何分隔集合和映射
collection items terminated by ','
map keys terminated by ':'
#换行设置
lines terminated by '\n'
#文件存储格式
stored as textfile
#数据存储路径(HDFS)
location '/usr/test/employee';

在这里插入图片描述
在这里插入图片描述

Hive分区操作

定义分区

在这里插入图片描述

#创建p_test表时定义分区
create table p_test(
pid int,
pname string)
#通过partitioned by定义分区
partitioned by (person string)
row format delimited
fields terminated by ','
lines terminated by '\n';

在这里插入图片描述

动态分区

  • 使用动态分区需设定属性
    在这里插入图片描述
  • 动态分区设置方法
    在这里插入图片描述
#动态定义分区person
insert into p_test partition (person='sam') values(1,'a'),(2,'b'),(3,'c');
insert into p_test partition (person='bob') values(4,'d'),(5,'e'),(6,'f');

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

静态分区

在这里插入图片描述

#创建表employee_partition
create table employee_partition(
name string,
address array<string>,
info struct<gender:string,age:int>,
technol map<string,int>,
jobs map<string,string>)
partitioned by (country string,add string)
row format delimited
fields terminated by '|'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n';

在这里插入图片描述

  • 从本地目录中读取数据
#从本地/root/employee.txt目录下读取数据至employee_partition表中
load data local inpath '/root/employee.txt'
into table employee_partition
#定义分区
partition (country='china',add='LiaoNing');

在这里插入图片描述

#查询employee_partition表中数据
select * from employee_partition;

在这里插入图片描述

  • 2、从hdfs中读取数据
#上传employee.txt至hdfs的/opt/hive/warehouse/hivetest.db/employee目录下
hadoop fs -put employee.txt /opt/hive/warehouse/hivetest.db/employee

在这里插入图片描述

#从hdfs的/opt/hive/warehouse/hivetest.db/employee/employee.txt目录下读取数据至employee_partition表
load data inpath '/opt/hive/warehouse/hivetest.db/employee/employee.txt'
into table employee_partition
#定义分区
partition (country='china',add='ninjing');

在这里插入图片描述

#查询employee_partition表中数据
select * from employee_partition;

在这里插入图片描述
查看hdfs中的分区情况
在这里插入图片描述

  • 动态分区
    • 使用动态分区需设定属性
      在这里插入图片描述
    • 动态分区设置方法
      在这里插入图片描述
#动态定义分区person
insert into p_test partition (person='sam') values(1,'a'),(2,'b'),(3,'c');
insert into p_test partition (person='bob') values(4,'d'),(5,'e'),(6,'f');

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Hive分桶操作

分桶(Buckets )

  • 分桶对应于HDFS中的文件
    更高的查询处理效率
    使抽样(sampling)更高效
    根据“桶列”的哈希函数将数据进行分桶
  • 分桶只有动态分桶
set hive.enforce.bucketing = true;
  • 定义分桶
#分桶的列是表中已有的列
#分桶数最好是2的n次方
clustered by (employee_id) into 2 buckets
  • 必须使用INSERT方式加载数据

案例:

create external table emp_bucket(
name string,
id int,
address array<string>,
personalInfo struct<sex:string,age:int>,
technol map<string,int>,
job map<string,string>)
#定义分桶
clustered by(id) into 3 buckets
row format delimited
fields terminated by '|'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n'
stored as textfile
location '/usr/test/bucket';

#动态分桶
set hive.enforce.bucketing=true;

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值