• hive hive窗口打印库和表头、复杂类型ARRAY、MAP 和 STRUCT的定义和使用



    1. hive窗口打印库和表头

      在hive-site.xml中加入如下两个配置:

    <property>
        <name>hive.cli.print.header</name>
        <value>true</value>
    </property>
    <property>
        <name>hive.cli.print.current.db</name>
        <value>true</value>
    </property>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2. hive支持的类型

    data type文档

    Complex Types
    arrays: ARRAY<data_type> (Note: negative values and non-constant expressions are allowed as of Hive 0.14.)
    maps: MAP<primitive_type, data_type> (Note: negative values and non-constant expressions are allowed as of Hive 0.14.)
    structs: STRUCT<col_name : data_type [COMMENT col_comment], …>
    union: UNIONTYPE<data_type, data_type, …> (Note: Only available starting with Hive 0.7.0.)


    3. 复杂类型

    3.1 ARRAY、MAP 和 STRUCT

    数据类型描述语法
    STRUCT通过点符号访问元素内容。如列的数据类型是struct<street:string,city:string>,那么第1个元素可以通过字段名.street来引用STRUCT<col_name : data_type [COMMENT col_comment], ...>
    MAPMAP是一组键值对元组集合,使用数组表示法可以访问数据。如列的数据类型是map<string,int>,列某一行的值为{"xiao song":18,"xiaoxiao song":19},那么可以通过 字段名['xiao song']获取第一个元素对应的值 MAP<primitive_type, data_type>
    arrays数组是一组具有相同类型和名称的变量的集合。这些变量称为数组的元素,每个数组元素都有一个编号,编号从零开始。如数组值为["bingbing","lili"],那么第2个元素可以通过字段名[1]进行引用。ARRAY<data_type>

    3.2 复杂类型使用范例

      (1)创建本地测试文件test.txt

    songsong,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
    yangyang,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
    cz,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
    zx,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
    zmy,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
    tom,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
    mike,bingbing_lili,xiao song:18_xiaoxiao song:19,hui long guan_beijing
    jack,caicai_susu,xiao yang:18_xiaoxiao yang:19,chao yang_beijing
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

      (2)创建测试表test

    create table test(
    name string,
    friends array<string>,
    children map<string, int>,
    address struct<street:string, city:string>
    )
    row format delimited fields terminated by ','
    collection items terminated by '_'
    map keys terminated by ':'
    lines terminated by '\n';
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    字段解释:

    row format delimited fields terminated by ','  -- 列分隔符
    collection items terminated by '_'  	--MAP STRUCT 和 ARRAY 的分隔符(数据分割符号)
    map keys terminated by ':'			-- MAP中的key与value的分隔符
    lines terminated by '\n';				-- 行分隔符
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

      (3)导入文本数据到测试表

    load data local inpath '/home/hdfs/test.txt' into table test;
    
    • 1

    在这里插入图片描述
      (4)访问三种集合列里的数据,(ARRAY,MAP,STRUCT的访问方式)

    select friends[1],children['xiao song'],address.city from test;
    
    • 1

    在这里插入图片描述


  • 相关阅读:
    数据库基础面试第二弹
    PyTorch主要组成模块 | 数据读入 | 模型构建 | 模型初始化 | 损失函数 | 优化器 | 训练与评估
    第五章 数据库完整性
    目前为止的所有取数逻辑收集
    JavaScript事件监听器总结
    根据基站位置区识别码(LCA)和小区识别(CI)获取经纬度
    FBAR滤波器的工作原理及制备方法
    fabric.js介绍
    Pipeline 问题处理
    sql优化
  • 原文地址:https://blog.csdn.net/javahelpyou/article/details/125522994