一、数据库内容
1、创建数据库
如果重复创建会提示存在,添加 IF NOT EXISTS 则不存在问题
location默认的是在hdfs上use/werehost上面,如果自己指定的是不带DB的,需要创建
官网语法:
CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name
[COMMENT database_comment]
[LOCATION hdfs_path]
[WITH DBPROPERTIES (property_name=property_value, ...)];
示例:
hive (default)> create database d7_hive;
OK
Time taken: 12.868 seconds
hive (default)> show databases;
OK
database_name
d7_hive
default
Time taken: 0.576 seconds, Fetched: 2 row(s)
如果重复创建会提示存在,添加 IF NOT EXISTS 则不存在问题
hive (default)> reate database d7_hive;
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. Database ruozedata already exists
hive (default)> create database if not exists d7_hive;
OK
Time taken: 0.017 seconds
不指定创建数据库路径,默认创建在/user/hive/warehouse目录下,有一个数据库名.db的文件夹
[hadoop@hadoop001 ~]$ hdfs dfs -ls /user/hive/warehouse
Found 2 items
drwxr-xr-x - hadoop supergroup 0 2019-07-28 00:02 /user/hive/warehouse/d7_hive.db
drwxr-xr-x - hadoop supergroup 0 2019-07-27 18:02 /user/hive/warehouse/hello
指定创建数据库目录,自己指定的是不带DB的,需要创建,创建好之后对应的文件夹下面会有对应的文件
需要注意的创建表默认是内部表,指定EXTERNAL则是外部表,两者的区别在于删除时内部表hdfs上数据和元数据都会被删除,外部表只删除元数据不会。
hive (default)> use d7_hive;
OK
Time taken: 0.153 seconds
hive (d7_hive)> create table hello(id int,name string)
> row format delimited fields terminated by ',';
OK
Time taken: 2.172 seconds
hive (d7_hive)>
> load data local inpath '/home/hadoop/data/hello.txt' overwrite into table hello;
Loading data to table d7_hive.hello
Table d7_hive.hello stats: [numFiles=1, totalSize=28]
OK
Time taken: 3.102 seconds
2、删除数据库
数据库删除的时候,只要库下有表,默认就不能删除
如果必须删除,在语句后加入cascade
CASCADE慎用
官网语法:
DROP (DATABASE|SCHEMA) [IF EXISTS] database_name [RESTRICT|CASCADE];
删除的时候提示错误,库下存在一个或多个表
hive (default)> DROP DATABASE IF EXISTS d7_hive;
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. InvalidOperationException(message:Database d7_hive is not empty. One or more tables exist.)
语句后加入cascade可删除,但要慎用
3、元数据的信息关联
mysql>show tables;——几个重要的表:
VERSION表中有且只能存在一条记录,描述表的信息
DBS databases 库
TBLS tables 表
COLUMNS_V2 描述表的字段
mysql>select * from DBS;
mysql>select * from DBS \G;
每一个SQL语句底层都有相应的元数据信息做关联
数据库创建完成后,元数据信息在DBS表里面
二、表的内容
字段的数据类型
*****数值:int、bigint、float、double
*****字符串:string 至少90%会用到
时间类型可以使用字符串类型: string
BOOLEAN:true false 用0/1会更简单
分隔符
字段和字段分隔符、行与行的分隔符
字段和字段分隔符:hive里面默认的分隔符 \001 ^A ,生产上最多的是, 逗号、空格、制表符tab
行与行的分隔符 :\n
举例:
1,ruoze
2,jepson
3,xingxing
==> row format delimited fields terminated by ‘,’;
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
创建表
hive (default)> create table emp(
> empno int,
> ename string,
> job string,
> mgr int,
> hiredate string,
> sal double,
> comm double,
> deptno int)
> row format delimited fields terminated by ',';
OK
Time taken: 0.083 seconds
Create Table As Select (CTAS)
---------------------
重新拷贝一个表
CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name
LIKE existing_table_or_view_name
[LOCATION hdfs_path];
拷贝表结构,不拷贝表数据
CREATE TABLE ruozedata_emp2 LIKE ruozedata_emp;
如果不知道表结构,通过show create table ruozedata_emp;查看
真正拷贝一份数据
Create Table As Select (CTAS)
create table ruozedata_emp3 as select empno,ename,deptno from ruozedata_emp;
修改表名
ALTER TABLE 老表 RENAME TO 新表;
ALTER TABLE ruozedata_emp3 rename to ruozedata_emp3_bak;
删除表
DROP TABLE [IF EXISTS] table_name [PURGE];
DROP TABLE table_name ruozedata_emp3_bak;
Truncate Table 删表的数据