Hive's DDL data definition

create database

  1. Create a database, the default storage path of the database on HDFS is /user/hive/warehouse/*.db
    hive (default)> create database db_hive;
  2. To avoid errors in the database to be created, increase the if not exists judgment. (standard writing)

    hive> create database db_hive;
    FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Database db_hive already exi
    sts
    hive (default)> create database if not exists db_hive;
  3. Create a database and specify the location where the database is stored on HDFS
    hive (default)> create database db_hive2 location '/db_hive2.db';

Modify the database

Users can use the ALTER DATABASE command to set key-value pair property values ​​for DBPROPERTIES of a database to describe the property information of the database. Other metadata information about the database is immutable, including the database name and the directory location where the database is located.

alter database db_hive set dbproperties("mynick"="lzl");

View modification results in mysql

hive> desc database extended db_hive; // Note that the extended keyword should be added, which means to view the additional information of the database, which includes our own definition


query database

show database

  1. Show databases     hive> show databases;
  2. Filter the database that shows the query     hive> show databases like 'db_hive*';

View database details

  1. Display database information     hive> desc database db_hive;
  2. Show database details, extended     hive> desc database extended db_hive;

Use database
    hive (default) > use db_hive;

delete database

  1. Drop empty database
    hive>drop database db_hive2;
  2. If the deleted database does not exist, it is best to use if exists to determine whether the database exists
    hive> drop database if exists db_hive2;
  3. If the database is not empty, you can use the cascade command to force delete
    hive> drop database db_hive cascade; // cascade force delete keyword

create table

table creation syntax

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]

    [STORED AS file_format]

    [LOCATIONhdfs_path]

Field Explanation

  • CREATE TABLE creates a table with the specified name. Throws an exception if a table with the same name already exists; the user can ignore this exception with the IF NOT EXISTS option
  • The EXTERNAL keyword allows users to create an external table and specify a path (LOCATION) to the actual data when creating the table. When Hive creates an internal table, it will move the data to the path pointed to by the data warehouse; if an external table is created, only Record the path where the data is located, without making any changes to the location of the data. When deleting a table, the metadata and data of the internal table will be deleted together, while the external table only deletes the metadata, not the data
  • COMMENT: Add comments to tables and columns
  • PARTITIONED BY create a partitioned table
  • CLUSTERED BY creates a bucket table
  • SORTED BY is not commonly used
  • ROW FORMAT

    DELIMITED [FIELDS TERMINATED BY char][COLLECTION ITEMS TERMINATED BY char]

           [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char]

       | SERDE serde_name [WITH SERDEPROPERTIES(property_name=property_value, property_name=property_value, ...)]
    Users can customize the SerDe or use the built-in SerDe when creating a table. If ROW FORMAT or ROW FORMAT DELIMITED is not specified, the built-in SerDe will be used. When creating a table, the user also needs to specify columns for the table. When specifying the columns of the table, the user will also specify a custom SerDe. Hive determines the specific column data of the table through SerDe.
  • STORED AS specifies the storage file type

    Commonly used storage file types: SEQUENCEFILE (binary sequence file), TEXTFILE (text), RCFILE (column storage format file)

    If the file data is plain text, STORED AS TEXTFILE can be used. If the data needs to be compressed, use STORED ASSEQUENCEFILE
  • LOCATION: Specifies the storage location of the table on HDFS
  • LIKE allows users to copy the existing table structure, but does not copy the data
Management Tables
       The tables created by default are so-called management tables, sometimes referred to as internal tables. Because of this kind of table, Hive will (more or less) control the life cycle of the data. By default, Hive stores data for these tables in subdirectories of the directory defined by the configuration item hive.metastore.warehouse.dir (for example, /user/hive/warehouse). When we delete a management table, Hive also deletes the data in this table. Management tables are not suitable for sharing data with other tools.
Case practice
  1. normal create table
    create table if not exists student2(
    id int, name string
    )
    row format delimited fields terminated by '\t'
    stored as textfile
    location '/user/hive/warehouse/student2';
  2. 根据查询结果创建表(查询的结果会添加到新创建的表中)
    create table if not exists student3 as select id, name from student;
  3. 根据已经存在的表结构创建表
    create table if not exists student4 like student;
  4. 查询表的类型
    hive (default)> desc formatted student2;

外部表

       因为表是外部表,所有Hive并非认为其完全拥有这份数据。删除该表并不会删除掉这份数据,不过描述表的元数据信息会被删除掉。

管理表和外部表的使用场景:每天将收集到的网站日志定期流入HDFS文本文件。在外部表(原始日志表)的基础上做大量的统计分析,用到的中间表、结果表使用内部表存储,数据通过SELECT+INSERT进入内部表。
案例实操
    分别创建部门和员工外部表,并向表中导入数据。

  1. 原始数据
    emp.txtdept.txt
  2. 建表语句
    create external table if not exists default.dept(
    deptno int,
    dname string,
    loc int
    )
    row format delimited fields terminated by '\t';
    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 '\t';
  3. 查看创建的表
    hive (default)> show tables;
  4. Import data into an external table
    /*Note that /opt/module/datas/dept.txt is modified according to your own path*/
    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;
    query
    hive (default)> select * from emp;
    hive (default)> select * from dept;
  5. After viewing the table formatted data
    hive (default)> desc formatted dept; you will see that the table type is as follows:
    Table Type: EXTERNAL_TABLE

