hive基础知识二

1. Hive的分区表

1.1 hive的分区表的概念

在文件系统上建立文件夹,把表的数据放在不同文件夹下面,加快查询速度。

1.2 hive分区表的构建

  • 创建一个分区字段的分区表
hive> create table student_partition1(
    id int,
    name string,
    age int)
    partitioned by (dt string)
    row format delimited fields terminated by ‘\t‘;   
  • 创建二级分区表
hive> create table student_partition2(
    id int,
    name string,
    age int)
    partitioned by (month string, day string)
    row format delimited fields terminated by ‘\t‘; 

2、Hive修改表结构

2.1 修改表的名称

hive> alter table student_partition1 rename to student_partition3;

2.2 表的结构信息

hive> desc student_partition3;
hive> desc formatted  student_partition3;

2.3 增加/修改/替换列信息

  • 增加列
hive> alter table student_partition3 add columns(address string);
  • 修改列
hive> alter table student_partition3 change column address address_id int;
  • 替换列
hive> alter table student_partition3 replace columns(deptno string, dname string, loc string);
 #表示替换表中所有的字段

2.4 增加/删除/查看分区

  • 添加分区
//添加单个分区
hive> alter table student_partition1 add partition(dt=‘20170601‘) ;
//添加多个分区
hive> alter table student_partition1 add partition(dt=‘20190818‘) partition(dt=‘20190819‘); 

  • 删除分区
hive> alter table student_partition1 drop partition (dt=‘20170601‘);
hive> alter table student_partition1 drop partition (dt=‘20170602‘),partition (dt=‘20170603‘);
  • 查看分区
hive> show partitions student_partition1;

3. Hive数据导入

3.1 向表中加载数据(load)

  • 语法
hive> load data [local] inpath ‘dataPath‘ overwrite | into table student [partition (partcol1=val1,…)]; 

?   load data: 表示加载数据

?   ==local==: 表示从本地加载数据到hive表;否则从HDFS加载数据到hive表

?   inpath: 表示加载数据的路径

?   overwrite: 表示覆盖表中已有数据,否则表示追加

?   into table: 表示加载到哪张表

?   student: 表示具体的表

?   partition: 表示上传到指定分区

  说明:load data ...本质,就是将dataPath文件上传到hdfs的 ‘/user/hive/warehouse/tablename’ 下。

  实质:hdfs dfs -put /opt/student1.txt /user/hive/warehouse/student1

例如:
--普通表:
load data local inpath ‘/opt/bigdata/data/person.txt‘ into table person
--分区表:
load data local inpath ‘/opt/bigdata/data/person.txt‘ into table student_partition1 partition(dt="20190505")
--查询表:
select * from student_partition1 where dt=‘20190818‘;
--实操案例:
实现创建一张表,然后把本地的数据文件上传到hdfs上,最后把数据文件加载到hive表中

3.2 通过查询语句向表中插入数据(insert)

  • 从指定的表中查询数据结果数据然后插入到目标表中

    • 语法
insert into/overwrite table  tableName  select xxxx from tableName
insert into table student_partition1 partition(dt="2019-07-08") select * from student1;

3.3 查询语句中创建表并加载数据(as select)

  • 在查询语句时先创建表,然后进行数据加载

    • 语法
create table if not exists tableName as select id, name from tableName;

3.4 创建表时通过location指定加载数据路径

  • 创建表,并指定在hdfs上的位置
create table if not exists student1(
id int,
name string)
row format delimited fields terminated by ‘\t‘
location ‘/user/hive/warehouse/student1‘;
  • 上传数据文件到hdfs上对应的目录中
hdfs dfs -put /opt/student1.txt /user/hive/warehouse/student1

3.5 Import数据到指定Hive表中

  注意:先用export导出后,再将数据导入。

create table student2 like student1;
export table student1  to   ‘/export/student1‘;
import table student2  from ‘/export/student1‘;

