hive命令行常用配置 知道hive的这些ddl和dml操作语句

DDL

库操作:

创建库

create database 库名;

create database if not exists 库名;

查看库的列表

show databases;

show databases like 's*'; 查看以s开头的数据库;

使用库:

use database;

查看正在使用的库

select current_database(); --是一个函数

查看库信息

desc database 库名;

删除库

drop database 库名; 只能删除空库,里面没有表的

drop database if exists 库名;

drop database 库名 cascade; 删除库 非空库

drop database 库名 restrict; 默认情况下的删除库的操作

修改库:基本不用

if not exists

if exists 避免错误 同样适用于表的操作

表的操作:

DML

1.数据加载:load

语法:

load database

hdfs: inpath

本地: local inpath

'/home/hadoop' into table 表名

1)从本地加载数据

load data local inpath '/home/hadoop/student.txt' into table student_base ;

从本地加载数据相当于复制的工作 将本地的文件复制到hive的默认路径下

2)从hdfs加载

hive 原始的数据就存在hdfs上 如果在建一个内部表 复制?移动?

load data inpath '/student.txt' into table student_base ;

同在hdfs上的数据有必要存储两份吗?没有 所以这个操作是移动的操作

一般情况下像这种情况应该建内部表还是外部表?

如果是内部表?删除数据的时候数据和元数据会一并删除

如果是外部表删除数据的时候元数据会被删除,而原始数据能暴力流

这种情况(hdfs上原始存储的数据(公共数据)建外部表

数据加载成功之后,发现在/user/myhive/bd1803/bd1803.db/student_base目录下有两个文件

第一次加载从本地加载的 文件student.txt

第一次加载的时候从hdfs加载的 文件student.txt

这两个文件在加载的时候都会被加载到hive的指定目录下

由于在hdfs上相同的目录下不能有两个同名的文件,这时候会将第二次上传的数据重命名为student_copy01.txt

2,insert 方式:

1)单条数据插入:

insert into table student_base values(1,'zs','f',24,'CS');

会先建立一个临时表,然后将临时表中的数据拷贝到指定的表中

效率比较低,会出现很多小文件

2)多条数据插入:可以从一个表中查询 将查询结果全部插入到指定的表中 单重插入

insert into table student_base select * from stu where age>20;

一次只能插入到一个表中

3)多重插入:可以一次插入多个表 但是对基表值扫描一次 优点:减少基表的扫描次数,提升插入性能

from 表名 --要扫描的表

insert into table 表1 select ...

insert into tbale 表2 select ...

例如: stu01:大于等于24岁

stu02:小于24岁

from stu

insert into table stu01 select * where age>=24

insert into table stu02 select * where age<24;

分区表的数据插入:

create table if not exists stu_ptn(id int,name string,sex string,department string)

partitioned by (age int)

row format delimited fields terminated by ','

;

这种方式加载数据的时候一定先添加分区

alter table stu_ptn add partition (age=18);

1)load

向分区表中加载数据一定指定加载到哪个分区中

load data local inpath '/home/hadoop/student.txt' into table stu_ptn partition(age=18);

一般情况下分区表的数据加载不使用load方式, load方式不会进行数据监测

2)insert方式

insert into table stu_ptn partition(age=18) values(,,,,,,) 一般也不建议

多条数据插入

insert into table stu_ptn partition(age=18) select id,name,sex,department from stu where age=18;

正常情况下分区肯定有多个按年龄进行分区

数据加载时候这种方式可以吗?

分区比较少的时候:

alter table stu_ptn add partition (age=18);

alter table stu_ptn add partition (age=19);

alter table stu_ptn add partition (age=20);

from stu

insert into table stu_ptn partition(age=18) select id,name,sex,department from stu where age=18;

insert into table stu_ptn partition(age=18) select id,name,sex,department from stu where age=19;

insert into table stu_ptn partition(age=18) select id,name,sex,department from stu where age=20;

静态分区:在向分区表中添加数据之前手动的添加一个指定分区

分区个数不确定 添加分区的时候怎么添加?上面的方式不使用 要使用动态分区

动态分区:可以根据你的数据自动进行分区 不需要提前添加分区

create table if not exists stu_ptn01(id int,name string,sex string,department string)

partitioned by (age int)

row format delimited fields terminated by ','

;

insert into table stu_ptn partition(age) select id,name,age,sex,department from stu; 错误

insert into table stu_ptn partition(age) select id,name,sex,department,age from stu; 正确

动态分区在查询插入的时候动态分区字段必须放在最后一个字段,否则会将最后一个字段默认为你的分区字段

动态分区需要设置:

set hive.exec.dynamic.partitoin.mode=nostrict

查询属性值:

set hive.exec.dynamic.partitoin.mode;

hive.exec.dynamic.partitoin.mode=nostrict

hive.exec.dynamic.partitoin.mode=strict 严格模式 必须手动添加一个静态分区

set hive.exec.dynamic.partitoin.mode=nostrict

hive1.2版本中以下需要设置

set hive.exec.dynamic.partitoin=true; --开启动态分区

set hive.exec.dynamic.partitoin.mode=nostrict;

多个分区字段的时候:

create table if not exists stu_ptn02(id int,name string,sex string)

partitioned by (department string,age int)

row format delimited fields terminated by ','

;

insert into table stu_ptn02 partition(department,age)

select id,name,sex,department,age from stu;

多字段分区的时候目录结构按照分区字段顺序进行划分的

hive1.2中在进行动态分区的时候如果是多字段分区必须手动指定第一级的分区

分区表使用的时候分区字段可以看做一个普通字段

select * from stu_ptn02 where age=18;

只是在建表和添加数据的时候不一样

分桶表的数据插入

create table if not exists stu_bak(id int,name string,sex string,age int,departmrnt string)

clustered by (age) sorted by (sex) into 3 buckets

row format delimited fields terminated by ',';

1)load方式

load data local inpath '/home/hadoop/student.txt' into table stu_bak

load方式不能向分通表中添加数据

2)insert 方式

insert into table stu_bak select * from stu;

hive命令行常用配置 知道hive的这些ddl和dml操作语句(1)

好了,我要接着专心的去做需求,改bug了!!!

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页