码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • hive3


    这里写目录标题

    • Hive常用交互命令
    • 其他命令
    • Hive数据类型
    • 类型转换
      • 隐式转换规则
      • 可以使用 CAST 操作显示进行数据类型转换
    • DDL
      • 创建一个数据库,指定数据库在 HDFS 上存放的位置
      • 创建表
        • 创建管理表
        • 创建外部表
      • DML
      • 加载数据
        • 向表中加载数据
        • 通过查询语句向表中插入数据
        • 查询语句中创建表并加载数据(As Select)
      • 排序
        • 使用 ORDER BY 子句排序
        • Sort By
        • 分区(Distribute By)
        • Cluster By
      • 分区

    Hive常用交互命令

    1. “-e”不进入 hive 的交互窗口执行 sql 语句

    bin/hive -e “select id from student;”

    1. “-f”执行脚本中 sql 语句

    bin/hive -f hivef.sql
    执行文件中的 sql 语句并将结果写入文件中
    bin/hive -f hivef.sql > hive_result.txt

    其他命令

    1. 在hive cli命令中执行hdfs 命令
      dfs -ls /;
    2. 查看hive历史命令
      在 /root 或者/home/atguigu
      查看.hivehistory文件
      cat .hivehistory

    Hive数据类型

    TINYINT	SMALINT	INT		BIGINT
    BOOLEAN
    FLOAT DOUBLE
    STRING
    TIMESTAMP
    BINARY
    
    集合类型
    
    STRUCT				通过 . 方式访问
    MAP														通过['key'] 访问
    ARRAY														通过[1]访问
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    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

    create table test(
    name string,
    friends array,
    children map,
    address structstreet:string,city:string
    )
    row format delimited fields terminated by “,”
    collection items terminated by “_”
    map keys terminated by “:”
    lines terminated by “\n”;

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

    测试 load data local inpath ‘/opt/module/hive/datas/test.txt’ into table test;

    select friends[0] as f1,children[‘xiao song’] as xiaosong,address.street as st from test;

    类型转换

    隐式转换规则

    (1)任何整数类型都可以隐式地转换为一个范围更广的类型,如 TINYINT 可以转换成
    INT,INT 可以转换成 BIGINT。
    (2)所有整数类型、FLOAT 和 STRING 类型都可以隐式地转换成 DOUBLE。
    (3)TINYINT、SMALLINT、INT 都可以转换为 FLOAT。
    (4)BOOLEAN 类型不可以转换为任何其它的类型。

    可以使用 CAST 操作显示进行数据类型转换

    例如 CAST(‘1’ AS INT)将把字符串’1’ 转换成整数 1;如果强制类型转换失败,如执行 CAST(‘X’ AS INT),表达式返回空值 NULL。

    DDL

    创建一个数据库,指定数据库在 HDFS 上存放的位置

    create database db_hive2 location ‘/db_hive2.db’;

    创建表

    创建管理表

    create table test_clazz(
    id int,
    name string)
    row format delimited fields terminated by ","
    lines terminated by "\n"
    location "/home/atguigu/clazz.txt";
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    创建外部表

    create external table 删除表时,不会删除数据,默认不加 是管理表,会删除数据
    
    alter table student2 set tblproperties('EXTERNAL'='TRUE'); 修改表类型
    
    • 1
    • 2
    • 3

    DML

    加载数据

    向表中加载数据

    load data [local] inpath ‘数据的 path’ [overwrite] into table
    student [partition (partcol1=val1,…)];

    通过查询语句向表中插入数据

    insert overwrite table student_par
    select id, name from student where month=‘201709’;

    查询语句中创建表并加载数据(As Select)

    create table if not exists student3 as select id, name from student;

    排序

    使用 ORDER BY 子句排序

    Sort By

    Sort By:对于大规模的数据集 order by 的效率非常低。在很多情况下,并不需要全局排 序,此时可以使用 sort by
    
    • 1

    分区(Distribute By)

    分区内排序
    
    • 1

    Cluster By

    当 distribute by 和 sorts by 字段相同时,可以使用 cluster by 方式。
    
    • 1

    分区

    增加分区

    alter table dept_partition add partition(day=‘20200404’);
    alter table dept_partition add partition(day=‘20200405’) partition(day=‘20200406’);

  • 相关阅读:
    智能崛起的简约性和自一致性原理(上)
    我的十年编程路 2014年篇
    Flask, Access-Control-Allow-Origin 跨域请求的解决方法
    FA18# 中间件稳定性治理内容提点
    GIT_工作3年后对GIT重新总结学习
    Spring注解详解:@ComponentScan自动扫描组件使用
    YOLOv8改进Swin Transformer:在基础SwinTransformer结构的基础上进行多种改进结构,集成Transformer和CNN的优势
    4 AI scams to be aware of
    山西电力市场日前价格预测【2023-10-24】
    图像特征之SIFT
  • 原文地址:https://blog.csdn.net/zhouhe_/article/details/127849389
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号