–变量
–局部变量:作用域是方法内
declare val data_type default df_value;
–全局变量:跨函数访问
set @var = 1;
– <> and or not
–逻辑结构
–分支
if expr1 then
…
elseif expr2 then
…
else
…
end if;
–循环
while expr do
…
end while;
–函数
设置结束提交符
delimiter //
创建函数
create function FUNC_NAME(param…) returns RETURN_TYPE
begin
…
end //
调用函数 select FUNC_NAME(params…) //
查看当前函数
show function status like '...%' //
删除函数
drop function FUNC+NAME //
substring_index(varchar(N),int,varchar(N) --2,'aa,bb,cc,dd'
create function size_index(sep char(1),src varchar(100)) returns int
begin
declare t int default 0;
declare pos int default 1;
while 0<>locate(sep,src,pos)do
set t=t+1;
set pos = locate(sep,src,pos)+1;
end while;
return t+1;
end //
4-1
--abceedefeeopg
create function split(src varchar(100),sep varchar(20)) returns varchar(100)
begin
declare rst varchar(100) default '';
declare pos int default 1;
declare L int default char_length(sep);
while 0<>locate(sep,src,pos) do
if pos<>1 then set rst=concat(rst,',') ; enf if;
set rst = concat(rst,substr(src,pos,locate(sep,src,pos)-pos));
set pos=locate(sep,src,pos)+L;
end while;
set rst=concat(rst,',',substr(src,pos));
return rst;
end //
–存储过程
–查看存储过程
show procedure status like …
--删除存储过程
drop procedure PRO_NAME;
--创建存储过程
create procedure PRO_NAME(in/out/inout VAR_NAME DATA_TYPE)
begin
...
end //
--调用存储过程
call PRO_NAME(...);
--成绩表分页查询
create procedure pro_selscore(in pageNo int,in pageSize int,out total int)
begin
declare _begin int default (pageNo-1)*pageSize;
select ceil(count(1)/pageSize) into total from score;
select * from score limit _begin,pageSize;
end //
create procedure pro_carryover(
in fromUser int,
in toUser int,
in amount decimal(10,2),
out rst int
)
begin
declare accnum int default 0;
declare _balance decimal(10,2) default 0;
declare continue handler for sqlexception set rst=-1;
set rst=0;
select count(userid) into accnum from account where userid in(fromUser,toUser);
if accnum<2 then
set rst = -2;
else
select balance into _balance from account where userid=fromUser;
if _balance<account then
set rst=-1;
else
start transaction;
update account set balance=balance-amount where userid=fromUser;
update account set balance=balance+amount where userid=toUser;
if rst<>-1 then
set rst=1;
commit;
else
set rst=0;
rollback;
end if;
end if;
end if;
end //
--事务
set autocommit=0;--关闭自动提交:开启事务
commit;--提交后结束事务
rollback; --回滚后结束事务
索引
create index ix_stuid on score(student_id)
--锁
--共享锁,排他锁
select操作 --默认不加锁 lock in share mode/for update
insert/update/delete --默认添加排它锁
--
create 'kb10:student','basicinfo','stuinfo'
insert overwrite directory '/kb10/shop1118/' row format fields terminated by ',' stored as textfile select
shopid,shopname,contact.mobile mobile
insert overwrite directory ‘/kb10/shop1118’ row format
delimited fields terminated by ‘,’ stored as textfile
select shopid,shopname,contact.mobile mobile,concat_ws(’’,address)
address,volumn[‘2020’] vol2020 from shop;
create table hive_shop(
id int,
name varchar(50),
mobile varchar(20),
address varchar(100),
vol2020 int
);
sqoop export
–connect jdbc:mysql://192.168.75.140:3306/kb10
–username root
–password kb10
–table hive_shop
–columns id,name,mobile,address,vol2020
–fields-terminated-by ‘,’
–export-dir ‘/kb10/shop1118/000000_0’
sqoop import
–connect jdbc:mysql://master:3306/kb10
–username root
–password kb10
–table student
–hbase-table mysql_student1
–column-family stuinfo
–hbase-create-table
–hbase-row-key sid
上课讲的
sqoop import
–connect jdbc:mysql://master:3306/kb06mysqltestdb
–username root
–password kb10
–table score
–hbase-table kb10:mysql_stu
–column-family score
–hbase-row-key sid
(在hive中输入)
create external table hbase_score(sid string,student_id int,course_id int,score int)
stored by ‘org.apache.hadoop.hive.hbase.HBaseStorageHandler’
with SERDEPROPERTIES (“hbase.columns.mapping” = “:key,score:student_id,score:course_id,score:score”)
TBLPROPERTIES (“hbase.table.name” = “kb10:mysql_stu”);
mysql - > hdfs
#–incremental append 只支持新增不支持更新
#–table TABLE)NAME 或 --query SELECT)COMMAND
#–split-by 和 -m 结合实现numberReduceTasks并行
#–check-column 和–last-value 结合实现–check-column:where sid>5
sqoop import
–connect jdbc:mysql://master:3306/kb06mysqltestdb
–username root
–password kb10
–query “select tid,tname from teacher where $CONDITIONS”
–target-dir /kb10/mysqlscore1119
–split-by tid
-m 2
–check-column tid
–incremental append
–last-value 5
–incremental lastmodified 修改和增加
–check-column 必须为timestamp类型
create table lmtest(id int auto_increment primary key,name varchar(20),time timestamp);
insert into lmtest(name) value(“henry”),(“pola”),(“mike”),(“ariel”),(“jerry”);
sqoop import
–connect jdbc:mysql://master:3306/kb06mysqltestdb
–username root
–password kb10
–query “select id,name,time from lmtest where $CONDITIONS”
–target-dir /kb10/lm1118
–split-by id
-m 1
–check-column time
–incremental lastmodified
–merge-key id
–last-value “2020-11-22 13:35:23.0”
创建job
sqoop job --create torderincrementjob --import \vi
–connect jdbc:mysql://master:3306/kb06mysqltestdb
–username root
–password kb10
–table score
–hbase-table kb10:hbasestu
–column-family fenshu
–hbase-row-key sid
课后演练的
sqoop import
–connect jdbc:mysql://master:3306/kb06mysqltestdb
–username root
–password kb10
–table score
–hbase-table kb10:hbasestu
–column-family fenshu
–hbase-row-key sid
create external table hbase_stu(sid string,student_id int,course_id int,score int)
stored by ‘org.apache.hadoop.hive.hbase.HBaseStorageHandler’
with SERDEPROPERTIES (“hbase.columns.mapping” = “:key,fenshu:student_id,fenshu:course_id,fenshu:score”)
TBLPROPERTIES (“hbase.table.name” = “kb10:hbasestu”);