zoukankan      html  css  js  c++  java
  • Hive—内部表与外部表

    一、区别

    1.建表语句不同。外部表建表被external修饰;内部表没有,默认为内部表。
    2.存储位置不同。内部表数据存储的位置是hive.metastore.warehouse.dir(默认:/user/hive/warehouse),外部表数据的存储位置由自己制定;
    3.管理方式不同。内部表数据由hive本身管理,外部表数据由HDFS管理,因此删除表的时候不同,内部表会把所有的数据都删除,外部表只会删除掉MySQL中的元数据信息。

    二、内部表

    (1)普通创建表

    hive (default)> create table if not exists student2(id int, name string)row format delimited fields terminated by '	'
                  > stored as textfile location '/user/hive/warehouse/student2';
    OK
    Time taken: 0.563 seconds

    (2)根据查询结果创建表(查询的结果会添加到新创建的表中)

    hive (default)> create table if not exists student3 as select id, name from student;
    OK
    Time taken: 0.029 seconds

    (3)根据已经存在的表结构创建表

    hive (default)> create table if not exists student4 like student;
    OK
    Time taken: 0.466 seconds

    (4)查询表的类型

    hive (default)> desc formatted student2;                   
    # Detailed Table Information             
    Table Type:             MANAGED_TABLE            

    三、外部表

    分别创建部门和员工外部表,并向表中导入数据。

    (1)原始数据

    dept.txt文本

    10    ACCOUNTING    1700
    20    RESEARCH    1800
    30    SALES    1900
    40    OPERATIONS    1700

    emp.txt文本

    7369 SMITH CLERK 7902 1980-12-17 800.00 20
    7499 ALLEN SALESMAN 7698 1981-2-20 1600.00 300.00 30
    7521 WARD SALESMAN 7698 1981-2-22 1250.00 500.00 30
    7566 JONES MANAGER 7839 1981-4-2 2975.00 20
    7654 MARTIN SALESMAN 7698 1981-9-28 1250.00 1400.00 30
    7698 BLAKE MANAGER 7839 1981-5-1 2850.00 30
    7782 CLARK MANAGER 7839 1981-6-9 2450.00 10
    7788 SCOTT ANALYST 7566 1987-4-19 3000.00 20
    7839 KING PRESIDENT 1981-11-17 5000.00 10
    7844 TURNER SALESMAN 7698 1981-9-8 1500.00 0.00 30
    7876 ADAMS CLERK 7788 1987-5-23 1100.00 20
    7900 JAMES CLERK 7698 1981-12-3 950.00 30
    7902 FORD ANALYST 7566 1981-12-3 3000.00 20
    7934 MILLER CLERK 7782 1982-1-23 1300.00 10

    (2)建表语句

    创建部门表

    create external table if not exists default.dept(
    deptno int,
    dname string,
    loc int)
    row format delimited fields terminated by '	';

    创建员工表

    create external table if not exists default.emp(
    empno int,
    ename string,
    job string,
    mgr int,
    hiredate string,
    sal double,
    comm double,
    deptno int)
    row format delimited fields terminated by '	';

    (3)查看创建的表

    hive (default)> show tables;
    OK
    tab_name
    dept
    emp

    (4)向外部表中导入数据

    导入数据

    hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept;
    hive (default)> load data local inpath '/opt/module/datas/emp.txt' into table default.emp;

    查询结果

    hive (default)> select * from emp;
    hive (default)> select * from dept;

    (5)查看表格式化数据

    hive (default)> desc formatted dept;
    Table Type:             EXTERNAL_TABLE

    四、内部表与外部表转换

    (1)查询表的类型

    hive (default)> desc formatted student2;
    Table Type:             MANAGED_TABLE

    (2)修改内部表student2为外部表

    alter table student2 set tblproperties('EXTERNAL'='TRUE');

    (3)查询表的类型

    hive (default)> desc formatted student2;
    Table Type:             EXTERNAL_TABLE

    (4)修改外部表student2为内部表

    alter table student2 set tblproperties('EXTERNAL'='FALSE');

    (5)查询表的类型

    hive (default)> desc formatted student2;
    
    Table Type:             MANAGED_TABLE

    注意:(‘EXTERNAL’=’TRUE’)和(‘EXTERNAL’=’FALSE’)为固定写法,区分大小写!

    源于atgugui视频

  • 相关阅读:
    二 web爬虫,scrapy模块以及相关依赖模块安装
    一 web爬虫,requests请求
    Linux 常用命令大全
    HTTP响应状态码参考
    python-进程,线程,协程
    SVN图形管理工具-Submint
    python-Socket网络编程
    python基础-pickle与shelve
    python-面向对象
    10分钟看懂, Java NIO 底层原理
  • 原文地址:https://www.cnblogs.com/zs-chenkang/p/14388587.html
Copyright © 2011-2022 走看看