4、Hive数据导出(15分钟)

4.1 insert 导出

  • 1、将查询的结果导出到本地
insert overwrite local directory ‘/opt/bigdata/export/student‘ select * from student;
默认分隔符‘\001‘
  • 2、将查询的结果格式化导出到本地
insert overwrite local directory ‘/opt/bigdata/export/student‘
           row format delimited fields terminated by  ‘,‘
           select * from student;
  • 3、将查询的结果导出到HDFS上(没有local)
insert overwrite  directory ‘/export/student‘
           row format delimited fields terminated by  ‘,‘
           select * from student;

4.2 Hadoop命令导出到本地

hdfs dfs -get /user/hive/warehouse/student/student.txt /opt/bigdata/data

4.3 Hive Shell 命令导出

  • 基本语法:

    • hive -e "sql语句" > file
    • hive -f sql文件 > file
    bin/hive -e ‘select * from default.student;‘ > /opt/bigdata/data/student1.txt

4.4 export导出到HDFS上

export table default.student to ‘/user/hive/warehouse/export/student1‘;

5、hive的静态分区和动态分区

5.1 静态分区

  • 表的分区字段的值需要开发人员手动给定

    • 1、创建分区表
    create table order_partition(
    order_number string,
    order_price  double,
    order_time string
    )
    partitioned BY(month string)
    row format delimited fields terminated by ‘\t‘;
    • 2、准备数据 order_created.txt内容如下
  10001 100 2019-03-02
  10002 200 2019-03-02
  10003 300 2019-03-02
  10004 400 2019-03-03
  10005 500 2019-03-03
  10006 600 2019-03-03
  10007 700 2019-03-04
  10008 800 2019-03-04
  10009 900 2019-03-04
  • 3、加载数据到分区表
load data local inpath ‘/opt/bigdata/data/order_created.txt‘ overwrite into table order_partition partition(month=‘2019-03‘);
  • 4、查询结果数据
  select * from order_partition where month=‘2019-03‘;
  结果为:
  10001   100.0   2019-03-02      2019-03
  10002   200.0   2019-03-02      2019-03
  10003   300.0   2019-03-02      2019-03
  10004   400.0   2019-03-03      2019-03
  10005   500.0   2019-03-03      2019-03
  10006   600.0   2019-03-03      2019-03
  10007   700.0   2019-03-04      2019-03
  10008   800.0   2019-03-04      2019-03
  10009   900.0   2019-03-04      2019-03

5.2 动态分区

  • 按照需求实现把数据自动导入到表的不同分区中,不需要手动指定

    • 需求:按照不同部门作为分区导数据到目标表

      • 1、创建表
      --创建普通表
      create table t_order(
          order_number string,
          order_price  double,
          order_time   string
      )row format delimited fields terminated by ‘\t‘;
      ?
      --创建目标分区表
      create table order_dynamic_partition(
          order_number string,
          order_price  double
      )partitioned BY(order_time string)
      row format delimited fields terminated by ‘\t‘;
      • 2、准备数据 order_created.txt内容如下
      10001   100 2019-03-02
      10002   200 2019-03-02
      10003   300 2019-03-02
      10004   400 2019-03-03
      10005   500 2019-03-03
      10006   600 2019-03-03
      10007   700 2019-03-04
      10008   800 2019-03-04
      10009   900 2019-03-04
      • 3、向普通表t_order加载数据
      load data local inpath ‘/opt/bigdata/data/order_created.txt‘ overwrite into table t_order;
      • 4、动态加载数据到分区表中
      ---要想进行动态分区,需要设置参数
      hive> set hive.exec.dynamic.partition=true; //使用动态分区
      hive> set hive.exec.dynamic.partition.mode=nonstrict; //非严格模式
      
          insert into table order_dynamic_partition partition(order_time) select order_number,order_price,order_time from t_order;
      
      --注意字段查询的顺序,分区字段放在最后面。否则数据会有问题。
      • 5、查看分区
      show partitions order_dynamic_partition;

