官网文档:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL

一、create table

1、官方字段

#
# CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name -- (Note: TEMPORARY available in Hive 0.14.0 and later)
[(col_name data_type [COMMENT col_comment], ... [constraint_specification])]
[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]
[SKEWED BY (col_name, col_name, ...) -- (Note: Available in Hive 0.10.0 and later)]
ON ((col_value, col_value, ...), (col_value, col_value, ...), ...)
[STORED AS DIRECTORIES]
[
[ROW FORMAT row_format]
[STORED AS file_format]
| STORED BY 'storage.handler.class.name' [WITH SERDEPROPERTIES (...)] -- (Note: Available in Hive 0.6.0 and later)
]
[LOCATION hdfs_path]
[TBLPROPERTIES (property_name=property_value, ...)] -- (Note: Available in Hive 0.6.0 and later)
[AS select_statement]; -- (Note: Available in Hive 0.5.0 and later; not supported for external tables) CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name
LIKE existing_table_or_view_name
[LOCATION hdfs_path]; data_type
: primitive_type
| array_type
| map_type
| struct_type
| union_type -- (Note: Available in Hive 0.7.0 and later) primitive_type
: TINYINT
| SMALLINT
| INT
| BIGINT
| BOOLEAN
| FLOAT
| DOUBLE
| DOUBLE PRECISION -- (Note: Available in Hive 2.2.0 and later)
| STRING
| BINARY -- (Note: Available in Hive 0.8.0 and later)
| TIMESTAMP -- (Note: Available in Hive 0.8.0 and later)
| DECIMAL -- (Note: Available in Hive 0.11.0 and later)
| DECIMAL(precision, scale) -- (Note: Available in Hive 0.13.0 and later)
| DATE -- (Note: Available in Hive 0.12.0 and later)
| VARCHAR -- (Note: Available in Hive 0.12.0 and later)
| CHAR -- (Note: Available in Hive 0.13.0 and later) array_type
: ARRAY < data_type > map_type
: MAP < primitive_type, data_type > struct_type
: STRUCT < col_name : data_type [COMMENT col_comment], ...> union_type
: UNIONTYPE < data_type, data_type, ... > -- (Note: Available in Hive 0.7.0 and later) row_format
: DELIMITED [FIELDS TERMINATED BY char [ESCAPED BY char]] [COLLECTION ITEMS TERMINATED BY char]
[MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char]
[NULL DEFINED AS char] -- (Note: Available in Hive 0.13 and later)
| SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, ...)] file_format:
: SEQUENCEFILE
| TEXTFILE -- (Default, depending on hive.default.fileformat configuration)
| RCFILE -- (Note: Available in Hive 0.6.0 and later)
| ORC -- (Note: Available in Hive 0.11.0 and later)
| PARQUET -- (Note: Available in Hive 0.13.0 and later)
| AVRO -- (Note: Available in Hive 0.14.0 and later)
| JSONFILE -- (Note: Available in Hive 4.0.0 and later)
| INPUTFORMAT input_format_classname OUTPUTFORMAT output_format_classname constraint_specification:
: [, PRIMARY KEY (col_name, ...) DISABLE NOVALIDATE ]
[, CONSTRAINT constraint_name FOREIGN KEY (col_name, ...) REFERENCES table_name(col_name, ...) DISABLE NOVALIDATE

2、建表例子

例子1、2

##################栗子#####################

--------------------------------------------------------------------------------------------
create table IF NOT EXISTS default.bf_1og_20150913 #在default库下创建一个表,不存在则创建;
(
ip string COMMENT 'remote ip address', #COMMENT:字段注释
user string,
req_url string COMMENT 'user request url'
)
COMMENT ' BeiFeng Web Access Logs' #表注释
ROW FORMAT DELIMITED FIELDS TERMINATED BY‘ ’ #hive的数据存在hdfs上,此项指定数据文件中列之间的间隔符
STORED AS TEXTFILE #数据格式
LOCATION '/user/bf/hive/warehouse/bf_log_201501913' #表的存储路径,可以自己指定 --------------------------------------------------------------------------------------------
create table IF NOT EXISTS default.bf_1og_20150913_sa
AS select ip, req_url from default.bf_log_20150913; #创建一个表,此表的字段来源于查询另外一个表

例子3

################################
create table IF NOT EXISTS default.bf_log_20150914
like default.bf_log_20150913 #根据另外一张表来创建表

二、演示

1、建表

#创建表
hive (default)> create table IF NOT EXISTS default.bf_1og_20150913(
> ip string COMMENT 'remote ip address',
> user string,
> req_url string COMMENT 'user request url')
> COMMENT 'BeiFeng Web Access Logs'
> ROW FORMAT DELIMITED FIELDS TERMINATED BY' '
> STORED AS TEXTFILE;
OK
Time taken: 0.361 seconds hive (default)> show tables;
OK
tab_name
bf_1og_20150913
bf_log
Time taken: 0.052 seconds, Fetched: 2 row(s)

2、导入数据

#########
hive (default)> load data local inpath '/opt/datas/bf-log.txt' into table default.bf_1og_20150913;
Copying data from file:/opt/datas/bf-log.txt
Copying file: file:/opt/datas/bf-log.txt
Loading data to table default.bf_1og_20150913
Table default.bf_1og_20150913 stats: [numFiles=1, numRows=0, totalSize=141, rawDataSize=0]
OK
Time taken: 0.36 seconds #########
hive (default)> select * from default.bf_1og_20150913;
OK
bf_1og_20150913.ip bf_1og_20150913.user bf_1og_20150913.req_url
"27.38.5.159" "-" "31/Aug/2015:00:04:53
"27.38.5.159" "-" "31/Aug/2015:00:04:37
"27.38.5.159" "-" "31/Aug/2015:00:04:53
Time taken: 0.156 seconds, Fetched: 3 row(s)

3、第二种建表例子

#建表
hive (default)> create table IF NOT EXISTS default.bf_1og_20150913_sa AS select ip, req_url from default.bf_1og_20150913; #
hive (default)> show tables;
OK
tab_name
bf_1og_20150913
bf_1og_20150913_sa #
hive (default)> select * from default.bf_1og_20150913_sa;
OK
bf_1og_20150913_sa.ip bf_1og_20150913_sa.req_url
"27.38.5.159" "31/Aug/2015:00:04:53
"27.38.5.159" "31/Aug/2015:00:04:37
"27.38.5.159" "31/Aug/2015:00:04:53
Time taken: 0.028 seconds, Fetched: 3 row(s)

4、第三种建表例子

##
hive (default)> create table IF NOT EXISTS default.bf_log_20150914 like default.bf_1og_20150913;
OK
Time taken: 0.046 seconds ##
hive (default)> show tables;
OK
tab_name
bf_1og_20150913
bf_1og_20150913_sa
bf_log
bf_log_20150914
Time taken: 0.013 seconds, Fetched: 4 row(s) #这里是指copy表结构,不copy表数据
hive (default)> select * from default.bf_log_20150914;
OK
bf_log_20150914.ip bf_log_20150914.user bf_log_20150914.req_url
Time taken: 0.029 seconds

三、Create Database

DDL:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL

DML:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DML

1、Create Database

CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name
[COMMENT database_comment]
[LOCATION hdfs_path]
[WITH DBPROPERTIES (property_name=property_value, ...)];

具体:

##
创建
create database db_name; create database if not exists db_name; #标准 #指定HDFS上的存储位置
create database if not exists db_name location ‘/user/root/hive/warehouse/db_name.db’; ##
查看
show databases;
show databases like 'db_hive*'; desc database extended db_name; ##
删除
drop database db_name; drop database db_name cascade; drop database if exists db_name; ##
Alter Database
ALTER (DATABASE|SCHEMA) database_name SET DBPROPERTIES (property_name=property_value, ...); -- (Note: SCHEMA added in Hive 0.14.0) ALTER (DATABASE|SCHEMA) database_name SET OWNER [USER|ROLE] user_or_role; -- (Note: Hive 0.13.0 and later; SCHEMA added in Hive 0.14.0) ALTER (DATABASE|SCHEMA) database_name SET LOCATION hdfs_path; -- (Note: Hive 2.2.1, 2.4.0 and later)

2.1-2.2 Hive 中数据库(Table、Database)基本操作的更多相关文章

  1. 当在hive中show&nbsp;table&nbsp;…

    当在hive中show table 时如果报以下错时 FAILED: Error in metadata: javax.jdo.JDODataStoreException: Error(s) were ...

  2. Hive中的数据库(Database)和表(Table)

    在前面的文章中,介绍了可以把Hive当成一个"数据库",它也具备传统数据库的数据单元,数据库(Database/Schema)和表(Table). 本文介绍一下Hive中的数据库( ...

  3. sqoop将关系型数据库的表导入hive中

    1.sqoop 将关系型数据库的数据导入hive的参数说明:

  4. hive中关于数据库与表等的基本操作

    一:基本用法 1.新建数据库 2.删除数据库 3.删除非空的数据库 4.指定数据库的位置 LOCATION:指定数据库的位置,不会在系统的默认文件下. 5.在指定数据库中新建表(验证在指定的数据库中可 ...

  5. 039 hive中关于数据库与表等的基本操作

    一:基本用法 1.新建数据库 2.删除数据库 3.删除非空的数据库 4.指定数据库的位置 LOCATION:指定数据库的位置,不会在系统的默认文件下. 5.在指定数据库中新建表(验证在指定的数据库中可 ...

  6. hive中简单介绍分区表(partition table)——动态分区(dynamic partition)、静态分区(static partition)

    一.基本概念 hive中分区表分为:范围分区.列表分区.hash分区.混合分区等. 分区列:分区列不是表中的一个实际的字段,而是一个或者多个伪列.翻译一下是:“在表的数据文件中实际上并不保存分区列的信 ...

  7. 使用Sqoop,最终导入到hive中的数据和原数据库中数据不一致解决办法

            Sqoop是一款开源的工具,主要用于在Hadoop(Hive)与传统的数据库(mysql.postgresql...)间进行数据的传递,可以将一个关系型数据库(例如 : MySQL , ...

  8. 数据库中Schema、Database、User、Table的关系[转]

    数据库的初学者往往会对关系型数据库模式(schema).数据库(database).表(table).用户(user)之间感到迷惘,总感觉他们的关系千丝万缕,但又不知道他们的联系和区别在哪里,对一些问 ...

  9. Hive中的Order by与关系型数据库中的order by语句的异同点

    在Hive中,ORDER BY语句是对查询结果集进行整体的排序,最终将会产生一个reducer进行全局的排序,达到的最终结果是和传统的关系型数据库是一样的. 在数据量非常大的时候,全局排序的单个red ...

随机推荐

  1. Django-celery分布式任务

    昨天一个很好的面试官问我你在python中怎么实现定时任务呢?我没回答好,我问了下原来有个叫celery的东西,感觉挺好用的 Celery 是一个 基于python开发的分布式异步消息任务队列,通过它 ...

  2. uboot 命令

    1.清除前一次的编译结果: make distclean 2.配置makefile:选择开发板 make  smdk6410_config 3.编译 make 注意::编译时,打开的文档文件,目录都要 ...

  3. Web性能测试工具:Siege安装&使用简介

    在Web性能测试工具中,siege是比较热门和常见的,它有安装简单,使用简单,测试报告详细的特点. 并且可以在文本中预定义一系列待测试url模拟,并可设定一定并发量下持续指定时间or测试进行测试. 比 ...

  4. python的id()函数的一个小方面(转载)

    >>> a = 2 >>> b = 2 >>> id(a) 21132060 >>> id(b) 21132060 >&g ...

  5. 【TensorFlow-windows】(零)TensorFlow的"安装"

    Tensorflow的安装,具体操作就不演示了.具体操作请移步: http://blog.csdn.net/darlingwood2013/article/details/60322258#comme ...

  6. Drawing Images and Text

    using System;using UIKit;using Foundation;using CoreGraphics;namespace GraphicsAnimation{ public cla ...

  7. 最新wap手机agent

    名称 agent 铃声格式 和弦数 数据量 删除 LGE-CU8080  LGE-CU8080/1.0 UP.Browser/4.1.26l UP.Link/5.1.2.9  pmd2.0  40   ...

  8. EasyRTMP视频直播推送H264 sps解析错误导致播放画面拉伸问题解决

    EasyRTMP是将H264流以及AAC流以RTMP协议推送到RTMP服务器上进行直播.EasyRTMP推送库中会从H264流中提取中SPS.PPS进行解析,开发的时候遇到过有些SPS解析有误,获取到 ...

  9. NOTE:rfc5766-turn-server

    NOTE:This project is active in Google code: http://code.google.com/p/rfc5766-turn-server/ 启动方法:./tur ...

  10. select version();desc mysql.user;

    D:\wamp64\wamp\bin\mysql\mysql5.6.17\bin>mysql -hgoDev -uroot -ppasswordWarning: Using a password ...