Hive的DDL数据定义和DML数据操作

Hive数据类型
Java数据类型 Hive数据类型 长度
byte TINYINT 1byte
short SMALINT 2byte
int INT 4byte
long BIGINT 8byte
float FLOAT 单精度浮点数
double DOUBLE 双精度浮点数
string STRING 字符
TIMESTAMP 时间类型
BINARY 字节数组

DDL数据定义
1)创建数据库
-》查看数据库
show databases;
-》创建数据库
create database hive_db;
-》创建数据库标准写法
create database if not exist db_hive;
-》创建数据库指定所在hdfs路径
create database hive_db1 location ‘/hive_db’;
2)修改数据库
-》查看数据库结构
desc database hive-db;
-》添加描述信息
alter database hive_db set dbproperties(‘datais’=‘terry’); -》查看拓展属性
desc database extended hive_db;
3)查询数据库
-》显示数据库
show databases; -》筛选查询的数据库
show database like ‘db*’;

4)删除数据库
	 -》删除数据库
	drop dabase hive_db; -》删除数据库标准写法
	drop database if exists hive_db;
5)创建表
	 -》创建表
	> create table db_h(id int,name string) > row format
	> delimited fields
	> terminated by "\t"
6)管理表(内部表)
	 不擅长做数据共享 删除hive中管理表,数据删除。
	-》加载数据
	load data local inpath '/root/terry.txt' into table emp; -》查询并保存到一张新的表
	create table if not exists emp2 as select * from emp where name = 'hunte r';
	-》查询表结构
	desc formatted emp;
		Table Type: MANAGED_TABLE
7)外部表
	hive不认为这张表拥有这份数据,删除该表,数据不删除。 擅长做数据共享
	-》创建外部表
		> create external table if not exists emptable(empno int,ename string,job
		 string,mgr int,birthdate string,sal double,comm double,deptno int)
		> row format
		> delimited fields
		> terminated by '\t';
	-》导入数据
	load data local inpath '/root/emp.txt' into table emp;
	-》查看表结构
	desc formatted emp;
		Table Type: EXTERNAL_TABLE

	 -》删除表
	drop table emptable;
	提示:再次创建相同的表 字段相同 将自动关联数据!
8)分区表`
	 -》创建分区表
	hive> create table dept_partitions(depno int,dept string,loc string)
	> partitioned by(day string)
	> row format delimited fields
	> terminated by '\t';

	-》加载数据
	load data local inpath '/root/dept.txt' into table dept_partitions; //出错
	注意:不能直接导入,需要指定分区
	load data local inpath '/root/dept.txt' into table dept_partitions partition(day='1112');

	-》添加分区,不导数据
	alter table dept_partitions add partition(day='1113');

	-》单分区查询
	select * from dept_partitions where day='1112';
	
	-》全查询
	select * from dept_partitions;
	
	-》查询表结构
	desc formatted dept_partitions;
	
	-》删除单个分区
	alter table dept_partitions drop partition(day='1112');

9)修改表
	 -》修改表名
	alter table emptable rename to new_table_name;
	-》添加列
	alter table dept_partitions add columns(desc string); 
	-》更新列(重命名,更改数据类型)
	alter table dept_partitions change column desc desccc int; 
	-》替换(相当于重新定义,全部替换,数据也没有了
	alter table dept_partitions replace column(desccc int);

DML数据操作
1)向表中加载数据
load data local inpath ‘/root/itstar.txt’ into table terry;

 2)加载hdfs中数据
load data inpath '/terry.txt' into table terry; 提示:相当于剪切

3)覆盖原有的数据
load data local inpath '/root/itstar.txt' overwrite into table terry;

4)创建分区表
create table terry_partitions(id int,name string) partitioned by (month string) row format
delimited fields terminated by '\t';

5)向分区表插入数据
insert into table terry_partitions partition(month='201811') values (1,'tongliya');

6)按照条件查询结果存储到新表
create table if not exists terry_ppp as select * from terry_partitions where name='tongliya';

7)创建表时加载数据
> create table db_h(id int,name string) 
> row format
> delimited fields
> terminated by "\t"
> location '';

8)查询结果导出到本地
	1、
	insert overwrite local directory '/root/datas/yangmi.txt' select * from hh where name='yangmi';
		此时yangmi.txt是文件夹,不是文件
	2、
	bin/hive -e 'select * from terry ' 
	> /root/terry.txt
		此时terry.txt是文件
	bin/hive -e "select * from terry where name = 'yangmi' " 
	> /root/terry.txt
	3、
	hive> dfs -get /usr/hive/warehouse/00000 /root;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值