文章目录

  • Hive基本操作
    • Hive查看SQL解析计划
    • Hive建表
      • 建表1:全部使用默认建表方式
    • Hive 内部表 (Managed tables)
      • 指定location (这种方式也比较常用)
        • formatted 查看该表的结构化数据,但并不列出表中的数据
        • 将本地数据上传到HDfS上
    • Hive 外部表 (External tables)
      • 指定location (这种方式也比较常用)
        • 将本地数据上传到HDfS上
    • Hive 内部表(Managed tables)与 外部表(External tables)的区别
      • 建表3:指定存储格式
      • 建表4:create table xxxx as select_statement(SQL语句) (这种方式比较常用)
      • 建表5:create table xxxx like table_name 只想建表,不需要加载数据

Hive基本操作

Hive查看SQL解析计划

  我们都知道,hive在执行的时候会把所对应的SQL语句都会转换成MapReduce代码执行,但是具体的MR执行信息我们怎样才能看出来呢?这里就用到了explain关键字,他可详细的表示出在执行对应语句所对应的MR代码。语法格式如下;extended关键字可以更加详细的列举出代码的执行过程。

EXPLAIN [EXTENDED|DEPENDENCY|AUTHORIZATION] query

  explain会把查询语句转化成stage组成的序列,主要由三方面组成:

  1. 查询的抽象语法树
  2. plane中各个stage的依赖情况
  3. 每个阶段的具体描述:描述具体来说就是显示出对应的操作算子和与之操作的对应的数据,例如查询select算子,filter算子,fetch算子等等。

  下面来看一个具体的例子:

explain select * from students limit 10;
explain extended select * from students limit 10;
// extended 可选,可以打印更多细节
explain  select  a.id,a.name,a.clazz,t1.sum_score
from(select  id,sum(score) as sum_scorefrom score group by id
)t1 right join (select  id,name,'文科一班' as clazzfrom studentswhere clazz = '文科一班'
) a
on t1.id = a.id
order by t1.sum_score desc
limit 10;

Hive建表

  格式:

CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name// 定义字段名,字段类型[(col_name data_type [COMMENT col_comment], ...)]// 给表加上注解[COMMENT table_comment]// 分区[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]// 分桶[CLUSTERED BY (col_name, col_name, ...) // 设置排序字段 升序、降序[SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS][// 指定设置行、列分隔符 [ROW FORMAT row_format] // 指定Hive储存格式:textFile、rcFile、SequenceFile 默认为:textFile[STORED AS file_format]| STORED BY 'storage.handler.class.name' [ WITH SERDEPROPERTIES (...) ]  (Note:  only available starting with 0.6.0)]// 指定储存位置[LOCATION hdfs_path]// 跟外部表配合使用,比如:映射HBase表,然后可以使用HQL对hbase数据进行查询,当然速度比较慢[TBLPROPERTIES (property_name=property_value, ...)]  (Note:  only available starting with 0.6.0)[AS select_statement]  (Note: this feature is only available starting with 0.5.0.)

建表1:全部使用默认建表方式

