create table test(
name string,
friends array<string>,
children map<string,int>,
address struct<street:string,city:string>
)
row format delimited fields terminated by ','
collection items terminated by '_'
map keys terminated by ':';
load data local inpath '/opt/module/datas/test.txt' into table test;
DDL:data defination language
创建库:
- CREATE DATABASE [IF NOT EXISTS] database_name
- [COMMENT database_comment]
- [LOCATION hdfs_path]
- [WITH DBPROPERTIES (property_name=property_value, ...)]
create database if not exists testdb4 comment 'this is a score database';
create database if not exists testdb5
comment 'this is a database of person info'
with dbproperties('creater'='myself','createtime'='2021-11-01');
desc database extended testdb5; 查看自定义的属性值
create database testdb6 location '/datas/abcdef';
删除数据库的时候,数据库为空才可以删除
若要强制删除
drop database testdb5 cascade;
alter database testdb5 set dbproperties('creater'='zhangsan');
创建表的语句:
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]
[LOCATION hdfs_path]
[TBLPROPERTIES (property_name=property_value, ...)]
[AS select_statement]
1、EXTERNAL:外部表 可以让用户创建一个外部表,在建表的同时可以指定一个指向实际数据的路径(LOCATION),在删除表的时候,内部表的元数据和数据会被一起删除,而外部表只删除元数据,不删除数据。
create external table human(id int,name string) location '/datas';
create table human1(id int,name string) location '/datas';
2、IF NOT EXISTS:当表名不存在的时候创建
3、[(col_name data_type [COMMENT col_comment], ...)] :指定列名和列的数据类型,以及给每列添加注释
create table human2(id int comment 'this is the persons id',name string comment 'every persons name');
4、[COMMENT table_comment] :给这张表添加注释
create external table if not exists human3(
id int comment 'this is the persons id',
name string comment 'every persons name'
)
comment 'the comment of this table';
5、[PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)] :分区表
6、[CLUSTERED BY (col_name, col_name, ...) : --分桶表
7、[SORTED BY (col_name [ASC|DESC], ...)] :分出的每个桶中按照哪些字段排序(正序/倒序)
8、INTO num_buckets BUCKETS] : --分成几个桶
9、row format delimited fields:规定导入数据的字段分隔符
collection items :
map keys:
10、stored as:在hdfs中将表的数据按照什么类型存储,默认存储类型是textfile,其他类型还有sequencefile ,parquet,orc
11、location:指定表的数据在hdfs上的位置
12、tblproperties:设置表的属性
create table human4(id int,name string) tblproperties('abc'='123','123'='abc');
13、as:复制一张表
create table test2 as select name,friends,children from test;
14、like:复制一张表结构
create table test3 like test;
将内部表转换为外部表语法:
alter table test set tblproperties('EXTERNAL' = 'true');
修改表:
重命名: alter table 表名 rename to 新表名;
更新列:
ALTER TABLE table_name CHANGE [COLUMN] col_old_name col_new_name column_type [COMMENT col_comment] [FIRST|AFTER column_name]
alter table t1 change d ddd string comment 'this col is string' after b;
alter table t1 change ddd d int first;
增加和替换列
ALTER TABLE table_name ADD|REPLACE COLUMNS (col_name data_type [COMMENT col_comment], ...)
alter table t1 add columns(f int,g string);
alter table t1 REPLACE columns(h int,j string);