Hive基础(三)

复杂类型、查询

知识回顾

  1. 分桶:把具有海量数据的数据文件拆分成多个文件
  2. 语法:clustered by (字段名) into 桶数 buckets;
    1. 导入数据:查询HDFS页面数据效果:多个文件
    2. hsahcode % 桶数=? 相同的放在同一个桶

SerDe机制

  1. 什么是SerDe机制

序列化:对象或数据转换为字节码

反序列化:字节码转换为对象或数据

  1. 作用

    在hive中,支持的复杂数据类型有:

image-20230913104143966

数据类型 表示 定义类型 示例
array 数组 array [1,2,3]
struct 结构 struct<子列名 类型,子列名 类型,……> {“张三”,18}
map map集合 map<key类型,value类型> {“name”:”zhangshan”, “age”:18}
3. 使用
  1. 字段之间分割符:row format delimited fields terminated by “,”;
  2. 集合/数组之间分割符:collection items terminated by “,”;
  3. map映射K-V之间分割符:map keys terminated by “,”;
  4. 行数据之间分隔符:lines terminated by “,”;
  1. Hive使用SerDe(和FileFormat)读取和写入行对象

    1
    2
    3
    4
    5
    # read文件
    hdfs files --> InputFilesFormat --><key,value> --> deserializer --> Row object;

    # 写文件
    Row object --> Serializer --> <key,value> --> OutputFileFormat -->HDFS files

复杂类型

  1. 创建表

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    -- 创建表
    create table tset01(
    name string,
    citys array<string>
    )
    row format delimited
    fields terminated by "\t"
    collection items terminated by ","
    map keys terminated by ","
    lines terminated by ",";
  2. array数组

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    create table complex_array(
    name string,
    city_array array<string>
    )
    row format delimited
    fields terminated by "\t" --字段间分割
    collection items terminated by ",";

    -- 导入数据
    load data local inpath '/root/day09_hive/array/01-data_for_array_type.txt' into table complex_array;

    select * from complex_array;

    -- 获取数组数据
    select *,city_array[0] from complex_array; --获取数组中的值
    select *,size(city_array) from complex_array; --获取数组大小
    select *,array_contains(city_array,"beijing") from complex_array; --查看数组中是否包含beijing
  3. struct结构

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    -- 创建表
    create table complex_struct(
    name string,
    city_struct struct<name:string,age:int>
    )
    row format delimited
    fields terminated by "#" --字段间分割
    collection items terminated by ":";

    -- 查看数据
    select * from complex_struct;

    -- 加载数据
    load data inpath '/input/02-data_for_struct_type.txt' into table complex_struct;

    -- 查看数据
    select *,city_struct.name,city_struct.age from complex_struct;

  4. map集合

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    -- 建表
    create table complex_map(
    id int,
    name string,
    homeInfo map<string,string>,
    age int
    )
    row format delimited
    fields terminated by "," --字段间分割
    collection items terminated by "#"
    map keys terminated by ":";

    -- 查看数据
    select * from complex_map;

    /*
    -- 常用函数
    变量名[key] :
    map_keys[变量名] :
    map_values[变量名] :
    array_contains(数组名, value)
    */
    select name,homeInfo["father"],homeInfo["mother"] from complex_map;
    select map_keys(homeInfo),map_values(homeInfo) from complex_map;
    select * from complex_map where array_contains(map_keys(homeInfo),"sister");

查询

  1. 基础查询

    1. 简单查询

      1
      2
      -- 去除重复 :distinct、group by、partition by
      -- 运算操作:范围、比较运算、逻辑运算、,模糊查询、非空
    2. 聚合查询

      1
      2
      3
      4
      5
      count()
      avg()
      max()
      min()
      sum()
    3. 排序查询

      1
      order by asc \ desc
    4. 分页查询

      1
      2
      3
      4
      limit m n;
      -- 每页显示10条,第五页
      -- limit (n-1)*10 10
      limit 40 10;
    5. 分组查询

      1
      2
      3
      4
      5
      6
      7
      8
      -- sql执行顺序
      from --> group by --> select ---> having

      -- having 和 where的异同点
      相同点:都可以进行过滤数据
      不同点:where的执行优先于having
      having 只能和group by配合使用
      having 比select优先级还低,所以可以使用聚合函数
  2. 进阶查询

    1. 多表join查询
    2. 区别SQL的join查询
      1. 全外连接(满连接):full join
      2. 左半开连接:left semi join,只取左表数据