1.hive 简化mapreduce编程
- facebook开发 数据仓库date wareHouse(比数据库大,随数据变化的),主要对企业的决策分析
- HiveQL(SQL)语句语句进行分析数据
- 可以导入不同数据库的数据
- 比较mysql
1. 引擎mapreduce spark存储在hdfs中
2. 数据插入 hive仅支持批量插入,不支持单语句插入
3. 不支持数据更新
4. 延迟高
5. 没有对数据建索引,查询慢- 数据模型
database—>table(内部表[位置固定,会删除元数据] 外部表[放的位置不固定,删除数据,不会删元数据])
分区表(相当于索引)提高查询效率 是目录
bucket桶表 根据hash决定文件在哪里提高查询效率 是文件
2.hive模式(他的系统的元数据)
1.内嵌 derby数据库存储元数据
2.直连数据库 mysql等其他数据库存储元数据
3.远程 同2,但需单独开启metastore服务
3.修改服务器端的字符集
#exit退出mysql
sudo vim /etc/my.cnf
#在[mysqld]下输入
character_set_server=utf8
4.Hsql与mysql语句的区别
#mysql和hive,文件会存储在hdfs的/user/hive/warehouse的 t1.db文件
alter database testjsc2 set dbproperties('edited-by'='jss'); #添加附加信息
#,也可以desc database extended t1查看位置和附加信息
create database t1 if no exists t1;
create database student2 LOCATION '/input/student2.db'; #指定路径
use t1;
#删除 有表不能删除数据库,加上cascade可以强制删除
drop database t1;
drop database t1 cascade;
#区别,hive有默认数据库default,所以不使用use也可以选择 有自动补全和提示功能
#查看当前数据库
select current_database();
#使用数据库
show databases;
use default;
#创建表 ,外部表是从文本文件读的表(指定位置),区别在于删除会删除原来的文件
create external table student_info(stu_no string,stu_name string,stu_sex string,telephone string,stu_class string) row format delimited fields terminated by ',' location '/student'; #默认使用文本存储,
create table test(id int,name string);
#还可以修改上面的创建表语句,根据分隔符 '/t'空格隔开
#查看表的信息
desc extended test ;#有附加信息
#hive命令行也可以操作linux 命令 dfs不用!
!ls;
dfs -put /aa.txt
#数据类型 string替换varchar,有复杂的struct和map类型
#可以先创建表,后上传表数据文件
5.数据加载(load data) 创建表后用这个修改,覆盖原有的数据(把文件复制到目标系统)
#覆盖 local指linux方式上传,也可以去掉local hdfs上传
load data local inpath '/aaa' overwrite into table xxx
#追加数据 ,会导致hdfs多一个文件
load data local inpath '/aaa' into table xxx
#设置表头(查询的时候看到字段名)
set hive.cli.print.header=true;
#查看表结构
desc formatted student;
6.分区(分为多个文件夹的文件)和分桶(平分数据)(也可以分表后再分桶)
#创建一个分区,表名为age=20,还有就是语句能搜索到
load data local inpath '/home/hadoop/file2.txt' into table student partition(age=20,gender='aaa');
#显示多少个分区
show partitions student;
select name,age from student where age=17;
select name,age from student where age=20;
#后期增加分区,再加载数据进来
alter table student add partition(age=22);
#删除分区
alter table student drop partition(age=17);
#创建分桶表 分成6个分区平分数据
create table user_info(user_id int,name string) clustered by(user_id) into 6 buckets row format delimited fields terminated by ',';
7.批量执行sql语句
source /home/hadoop/SQL.txt; #SQL.txt放所有的sql语句
8.导出数据放在文件夹
#on可以用where 替代,但是效率低
9.插入数据 (写好复制过去)
insert into table student values(2018213223,'王小哲' ,'男',18,'IS');
#查看hdfs数据
dfs -cat /xxx
10.查询
#3表连接
select student.Sname,course.Cname,sc.Grade from student join sc on student.sno=sc.sno join course on sc.cno=course.cno;
#导出数据到本地目录
insert overwrite local directory '/home/hadoop/sc' row format delimited fields terminated by ',' select student.Sname,course.Cname,sc.Grade from student join sc on student.sno=sc.sno join course on sc.cno=course.cno;
#加上where查询条件
select student.Sname,sc.grade from student join sc on student.sno=sc.sno where sc.cno=2 and sc.grade>90;
#分组查询,还是排序,物以类聚
select cno,count(*) from sc group by cno;
# 用于 group by 后面用 having (不可以where替代)
#!!!设置分区,会生成2个文件
set mapred.reduce.tasks=2;
#查看
set mapred.reduce.tasks;
#hive特有的 distribute by sort by,两表查询,生成俩表分别排序
select * from student distribute by sex sort by sage;
#范围区间查询
select name,age from studentp where age=20 or age=17;
#联合查询(连接两个查询语句)查出更多的结果
select name,age from studentp where age=20 union select name,age from studentp where age=17;
10.sort by 区别(hive)和order by区别
sort by 效率高 局部排序 在reduce中排序 配合 distribute by ,俩表分别排序
order by 全局排序
11.hive内置函数
#连接不上xshell启动网卡命令
ifup eth33
#查看所有函数
show functions;
show functions like 'c*';
#可以看例子
desc function extended ceil;
select ceil(2.3);//3
select unix_timestamp(); //时间戳
select from_unixtime(1685369460);//转时间戳为date时间
select current_database();
select array(1,2,3);//生成矩阵
select explode(array(1,2,3));//1行变1列
//输出
1
2
3
select split('hello,world',',');//分割,出数组 ['hello','world']
select explode(split('hello,world',','));
#实现wordcount函数
create table docs(line string);
load data local inpath '/home/hadoop/f1.txt' overwrite into table docs;
select * from docs;
select word, count(1) as count from (select explode(split(line,' '))as word from docs) as w group by word order by word;
#下面是写java代码,然后export打包为jar文件
#eclipse项目导入hadoop 的commonjar和hive/lib的jar
package hiveudf;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
/**
* Hive自定义函数类
*/
@SuppressWarnings("deprecation")
public class myudf extends UDF {
public String evaluate(Text text) {
String result = "手机号码错误!";
if (text != null && text.getLength() == 11) {
String inputStr = text.toString();
StringBuffer sb = new StringBuffer(); //链式操作
sb.append(inputStr.substring(0, 3));
sb.append("****");
sb.append(inputStr.substring(7)); //7下标到最后
result = sb.toString();
}
return result;
}
}
#添加jar包导入新函数
add jar /home/hadoop/aa.jar
#formatphone是函数名
create temporary function formatphone as 'hiveudf.myudf'; //hiveudf是包名 myudf是类名
Select id,formatphone(phone) as newphone from t_user; //使用函数, phone存入的值是我们的函数传入的值
//手机号会变成133****1111
drop temporary function formatphone;//删除导入函数