数据类型
读写模式
数据库加载数据分为读模式和写模式,hive属于读模式。
读模式:加载数据时不对数据合法性进行检验,查询时不合法的数据显示为null,优点是加载数据速度快适合大数据。
写模式:加载数据时对数据合法性进行检验。优点是不必担心数据库中的数据不合法,传统的数据库如mysql,oracle就是此种模式。
例1,创建一个hive表t1并导入数据
创建一张表
创建一个文件
导入数据并显示
将文件t1的数据导入hive中的表t1,实际上文件中的每一行数据都只导入到了t1表的id字段中,而name和online字段并未导入数据这是因为数据不合法没有指定分隔符,但是hive在加载数据时为读模式所以它并未检验合法性,而显示时用null来填充name和online字段。
分隔符
之前在上一节中介绍hive在创建表时必须指定行列分隔符,不指定则使用默认的分隔符,默认的行分隔符为’\n’,列分隔符为’\001’这是一个不可见字符。
例2:创建一个hive表t2并指定分隔符然后导入数据
创建一个文件包含创建语句然后在hive中执行
[root@shb01 data]# more t2_hql.hql
create table t2(
id string,
name string,
online boolean)
row format delimited
fields terminated by '\t'
lines terminated by '\n';
hive> source /usr/local/data/t2_hql.hql;
OK
Time taken: 0.116 seconds
创建一个数据文件
[root@shb01 data]# more t2_data
1 jack true
2 tom false
执行加载并显示结果
可以看到此次加载数据并没有像例1中t1表那样出现null,因为我创建t2表时指定的分隔符和数据文件t2_data中的行列分隔符一致。另外指定分隔符必须fields在前lines在后
基本数据类型
Hive和数据库类似也有一些基本的数据类型,这里就不介绍了只从网上找了一个截图。
复杂数据类型
Hive有三种复杂数据类型分别是array,map,struct。Array类似java中的集合,map类似java中的map容器,struct类似java中的对象。下面的一个例子介绍其基本使用方法。
Array通过下标获取数据,map通过key获取数据,通过字段属性获取数据。
1.创建hive表并加载数据
[root@shb01 data]# more t3_hql.hql
create table t3(
id string,
name string,
stu_num array<string>,
class_num map<string,int>,
person_addressstruct<floor:string,room:string>)
row format delimited
fields terminated by '\t'
collection items terminated by ','
map keys terminated by ':';
hive> source /usr/local/data/t3_hql.hql;
OK
Time taken: 2.755 seconds
[root@shb01 data]# more t3_data
1 teacher1 tom,jack,will class1:30,class2:20 13th,room2
2 teacher2 tom1,jack1,will1 class1:31,class2:21 6th,room4
hive> load data local inpath'/usr/local/data/t3_data' overwrite into table t3;
Loading data to table default.t3
Table default.t3 stats: [numFiles=1,numRows=0, totalSize=133, rawDataSize=0]
OK
Time taken: 4.836 seconds
创建一个教师信息表,stu_num表示其学生名称信息使用array,class_num表示其所带班级学生人数使用map,person_address表示此教师办公室地址使用struct。Array和struct的默认分隔符为\002,map的默认分隔符为\003,不过我定义时指定了分隔符。
2.查询复杂数据类型
hive> select * from t3;
OK
1 teacher1 ["tom","jack","will"] {"class1":30,"class2":20} {"floor":"13th","room":"room2"}
2 teacher2 ["tom1","jack1","will1"] {"class2":31} {"floor":"4th","room":"room1"}
查询复杂数据类型
hive> selectid,name,stu_num[1],class_num["class2"],person_address.floor,person_address.roomfrom t3 where id="1";
OK
1 teacher1 jack 20 13th room2
Time taken: 0.11 seconds, Fetched: 1 row(s)
stu_num是array,使用下标获取数据stu_num[1]
class_num是map,使用key获取数据class_num["class2"]
person_address.floor 是struct,使用对象属性获取数据,person_address相当于一个对象,floor相当与对象的属性person_address.floor
Hive中库,表数据与hdfs数据的对应关系
Hive中库和表都对应hdfs的目录
库
Hive有一个默认的数据库default,而hive数据库对应hdfs上的一个目录,此目录的位置可以在hive_site.xml中找到。
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
<description>location of default database for thewarehouse</description>
</property>
同样在SQLyog中也可以通过查阅DBS来查看数据库信息
我们可以在hive中通过命令来查看,创建,删除数据库,当我们执行创建删除数据库时DBS表都会及时感应到,注意存放元数据的默认数据库default不能被删除。
show databases;
use default;
create database mydb1;/ create databasemydb2 location '/user/hive/mydb2';
drop database mydb1;
表
和数据库一样我们可以创建查看删除表,表的元数据通过TBLS和COLUMNS_V2可以查看到。
create table t1(id int);
show tables;
desc tableName;
show create table tableName;
上面两个图是我创建的t1和t3表的元数据,COLUMNS_V2表的存放字段信息,CD_ID和TBLS表中的TBL_ID一致。
也可以通过web方式查看相关信息
http://shb01:50070也可以看到表数据信息
加载数据
1.使用load data local inpath '/usr/local/data/t3_data' overwrite intotable t3;加载数据,没有local是从hdfs中加载数据,有则是从本地的linux中加载数据。
2.hive中执行insert
insert into table t1 select id from t2
insert overwrite table t1 select id from t2
3.使用文件data1
创建一个文件,文件内容
from t5
insert into table t4 select id where id=1
insert into table t3 select id where id =2
然后在hive中使用source data1
4.import命令
Import table t1 from ‘usr/local/data/t1’
5.动态分区加载
开启动态分区,在hive中执行
set hive.exec.dynamic.partition=true
set hive.exec.dynamic.partition.mode=nonstrict
insert into table t5_part_2 partition(year=2015,school) select id,name from t5_part
6.也可以使用hadoop直接上传数据。[root@shb01 data]# hadoop fs -putt3_data_01 /user/hive/warehouse/t3
在hive下查看数据多了一倍
hive> selectid,name,stu_num[1],class_num["class2"],person_address.room,person_address.floorfrom t3;
OK
1 teacher1 jack 20 room2 13th
2 teacher2 jack1 31 room1 4th
1 teacher1 jack 20 room2 13th
2 teacher2 jack1 31 room1 4th
注意 :时间关系2~5我没有做实验验证只是把例子贴出来了:)