// 必选,指定列分隔符
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
create table students1
(id bigint,name string,age int,gender string,clazz string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

Hive 内部表 (Managed tables)

指定location (这种方式也比较常用)

LOCATION '/input2'

  指定Hive表的数据的存储位置,一般在数据已经上传到HDFS,想要直接使用,会指定Location,通常Locaion会跟外部表一起使用,内部表一般使用默认的location

create table students_internal
(id bigint,name string,age int,gender string,clazz string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
LOCATION '/input2';

formatted 查看该表的结构化数据,但并不列出表中的数据

desc formatted students_internal;

将本地数据上传到HDfS上

dfs -put /usr/local/soft/data/students.txt /input2/;
select * from students_internal limit 10;

有数据了

Hive 外部表 (External tables)

指定location (这种方式也比较常用)

create external table students_external
(id bigint,name string,age int,gender string,clazz string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
LOCATION '/input3';

将本地数据上传到HDfS上

dfs -put /usr/local/soft/data/students.txt /input3/;
select * from students_external limit 10;

有数据了

Hive 内部表(Managed tables)与 外部表(External tables)的区别

//查看所有表
show tables;//删除内部表students_internal
drop table students_internal;
show tables;//删除外部表students_external
drop table students_external;
show tables;

  当我们删除内部表的时候,将hdfs://master:9000/input2移动到回收站,目录、表、数据一起被删除。

  当我们删除外部表的时候目录、表、数据并没有被删除。

可以看出,删除内部表的时候,表中的数据(HDFS上的文件)会被同表的元数据一起删除

删除外部表的时候,只会删除表的元数据,不会删除表中的数据(HDFS上的文件)

一般在公司中,使用外部表多一点,因为数据可以需要被多个程序使用,避免误删,通常外部表会结合location一起使用

外部表还可以将其他数据源中的数据 映射到 hive中,比如说:hbase,ElasticSearch…

设计外部表的初衷就是让表的元数据与数据解耦


建表3:指定存储格式

stored as rcfile;

  指定储存格式为rcfile,inputFormat:RCFileInputFormat,outputFormat:RCFileOutputFormat,如果不指定,默认为textfile,注意:除textfile以外,其他的存储格式的数据都不能直接加载,需要使用从表加载的方式。

create table students2
(id bigint,name string,age int,gender string,clazz string
)
row format delimited fields terminated by ','
stored as rcfile;

建表4:create table xxxx as select_statement(SQL语句) (这种方式比较常用)

//给students1表上传数据
dfs -put /usr/local/soft/data/students.txt /user/hive/warehouse/stu_student.db/students1/;
create table students4 as select * from students1;
select * from students4 limit 10;

建表5:create table xxxx like table_name 只想建表,不需要加载数据

create table students5 like students1;
select * from students5 limit 10;

到底啦!关注靓仔学习更多的大数据知识。( •̀ ω •́ )✧

3、Hive数据仓库——建表语句相关推荐

  1. hive建表语句_Hive数据如何同步到MaxCompute之实践讲解

    摘要:本次分享主要介绍 Hive数据如何迁移到MaxCompute.MMA(MaxCompute Migration Assist)是一款MaxCompute数据迁移工具,本文将为大家介绍MMA工具的 ...

  2. Hive的核心概念以及建库建表语句

    hive的数据类型: Hive表中的列支持以下基本数据类型: integers(整型) : TINYINT:1字节的有符号整数: SMALLINT:2字节的有符号整数: INT:4字节的有符号整数: ...

  3. 使用java代码编写脚本,把oracle建表语句变成hive建表语句

    使用java代码编写脚本,把oracle建表语句变成hive建表语句 java代码 测试oracle.sql 生成hive创表语句 java代码 import java.io.File; import ...

  4. ClickHouse MergeTree表引擎和建表语句

    1. Clickhouse使用场景 ClickHouse是由俄罗斯Yandex公司开发的.面向列的数据库管理系统(DBMS),主要面向OLAP场景,用于在线分析处理查询,可以使用SQL查询实时生成数据 ...

  5. 获取impala下所有的数据库建表语句

    本博文介绍三种方法,推荐使用第三种,前两种都是尝试. 方法一: 现在的导出还是有缺陷的,导出的文件中还是存在其他不必要的信息 #!/bin/bash ##获取数据库 databases=$(hive ...

  6. linux下db2建表语句,DB2建表语句

    db2 => create table test (name char(8) not null primary key,depid smallint,pay bigint) DB20000I S ...

  7. 自动获取mysql建表语句_脚本工具---自动解析mysql建表语句,生成sqlalchemy表对象声明...

    常规建表语句: CREATE TABLE `test_table` ( `id` int(11) NOT NULL, `name` char(64) NOT NULL, `password` char ...

  8. 05_ClickHouse、MergeTree系列引擎概述与存储结构、建表模板、建表语句、MergeTree设置、建表示例、数据存储、数据片段(data part)

    2.MergeTree系列引擎概述与存储结构 2.1.建表模板 2.2.建表语句 2.3.MergeTree设置 2.4.建表示例 2.5.数据存储 2.6.数据片段(data part) 2.Mer ...

  9. idea使用dababase tools时导出db2建表语句,索引显示错误

    idea导出db2的建表语句问题 问题:(本次只是简单记录一下问题,防止以后再次遇到) 1.使用idea创建db2表索引是,不管下边这个Unique是否选择,等创建完成之后重新进来查看(或者用idea ...

最新文章

  1. SAP QM QE02 修改检验结果,报错 -No characteristics were found–
  2. Java 11将于本月25日发布,新特性一览
  3. Hazelcast介绍与使用
  4. 惊呆了!竟然还有这样的操作!
  5. android 展示星期方式,Android显示从一周到另一周的日期(星期四至星期四)
  6. 单源最短路径之迪杰斯特拉算法(C语言)
  7. java单元测试面试,Java必备!JUnit面试题和答案汇总
  8. 在线报刊html代码,数字报纸HTML版本
  9. 腾讯区块链团队首次换将,蔡弋戈将变动职务
  10. 在计算机英语中 memory的中文意思是,Memory是什么意思,memory什么意思中文
  11. 语义分割制作自己的数据集
  12. echarts圆柱形带背景图
  13. video.js API 详解
  14. C++ Pointer指针
  15. AVAudioPlayer常用属性
  16. HBase (一) --------- HBase 简介
  17. 远程教育英语计算机统考试题,2020年远程网络教育计算机应用基础统考题库原题真题(完整版)...
  18. 【PyTorch】11 聊天机器人实战——Cornell Movie-Dialogs Corpus电影剧本数据集处理、利用Global attention实现Seq2Seq模型
  19. 机器学习入门--唤起你的数学记忆
  20. 湖南省2021年上半年中小学教师资格考试(笔试)公告

热门文章

  1. Elasticsearch-02-es的restapi使用
  2. Object-C之文件操作
  3. 【项目精选】保险业务管理系统
  4. maven中DependencyManagement和Dependencies
  5. windows内存泄露定位方法
  6. swift之视频播放AVKIT、AVPlayerViewController、音频录制和播放
  7. ps play android下载地址,Ps Play apk下载|Ps Play安卓版下载 android1.0 - 跑跑车安卓网
  8. Linux下载工具photon,Photon v0.3.1 免费开源下载软件,替代迅雷的下载利器
  9. ubuntu设置.ttf和.otf字体
  10. 评析春江花月夜(River in spring and beautiful moon)