Hive中对库和表的操作

一.前提条件

hive安装配置完成
hive安装教程链接 Hive单机版安装

二.对库的操作

  • 建库 (库名hivetest)
create database hivetest;

在这里插入图片描述

  • 删库
drop database hivetest;

在这里插入图片描述

  • 进入库
use hivetest;

在这里插入图片描述

  • hive对库的操作和mysql相同

三.对表的操作

  • 建表语句
create table employee(
name string,
address array<string>,
personnalinfo array<string>,
technol map<string,int>,
job map<string,string>)
row format delimited
fields terminated by '|'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n';

在这里插入图片描述

  • 将素材上传至根目录下
    在这里插入图片描述
  • 素材内容
Michael|Montreal,Toronto|Male,30|DB:80|Product:DeveloperLead
Will|Montreal|Male,35|Perl:85|Product:Lead,Test:Lead
Shelley|New York|Female,27|Python:80|Test:Lead,COE:Architect
Lucy|Vancouver|Female,57|Sales:89|Sales:Lead
  • 将素材内容导入建好的表employee中
 hdfs dfs -put employee.txt /opt/hive/warehouse/hivetest.db/employee
  • 文件路径从网页获得: 50070端口
    在这里插入图片描述
  • 查询
select * from employee;

在这里插入图片描述

select name,address[0],job["Product"] from employee  where job["Product"] is not null;

在这里插入图片描述

  • 查看表结构
desc employee;

在这里插入图片描述

  • 改变列名,并修改属性
alter table employee change personnalinfo info struct<gender:string,age:int>;

在这里插入图片描述

四.Hive分区(Partitions)

  • 分区主要用于提高性能
    分区列的值将表划分为setments(文件夹)
    查询时使用分区列和常规列类似
    查询时Hive自动过滤掉不用于提高性能的分区

  • 分为静态分区和动态分区

  • Hive分区操作

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';
  • 将根目录下的employee.txt文件导入employee_partition表中
load data local inpath '/root/employee.txt' into table employee_partition partition (country='china',add='liaoning');

在这里插入图片描述

load data inpath '/opt/hive/warehouse/hivetest.db/employee/employee.txt' into table employee_partition partition (country="china",add="nanjing");

在这里插入图片描述

select * from employee_partition where add="nanjing";

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值