Partition Table

The partition table actually corresponds to an independent folder on the HDFS file system, and the folder contains all the data files of the partition. A partition in Hive is a sub-directory, which divides a large data set into smaller data sets according to business needs. When querying, the specified partition required by the query is selected through the expression in the WHERE clause, and the query efficiency will be greatly improved.

Partition table basic operations

  1. Introduce partition table (logs need to be managed according to date)
  2. Create Partitioned Table Syntax
    hive (default)> create table dept_partition(
                   deptno int, dname string, loc string
                   )
                   partitioned by (month string)
                   row format delimited fields terminated by '\t';
  3. 加载数据到分区表中
    hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201804');
    hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201805');
    hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201806');    
  4. 查询分区表中数据
    单分区查询    hive (default)> select * from dept_partition where month='201805';
    多分区联合查询
        hive (default)> select * from dept_partition where month='201805'
                  union
                  select * from dept_partition where month='201804'
                  union
                  select * from dept_partition where month='201806';
  5. 增加分区
    创建单个分区
    hive (default)> alter table dept_partition add partition(month='201807') ;
    同时创建多个分区
    hive (default)>  alter table dept_partition add partition(month='201808') partition(month='201809');
  6. Delete partition
    delete a single partition
    hive (default)> alter table dept_partition drop partition (month='201804');
    delete multiple partitions at the same time
    hive (default)> alter table dept_partition drop partition (month='201805'), partition (month= '201806');
  7. View how many partitions the partition table has
    hive>show partitions dept_partition;
  8. View the partition table structure
    hive>desc formatted dept_partition;
Partition Table Considerations
  1. Create a multilevel partitioned table
    hive (default)> create table dept_partition2(
                   deptno int, dname string, loc string
                   )
                   partitioned by (month string, day string)
                   row format delimited fields terminated by '\t';
  2. 正常的加载数据
    加载数据到二级分区表中
    hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition2 partition(month='201805', day='13');
    查询分区数据
    hive (default)> select * from dept_partition2 where month='201805' and day='13';
  3. 把数据直接上传到分区目录上,让分区表和数据产生关联的三种方式
    方式一:上传数据后修复
        
    上传数据 hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201805/day=1;
    hive (default)> dfs -put /opt/module/datas/dept.txt  /user/hive/warehouse/dept_partition2/month=201805/day=1;
    执行修复命令 hive>msck repair table dept_partition2;
    查询数据 hive (default)> select * from dept_partition2 where month='201805' and day='1';
    方式二:上传数据后添加分区

    上传数据 hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201805/day=1;
    hive (default)> dfs -put /opt/module/datas/dept.txt  /user/hive/warehouse/dept_partition2/month=201805/day=1;
    执行添加分区 hive (default)> alter table dept_partition2 add partition(month='201805', day='1');
    查询数据 hive (default)> select * from dept_partition2 where month='201805' and day='1';
    方式三:上传数据后load数据到分区

    创建目录 hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201805/day=1;
    上传数据 hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table dept_partition2
    partition(month='201805',day='1');
    查询数据 hive (default)> select * from dept_partition2 where month='201805' and day='1';

修改表

  1. 重命名表    ALTER TABLE table_name RENAME TO new_table_name
    实操案例: hive (default)> alter table dept_partition2 rename to dept_partition3;
  2.  增加、修改和删除表分区
    见分区表基本操作
  3. 增加/修改/替换列信息
    更新列
    ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment] [FIRST|AFTER column_name]
    增加和替换列
    ALTER TABLE table_name ADD|REPLACE COLUMNS (col_name data_type [COMMENT col_comment], ...) 
    注:ADD是代表新增一字段,字段位置在所有列后面(partition列前),REPLACE则是表示替换表中所有字段。
    实操案例
    (1)查询表结构
             hive>desc dept_partition;
    (2)添加列
             hive (default)> alter table dept_partition add columns(deptdesc string);
    (3)更新列
             hive (default)> alter table dept_partition change column deptdesc desc int;
    (4)替换列
             hive (default)> alter table dept_partition replace columns(deptno string, dname string, loc string);

删除表

hive (default)> drop table dept_partition;  //  可以用于删除空表

hive (default)> drop table dept_partition cascade;  // 可以用于删除有数据的表

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325853703&siteId=291194637
Recommended