[关闭]
@awsekfozc 2016-10-28T14:58:28.000000Z 字数 3828 阅读 1862

Hive DDL

Hive

数据库操作

基本语法

CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name
  [COMMENT database_comment]
  [LOCATION hdfs_path]
  [WITH DBPROPERTIES (property_name=property_value, ...)];
  说明:
  - IF NOT EXISTS:如果不存在则创建
  - COMMENT:注释
  - LOCATION:数据库存放目录
  - WITH DBPROPERTIES:拓展信息,key/value

创建数据库

  1. hive (default)> CREATE DATABASE testdb;

创建数据库判断数据库是否存在

  1. hive (default)> CREATE DATABASE IF NOT EXISTS testdb;

显示现有数据库

  1. hive (default)> show databases;

条件查询现有数据库

  1. hive (default)> show databases like 't.*';
  2. OK
  3. database_name
  4. testdb
  5. Time taken: 0.053 seconds, Fetched: 1 row(s)

创建指定存放目录位置数据库

  1. hive (default)> create database testdb_loc location '/user/hive/warehouse/testdb_loc';
  2. OK
  3. Time taken: 0.386 seconds
  4. hive (default)> show databases;
  5. OK
  6. database_name
  7. default
  8. testdb
  9. testdb_loc
  10. Time taken: 0.024 seconds, Fetched: 3 row(s)
  11. hive (default)>

创建数据库时,添加注释信息

  1. hive (default)> create database testdb_zhushi
  2. > COMMENT 'zhushi';
  3. hive (default)> DESCRIBE DATABASE testdb_zhushi;
  4. OK
  5. db_name comment location parameters
  6. testdb_zhushi zhushi hdfs://hadoop.zc.com:8020/user/hive/warehouse/testdb_zhushi.db zc
  7. Time taken: 0.015 seconds, Fetched: 1 row(s)

创建数据库,添加扩展信息

  1. hive (default)> create database testdb_otherinfo
  2. > WITH DBPROPERTIES ('creator' = 'zhangcheng', 'date' = '2015-11-30');

删除数据库

  1. hive (default)> drop database if exists testdb_zhushi;

删除数据库,当数据库存在表时,先要删除表才能删除数据库

  1. drop database if exists testdb CASCADE;

表操作

语法基本结构

CREATE [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name
  [(col_name data_type [COMMENT col_comment], ...)]
  [COMMENT table_comment]
  [PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
  [
   [ROW FORMAT row_format] 
   [STORED AS file_format]
  ]
  [LOCATION hdfs_path]
  [AS select_statement];  
  说明:
  - EXTERNAL:外部表
  - IF NOT EXISTS:表不存在创建
  - db_name:表所属数据库
  - COMMENT col_comment:列注释
  - COMMENT table_comment:表注释
  - PARTITIONED BY:分区字段
  - ROW FORMAT row_format:行的数据格式
  - STORED AS file_format:文件存储格式
  - STORED AS file_format
  - LOCATION hdfs_path:存放路径
  - AS select_statement:查询语句为结果集

CREATE TABLE [IF NOT EXISTS] [db_name.]table_name
  LIKE existing_table_or_view_name
  [LOCATION hdfs_path];
  说明:
  - IF NOT EXISTS:表不存在创建
  - db_name:表所属数据库
  - existing_table_or_view_name:结果集为存在的表或者师徒
  - LOCATION hdfs_path:存放路径

创建表,自定义表结构

  1. hive (testdb)> create table if not exists testdb.pepole(
  2. > id int COMMENT 'user id',
  3. > name string COMMENT 'user name',
  4. > phone string COMMENT 'user phone')
  5. > COMMENT 'pepele info'
  6. > ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
  7. > STORED AS textfile ;
  8. hive (testdb)> show tables;
  9. OK
  10. tab_name
  11. pepole
  12. Time taken: 0.02 seconds, Fetched: 1 row(s)

创建表,like

  1. hive (testdb)> create table if not exists testdb.pepole_like like testdb.pepole;
  2. hive (testdb)> show tables;
  3. OK
  4. tab_name
  5. pepole
  6. pepole_like
  7. Time taken: 0.015 seconds, Fetched: 2 row(s)

创建表,select

  1. hive (testdb)> create table if not exists testdb.pepole_select as select id,name from testdb.pepole;
  2. hive (testdb)> show tables;
  3. OK
  4. tab_name
  5. pepole
  6. pepole_like
  7. pepole_select
  8. Time taken: 0.015 seconds, Fetched: 3 row(s)

创建表,外部表,EXTERNAL

  1. hive (testdb)> CREATE EXTERNAL TABLE IF NOT EXISTS testdb.emp_ext(
  2. > empno int,
  3. > ename string,
  4. > job string,
  5. > mgr int,
  6. > hiredate string,
  7. > sal double,
  8. > comm double,
  9. > deptno int
  10. > )
  11. > ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
  12. > STORED AS textfile ;

分区表,partition

功能

创建

  1. hive (testdb)> CREATE TABLE IF NOT EXISTS testdb.emp_partition(
  2. > empno int,
  3. > ename string,
  4. > job string,
  5. > mgr int,
  6. > hiredate string,
  7. > sal double,
  8. > comm double,
  9. > deptno int
  10. > )
  11. > PARTITIONED BY (month string, day string)
  12. > ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
  13. > STORED AS textfile ;

加载数据

  1. load data local inpath '/opt/datas/emp.txt' overwrite into table testdb.emp_partition partition(month = '201512',day = '01');
  2. load data local inpath '/opt/datas/emp.txt' overwrite into table testdb.emp_partition partition(month = '201512',day = '02');

QQ截图20151202202503.png-4.9kB

查询数据

  1. hive (testdb)> select * from emp_partition where month='201512' and day='02';

QQ截图20151202202809.png-41.2kB

alert partition

  1. hive (testdb)> alter table emp_partition add partition(month='201512',day='03');

删除表

DROP TABLE [IF EXISTS] table_name ;

清空表

TRUNCATE TABLE table_name [PARTITION partition_spec];

在此输入正文

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注