@awsekfozc
2016-10-28T14:58:28.000000Z
字数 3828
阅读 1862
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
hive (default)> CREATE DATABASE testdb;
hive (default)> CREATE DATABASE IF NOT EXISTS testdb;
hive (default)> show databases;
hive (default)> show databases like 't.*';
OK
database_name
testdb
Time taken: 0.053 seconds, Fetched: 1 row(s)
hive (default)> create database testdb_loc location '/user/hive/warehouse/testdb_loc';
OK
Time taken: 0.386 seconds
hive (default)> show databases;
OK
database_name
default
testdb
testdb_loc
Time taken: 0.024 seconds, Fetched: 3 row(s)
hive (default)>
hive (default)> create database testdb_zhushi
> COMMENT 'zhushi';
hive (default)> DESCRIBE DATABASE testdb_zhushi;
OK
db_name comment location parameters
testdb_zhushi zhushi hdfs://hadoop.zc.com:8020/user/hive/warehouse/testdb_zhushi.db zc
Time taken: 0.015 seconds, Fetched: 1 row(s)
hive (default)> create database testdb_otherinfo
> WITH DBPROPERTIES ('creator' = 'zhangcheng', 'date' = '2015-11-30');
hive (default)> drop database if exists testdb_zhushi;
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:存放路径
hive (testdb)> create table if not exists testdb.pepole(
> id int COMMENT 'user id',
> name string COMMENT 'user name',
> phone string COMMENT 'user phone')
> COMMENT 'pepele info'
> ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
> STORED AS textfile ;
hive (testdb)> show tables;
OK
tab_name
pepole
Time taken: 0.02 seconds, Fetched: 1 row(s)
hive (testdb)> create table if not exists testdb.pepole_like like testdb.pepole;
hive (testdb)> show tables;
OK
tab_name
pepole
pepole_like
Time taken: 0.015 seconds, Fetched: 2 row(s)
hive (testdb)> create table if not exists testdb.pepole_select as select id,name from testdb.pepole;
hive (testdb)> show tables;
OK
tab_name
pepole
pepole_like
pepole_select
Time taken: 0.015 seconds, Fetched: 3 row(s)
hive (testdb)> CREATE EXTERNAL TABLE IF NOT EXISTS testdb.emp_ext(
> empno int,
> ename string,
> job string,
> mgr int,
> hiredate string,
> sal double,
> comm double,
> deptno int
> )
> ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
> STORED AS textfile ;
hive (testdb)> CREATE TABLE IF NOT EXISTS testdb.emp_partition(
> empno int,
> ename string,
> job string,
> mgr int,
> hiredate string,
> sal double,
> comm double,
> deptno int
> )
> PARTITIONED BY (month string, day string)
> ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
> STORED AS textfile ;
load data local inpath '/opt/datas/emp.txt' overwrite into table testdb.emp_partition partition(month = '201512',day = '01');
load data local inpath '/opt/datas/emp.txt' overwrite into table testdb.emp_partition partition(month = '201512',day = '02');
hive (testdb)> select * from emp_partition where month='201512' and day='02';
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];
在此输入正文