6、分桶

hive的分桶表

  • 分桶是相对分区进行更细粒度的划分。
  • 分桶将整个数据内容安装某列属性值取hash值进行区分,具有相同hash值的数据进入到同一个文件中
    • 比如按照name属性分为3个桶,就是对name属性值的hash值对3取摸,按照取模结果对数据分桶。

      • 取模结果为0的数据记录存放到一个文件
      • 取模结果为1的数据记录存放到一个文件
      • 取模结果为2的数据记录存放到一个文件
      • 取模结果为3的数据记录存放到一个文件
  • 作用
    • 1、取样sampling更高效。没有分区的话需要扫描整个数据集。
    • 2、提升某些查询操作效率,例如map side join
  • 案例演示
    • 1、创建分桶表

      • 在创建分桶表之前要执行命名

        • set hive.enforce.bucketing=true;开启对分桶表的支持
        • set mapreduce.job.reduces=4; 设置与桶相同的reduce个数(默认只有一个reduce)
      --分桶表
      create table user_buckets_demo(id int, name string)
      clustered by(id)
      into 4 buckets
      row format delimited fields terminated by ‘\t‘;
      ?
      --普通表
      create table user_demo(id int, name string)
      row format delimited fields terminated by ‘\t‘;
    • 2、准备数据文件 buckets.txt
      1    laowang1
      2    laowang2
      3    laowang3
      4    laowang4
      5    laowang5
      6    laowang6
      7    laowang7
      8    laowang8
      9    laowang9
      10    laowang10
    • 3、加载数据到普通表 user_demo 中
    load data local inpath ‘/opt/bigdata/data/buckets.txt‘ into table user_demo;
    • 4、加载数据到桶表user_buckets_demo中
    insert into table user_buckets_demo select * from user_demo;
  • 5、hdfs上查看表的数据目录

  • 6、抽样查询桶表的数据

    • tablesample抽样语句,语法:tablesample(bucket x out of y)

      • x表示从第几个桶开始取数据
      • y表示桶数的倍数,一共需要从 桶数/y 个桶中取数据
select * from user_buckets_demo tablesample(bucket 1 out of 2)
?
-- 需要的总桶数=4/2=2个
-- 先从第1个桶中取出数据
-- 再从第1+2=3个桶中取出数据


原文地址:https://www.cnblogs.com/lojun/p/11376476.html

时间: 2024-08-02 22:53:07

hive基础知识二的相关文章

ASP.NET Core 2.2 基础知识(二) 中间件

原文:ASP.NET Core 2.2 基础知识(二) 中间件 中间件是一种装配到应用管道以处理请求和相应的软件.每个软件都可以: 1.选择是否将请求传递到管道中的下一个组件; 2.可在调用管道中的下一个组件前后执行工作. 管道由 IApplicationBuilder 创建: 每个委托都可以在下一个委托前后执行操作,.此外,委托还可以决定不将请求传递给下一个委托,这就是对请求管道进行短路.通常需要短路,是因为这样可以避免不必要的工作.比如: 1.静态文件中间件可以返回静态文件请求并使管道的其余

Java基础知识二次学习-- 第一章 java基础

基础知识有时候感觉时间长似乎有点生疏,正好这几天有时间有机会,就决定重新做一轮二次学习,挑重避轻 回过头来重新整理基础知识,能收获到之前不少遗漏的,所以这一次就称作查漏补缺吧!废话不多说,开始! 第一章  JAVA简介 时间:2017年4月24日10:23:32 章节:01章_02节 内容:jdk的配置与安装 完成情况:已经完成,cmd中javac提示出相关命令 时间:2017年4月24日10:30:39 章节:01章_04节 内容:输出HelloWorld 完成情况: 已经完成 javac先将

Java基础知识(二)

