zoukankan      html  css  js  c++  java
  • Hive的DDL操作

    DDL:data definittion language  数据定义语言

      主要是定义或改变表的结构、数据类型、表之间的链接和约束等初始化操作

    DML:data manipulation language  数据操作语言

      主要是对数据库的数据进行增删改查操作,如select、insert、delete、update等

    一、对数据库的操作

      1.创建数据库并指定在hdfs的存储路径

      create database if not exists hive_db location '/hive_db';

      注释:不指定路径所创建的数据库默认存储路径为:“/user/hive/warehouse“

      create database if not exists hive_ab;

      2.查看数据库信息

        1)查看数据库结构

        desc database hive_db;

        2)添加数据库的描述信息

        alter database hive_db set dbproperties('creater'='wyh');

        3)查看数据库的拓展信息

        desc database extended hive_db;

      3.筛选查询数据库

      show database like 'hive*';

      4.删除数据库

      drop database wyh;

      drop database if exists hive_db;

    二、DDL操作

      hive中表的种类有很多,如管理表(Manager Table)、外部表(External Table)、分区表(Partition Table)、分桶表,下面我先介绍前三种表的定义、修改操作。

      1.管理表:Hive创建表时默认创建的就是管理表,也叫内部表,它不擅长数据共享,删除表后数据也会被删除。

     创建管理表

     create table if not exists emp1(id int,name string) row format delimited fields terminated by ' ';

     导入数据

     load data local inpath '/root/data/emp.txt' into table emp1;

     创建新管理表并从emp1表中导入name=wyh的该行数据

     create table if not exists emp2 as select * from emp1 where name = 'wyh';

     查询表的结构信息:

     desc formatted emp2;

      2.外部表:Hive不任务这张表拥有该数据,所以删除该表后数据不会删除,当再次创建结构与数据类型相同的表(无论是外部表还是管理表)时,数据会自动关联。但是若第二次创建的是管理表,再次删除后即使创建相同格式和数据类型的表数据将不再恢复!

      创建外部表

      create external table if not exists student(id int,name string) row format delimited fields terminated by ' ';

      导入数据

      load data local inpath '/root/data/student.txt' into table student;

      查看表结构

      desc formatted student;  (可以从Table Type看到:EXTERNAL_TABLE)

      删除表

      drop table if exists student;

      3.分区表分区表对应HDFS的一个独立的文件目录,目录下是该分区表所有分区的目录,每个分区目录下存储该分区内存储的数据。

      创建分区表

      create table dept_partitions(id int,name string,loc string) partitioned by(day string) row format delimited fiedls terminated by ' ';

      导入数据

      load data local inpath '/root/data/dept.txt' into table dept_partition partition(day='1001'); 

      (注意:不能直接导入数据,必须指定分区)

      添加分区

      alter table dept_partition add partition(day='1002');

      (添加该分区后该分区内是没有数据的)

      查询数据

      select * from dept_partition where day='1001';

      select * from dept_partition;

      删除分区

      alter table dept_partition drop partition(day='1002');

      alter table dept_partition drop partition(day='1001'),partition(day='1002');

    三、修改表

      1.修改表名

      alter table student rename to students;

      2.添加列

      alter table students add columns(age int,sex string);

      3.更新列(列名和列的数据类型)

      alter table student change column age birthday string;

      4.替换replace

      alter table students replace columns(descccc int);

      alter table students replace columns(id int,name string,loc string);

      注意:第二次替换后列的数据类型与第一次相同,数据会恢复!

  • 相关阅读:
    最新28个很棒的 jQuery 教程
    NetBeans 为PHP添加调试功能
    HTML5 存储API介绍
    PHP 变量判断
    jquery 与其它js 框架的解决办法
    从按下电源开关到bash提示符
    tar、gzip、unzip命令的详细使用方法
    Top命令中Load Average的含义
    Linux(BASH)命令搜索机制
    分析df和du的区别
  • 原文地址:https://www.cnblogs.com/HelloBigTable/p/10683329.html
Copyright © 2011-2022 走看看