hive相关操作

hive相关操作

学习了尚学堂的hive视频,做如下笔记。主要是hive中的建表,插入,函数相关的知识。

一、表数据

以下为人员信息表数据包含四个字段,分别为id、name、likes、address
- 1,xiaoming,book-tv-football,beijing:haidian-tianjin:wuqing
- 2,sunjian,tv-football,xian:gaoxin-tianjin:wuqing
- 3,liuyang,book-code-football,henan:xinxiang-liaoning:dalian

二、DDL 几种方式创建表

  • 1、创建内部表
    hive中数据类型比较丰富,如下有数组类型和键值对的map类型,上面数据字段之间以‘,’分隔,likes字段以‘-’分隔为数组样式,address字段为键值对形式(键‘:’值)。所以如下建表请注意这些分隔方式
create table psn1(
id int,
name string,
likes array<string>,
address map<string,string>
)
row format delimited
fields terminated by ','
collection items terminated by '-'
map keys terminated by ':';
  • 2、创建外部表
create table psn2(
id int,
name string,
likes array<string>,
address map<string,string>
)
row format delimited
fields terminated by ','
collection items terminated by '-'
map keys terminated by ':'
location '/usr/psn2';
  • 3、创建表3(根据其他表,创建中间表时使用)
create table psn3
as
select id,name,likes,address from psn1;
  • 4、创建表4(创建psn1的表结构)
create table psn4 like psn1;
  • 5、分区
    创建表(加分区)例 按性别分区 分区字段不能使列名
    装载数据时,按分区分别存储
    (应用:日志文件过大就可以采用时间分区)
create table psn5(
id int,
name string,
likes array<string>,
address map<string,string>
)
partitioned by (sex string)
row format delimited
fields terminated by ','
collection items terminated by '-'
map keys terminated by ':';

指定分区字段

load data local inpath '/root/data' into table psn5 partition (sex='boy');
load data local inpath '/root/data' into table psn5 partition (sex='girl');

按分区查询

select * from psn5 where sex='boy';

添加分区

alert table psn5 partition (sex='qita');

删除分区(会删除分区的数据)

alert table psn5 drop partition (sex='qita');

当分区字段定义了多个时(定义时分区字段有顺序,有层次结构)
,载入数据要将多个分区字段都写清楚。

  • 6、hive中有正则表达式方式来清洗数据
CREATE TABLE apachelog (
  host STRING,
  identity STRING,
  user STRING,
  time STRING,
  request STRING,
  status STRING,
  size STRING,
  referer STRING,
  agent STRING)
ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.RegexSerDe'
WITH SERDEPROPERTIES (
  "input.regex" = "([^]*) ([^]*) ([^]*) (-|\\[^\\]*\\]) ([^ \"]*|\"[^\"]*\") (-|[0-9]*) (-|[0-9]*)(?: ([^ \"]*|\".*\") ([^ \"]*|\".*\"))?"
)
STORED AS TEXTFILE;

三、DML

  • 1、插入数据load方式
    (load方式用的最多,不需要转为mr操作)
    加载本地数据(将本地文件上传到hdfs,在放到表的文件夹)
load data local inpath '本地路径' into table psn1;

加载hdfs数据(移动hdfs的文件到表的文件夹)

load data inpath 'hdfs路径' into table psn1;
  • 2、(一般做分析psn1存储到结果表psn2)用下面方式
from psn1 pvs
insert into table psn2
select pvs.id,pvs.name,psv.likes,psv.address;
  • 3、insert into (转换为mr执行,效率低一般不会使用)

四、hive中的函数

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值