1,字符串 new String("abc")创建了几个对象? 一个或两个,如果常量池中原来有"abc",则只创建一个对象:如果常量池中原来没有字符串"abc",那么就会创建两个对象. String s="abc"; String s1="ab"+"c"; System.out.println(s==s1); 输出 true ,因为"ab"+"c"

Powershell基础知识(二)

上一节主要介绍Powershell可发现,面向对象,一致性等特性,以及Powershell命令是基于.Net对象等重要概念,以及Powershell命令的命名规范,详细内容点击这里. 这一节的Powershell基础知识主要包含以下知识点 获取命令的摘要信息. 获取命令的帮助信息. 总结. 获取命令的摘要信息 Powershell命令 Get-Command 可检索当前shell中所有可用的命令名称.在Powershell提示符输入 Get-Command ,输出的内容类似以下内容(以下只写出输

《Programming Hive》读书笔记(二)Hive基础知识

阅读方法:第一遍读是浏览,建立知识索引,因为有些知识不一定能用到,知道就好.感兴趣的部分可以多研究. 以后用的时候再详细看,并结合其他资料一起. Chapter 3.Data Types and File Formats 原始数据类型和集合数据类型 Select出来的数据,列与列之间的分隔符可以指定 Chapter 4.HiveQL:Data Definition 创建数据库,创建和改动表,分区的操作 Chapter 5.HiveQL:Data Manipulation 1 加载数据和导出数据,

Hive基础知识

一.Hive简介什么是Hive为什么使用Hive面临的问题:为什么要使用Hive:二.Hive结构三.Hive支持的格式四.表的操作创建表删除表清空表修改表修改字段名与字段类型增加列修改列的顺序删除列替换表修改字段顺序其他操作查看表信息查看建表信息查看格式信息改变表文件格式查看函数五.数据操作1.插入数据从文件读取数据从其他结果集插入2.分区和分桶创建分区表查看分区插入分区数据添加分区重命名分区删除分区分区使用分桶六.复合类型ArrayMap七.创建视图和索引以及数据缓存视图创建视图删除视图索引

《Programming Hive》读书笔记(两)Hive基础知识

:第一遍读是浏览.建立知识索引,由于有些知识不一定能用到,知道就好.感兴趣的部分能够多研究. 以后用的时候再具体看.并结合其它资料一起. Chapter 3.Data Types and File Formats 原始数据类型和集合数据类型 Select出来的数据,列与列之间的分隔符能够指定 Chapter 4.HiveQL:Data Definition 创建数据库,创建和修改表,分区的操作 Chapter 5.HiveQL:Data Manipulation 1 载入数据和导出数据,应该从本

计算机科学基础知识(二)Relocatable Object File

一.前言 一个合格的c程序员(也可以叫做软件工程师,这样看起来更高大上,当然,我老婆心情不好的时候总是叫我"死打字的",基本也能描述这份职业,呵呵)需要理解编译.链接和加载的过程,而不是仅仅关注c语言的语法和词法.本文主要以此为切入点,描述linux系统下,一个普通的hello world程序的生命历程,并借机灌输一些程序编译时和运行时的基本术语和概念.当然,由于我本人是一个linuxer,因此借用linux来描述这些知识会方便些,但是对于计算机科学而言,这些东西概念上是类似的,只是实

hive基础知识四

1. hive表的数据压缩 1.1 数据的压缩说明 压缩模式评价 可使用以下三种标准对压缩方式进行评价 1.压缩比:压缩比越高,压缩后文件越小,所以压缩比越高越好 2.压缩时间:越快越好 3.已经压缩的格式文件是否可以再分割:可以分割的格式允许单一文件由多个Mapper程序处理,可以更好的并行化 常见压缩格式 压缩方式 压缩比 压缩速度 解压缩速度 是否可分割 gzip 13.4% 21 MB/s 118 MB/s 否 bzip2 13.2% 2.4MB/s 9.5MB/s 是 lzo 20.5