Hive----建库、建表扩展语句--1

简介: 建库、建表扩展语句

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

 

创建库:

  1. CREATE DATABASE [IF NOT EXISTS] database_name
  2. [COMMENT database_comment]
  3. [LOCATION hdfs_path]
  4. [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);

目录
相关文章
|
4月前
|
SQL 关系型数据库 MySQL
Hive【基础知识 02-1】【Hive CLI 命令行工具使用】【准备阶段-建库、建表、导入数据、编写测试SQL脚本并上传HDFS】
【4月更文挑战第7天】Hive【基础知识 02-1】【Hive CLI 命令行工具使用】【准备阶段-建库、建表、导入数据、编写测试SQL脚本并上传HDFS】
64 0
|
4月前
|
SQL HIVE
Hive 建表以及使用
Hive 建表以及使用
80 0
|
SQL 存储 JSON
Hive 建表语句解析
Hive 建表语句
223 0
|
SQL HIVE
附模板和代码 | Excel数据模型自动生成Hive建表语句
在日常数据开发过程中,会经常需要根据数据模型编写建表语句,每次写建表语句都会用几分钟的时间,而且还容易出一些低级的错误,于是打算做个 Excel 模板,把表字段、表分区、表名写在里面,通过程序自动生成建表语句
|
存储 SQL 分布式计算
工作常用之Hive 调优【三】 Explain 查看执行计划及建表优化
在查询时通过 WHERE 子句中的表达式选择查询所需要的指定的分区,这样的查询效率会提高很多,所以我们需要把常常用在 WHERE 语句中的字段指定为表的分区字段。
334 0
工作常用之Hive 调优【三】 Explain 查看执行计划及建表优化
|
SQL 数据库 HIVE
Hive 建库,删库,显示所有库,显示当前库
Hive 建库,删库,显示所有库,显示当前库
|
SQL Oracle 关系型数据库
hive 建表快捷方式
hive 建表快捷方式
|
4月前
|
SQL 数据采集 数据挖掘
大数据行业应用之Hive数据分析航班线路相关的各项指标
大数据行业应用之Hive数据分析航班线路相关的各项指标
168 1
|
4月前
|
SQL 分布式计算 数据库
【大数据技术Spark】Spark SQL操作Dataframe、读写MySQL、Hive数据库实战(附源码)
【大数据技术Spark】Spark SQL操作Dataframe、读写MySQL、Hive数据库实战(附源码)
190 0
|
2月前
|
SQL 分布式计算 大数据
大数据处理平台Hive详解
【7月更文挑战第15天】Hive作为基于Hadoop的数据仓库工具,在大数据处理和分析领域发挥着重要作用。通过提供类SQL的查询语言,Hive降低了数据处理的门槛,使得具有SQL背景的开发者可以轻松地处理大规模数据。然而,Hive也存在查询延迟高、表达能力有限等缺点,需要在实际应用中根据具体场景和需求进行选择和优化。