一:数据表建立
(一)创建数据库
hive> create database hadoop;
hive> use hadoop;
数据库位置在 hdfs://ns1/user/hive/warehouse/hadoop.db目录下
(二)建表
hive> create table t_order(id int,name string,container string,price double) > row format delimited > fields terminated by ' ';
(三)创建数据表使用array
//array create table tab_array(a array<int>,b array<string>) row format delimited fields terminated by ' ' //使用 分割字段 collection items terminated by ','; //使用,分割数组元素
select a[0] from tab_array; select * from tab_array where array_contains(b,'word'); insert into table tab_array select array(0),array(name,ip) from tab_ext t;
(四)使用map创建数据表
//map create table tab_map(name string,info map<string,string>) row format delimited fields terminated by ' ' //使用 分割字段 collection items terminated by ',' //使用,分割map元素 map keys terminated by ':'; //使用:分割每个map的key和value
load data local inpath '/home/hadoop/hivetemp/tab_map.txt' overwrite into table tab_map; insert into table tab_map select name,map('name',name,'ip',ip) from tab_ext;
(五)使用struct创建数据表
create table tab_struct(name string,info struct<age:int,tel:string,addr:string>) row format delimited fields terminated by ' ' collection items terminated by ',' load data local inpath '/home/hadoop/hivetemp/tab_st.txt' overwrite into table tab_struct; insert into table tab_struct select name,named_struct('age',id,'tel',name,'addr',country) from tab_ext;
二:数据文件导入
文件数据:
[hadoop@hadoopH1 ~]$ cat order.txt 00001001 iphone5 32G 4999 00001002 iphone6S 128G 9999 00001003 xiaomi6x 32G 2999 00001004 honor 32G 3999
(一)hive使用hql进行导入
1.从本地导入数据到hive的表中(实质就是将文件上传到hdfs中hive管理目录下)
load data local inpath '/home/hadoop/order.txt' into table t_order;
实际是拷贝数据到hdfs文件系统中。
(二)直接上传数据到hdfs文件目录下
[hadoop@hadoopH1 ~]$ cat order_1.txt 00002001 redmi 32G 3999 00002002 geli 128G 1999 00002003 xiami6x 32G 999 00002004 huawei 32G 3999
使用Hadoop命令: hadoop fs -put order_1.txt /user/hive/warehouse/hadoop.db/t_order/
(三)使用load data进行数据导入
1.从本地导入数据到hive的表中(实质就是将文件上传到hdfs中hive管理目录下)
load data local inpath '/home/hadoop/ip.txt' into table tab_ext;
2.从hdfs上导入数据到hive表中(实质就是将文件从原始目录移动到hive管理的目录下)
load data inpath 'hdfs://ns1/aa/bb/data.log' into table tab_user;
(四)使用insert进行数据导入(不允许一条插入,一般使用overwrite和select进行数据文件拷贝)
insert overwrite table tab_ip_seq select * from tab_ext;
三:数据查询
(一)select查询语句
select * from t_order;
(二)select查询测试调用mapreduce程序
select count(*) from t_order;
调用mapreduce程序进行数据处理。
四:其他方式建立数据表
(一)使用external外部表
从hdfs其他目录下引入数据,进行建表
CREATE EXTERNAL TABLE tab_ip_ext(id int, name string, ip STRING, country STRING) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE #数据表存储格式为TEXT文本 LOCATION '/external/user'; #从hdfs中其他文件目录/external/user下引入数据
不需要移动数据到专门的数据存放目录下。
(二)使用AS和select语句建立数据表(用于创建一些临时表存储中间结果)
CREATE TABLE t_order_sel
AS
SELECT id new_id, name new_name, price new_price
FROM t_order
SORT BY new_id;
会调用mapreduce程序进行数据处理,并将结果存放在数据表文件目录下,作为数据
(三)insert from select ...可以用于向已经存在的临时表中追加中间数据
五:partition分区
如果使用groupby或者where语句,则是针对所有数据集进行操作。所以我们可以在建表得时候指定分区,之后进行查询的时候可以使用分区查询或者全局查询操作,提高效率
partition分区,可以使用建表字段,也可以使用其他字段。
建表使用partition进行分区,数据插入也需要指定分区进行存放
建表:
create table tab_ip_part(id int,name string,ip string,country string) partitioned by (year string) row format delimited fields terminated by ',';
数据加载:
load data local inpath '/home/hadoop/data.log' overwrite into table tab_ip_part partition(year='1990'); load data local inpath '/home/hadoop/data2.log' overwrite into table tab_ip_part partition(year='2000');
实际:将数据加载再分区中,既是在表目录下新建一个分区目录,将数据放入该目录中。
显示分区:
show partitions tab_ip_part;
六:alter修改数据表信息
alter table tab_ip change id id_alter string; 修改字段
ALTER TABLE tab_cts ADD PARTITION (partCol = 'dt') location '/external/hive/dt'; 修改添加分区
七:使用cluster
数据表创建:
create table tab_ip_cluster(id int,name string,ip string,country string) clustered by(id) into 3 buckets;
数据导入:
load data local inpath '/home/hadoop/ip.txt' overwrite into table tab_ip_cluster; set hive.enforce.bucketing=true; insert into table tab_ip_cluster select * from tab_ip;
数据查询:
select * from tab_ip_cluster tablesample(bucket 2 out of 3 on id);
八:使用shell命令执行hive语句
使用shell机制,可以利用脚本语言shell/python进行hql语句批量执行
hive -S -e 'select country,count(*) from tab_ext' > /home/hadoop/hivetemp/e.txt
九:自定义函数(同之前利用电话号获取地区)
select getarea(phoneNB),upflow,downflow from t_flow
(一)原始数据
1389990045 239 300 1385566005 229 435 1385566005 192 256 1389990045 23 84 1390876045 682 432 1385566005 134 300 1390876045 378 656 1390876045 346 123 1389990045 78 352
(二)处理结果形式
1389990045 beijing 239 300 1385566005 nanjin 229 435 1385566005 nanjin 192 256 1389990045 beijing 23 84 1390876045 shenyang 682 432 1385566005 nanjin 134 300 1390876045 shenyang 378 656 1390876045 shenyang 346 123 1389990045 beijing 78 352
(三)函数实现
1.实现Java类,定义上述函数逻辑。转化为jar包,上传到hive的lib中
2.在hive中创建一个函数getarea,和jar包中的自定义java类建立关联
package cn.hadoop.hive; import java.util.HashMap; import org.apache.hadoop.hive.ql.exec.UDF; public class phoneNBToArea extends UDF{ public static HashMap<String,String> areamap = new HashMap<>(); static { areamap.put("1389", "beijing"); areamap.put("1385", "nanjin"); areamap.put("1390", "shenyang"); } public String evaluate(String phoneNB) { //需要重载该方法 String result = areamap.get(phoneNB.substring(0, 4))==null?(phoneNB+" nowhere"):(phoneNB+" "+areamap.get(phoneNB.substring(0, 4))); return result; } }
(四)将jar包导入hive中
hive> add jar /home/hadoop/hive.jar; hive> create temporary function getarea as 'cn.hadoop.hive.phoneNBToArea';
(四)数据表创建
create table t_flow(phoneNB string,upflow int,download int) row format delimited fields terminated by ' ';
(五)数据导入
load data local inpath '/home/hadoop/flow.txt' into table t_flow;
(六)结果显示
select getarea(phoneNB),upflow,download from t_flow;