摘要: spark 累加歷史主要用到了窗口函數(shù),而進(jìn)行全部統(tǒng)計,則需要用到rollup函數(shù)
1 應(yīng)用場景:
1、我們需要統(tǒng)計用戶的總使用時長(累加歷史)
2、前臺展現(xiàn)頁面需要對多個維度進(jìn)行查詢,如:產(chǎn)品、地區(qū)等等
3、需要展現(xiàn)的表格頭如: 產(chǎn)品、2015-04、2015-05、2015-06
2 原始數(shù)據(jù):
product_code |event_date |dur...
摘要: Spark1.4發(fā)布,支持了窗口分析函數(shù)(window functions)。在離線平臺中,90%以上的離線分析任務(wù)都是使用Hive實(shí)現(xiàn),其中必然會使用很多窗口分析函數(shù),如果SparkSQL支持窗口分析函數(shù),
那么對于后面Hive向SparkSQL中的遷移的工作量會大大降低,使用方式如下:
1、初始化數(shù)據(jù)
創(chuàng)建表
[sql] view plain cop...
1.in 不支持子查詢 eg. select * from src where key in(select key from test);
支持查詢個數(shù) eg. select * from src where key in(1,2,3,4,5);
in 40000個 耗時25.766秒
in 80000個 耗時78.827秒
2.union all/union
不支持頂層的union all eg. select key from src UNION ALL select key from test;
支持select * from (select key from src union all select key from test)aa;
不支持 union
支持select distinct key from (select key from src union all select key from test)aa;
3.intersect 不支持
4.minus 不支持
5.except 不支持
6.inner join/join/left outer join/right outer join/full outer join/left semi join 都支持
left outer join/right outer join/full outer join 中間必須有outer
join是最簡單的關(guān)聯(lián)操作,兩邊關(guān)聯(lián)只取交集;
left outer join是以左表驅(qū)動,右表不存在的key均賦值為null;
right outer join是以右表驅(qū)動,左表不存在的key均賦值為null;
full outer join全表關(guān)聯(lián),將兩表完整的進(jìn)行笛卡爾積操作,左右表均可賦值為null;
left semi join最主要的使用場景就是解決exist in;
Hive不支持where子句中的子查詢,SQL常用的exist in子句在Hive中是不支持的
不支持子查詢 eg. select * from src aa where aa.key in(select bb.key from test bb);
可用以下兩種方式替換:
select * from src aa left outer join test bb on aa.key=bb.key where bb.key <> null;
select * from src aa left semi join test bb on aa.key=bb.key;
大多數(shù)情況下 JOIN ON 和 left semi on 是對等的
A,B兩表連接,如果B表存在重復(fù)數(shù)據(jù)
當(dāng)使用JOIN ON的時候,A,B表會關(guān)聯(lián)出兩條記錄,應(yīng)為ON上的條件符合;
而是用LEFT SEMI JOIN 當(dāng)A表中的記錄,在B表上產(chǎn)生符合條件之后就返回,不會再繼續(xù)查找B表記錄了,
所以如果B表有重復(fù),也不會產(chǎn)生重復(fù)的多條記錄。
left outer join 支持子查詢 eg. select aa.* from src aa left outer join (select * from test111)bb on aa.key=bb.a;
7. hive四中數(shù)據(jù)導(dǎo)入方式
1)從本地文件系統(tǒng)中導(dǎo)入數(shù)據(jù)到Hive表
create table wyp(id int,name string) ROW FORMAT delimited fields terminated by '\t' STORED AS TEXTFILE;
load data local inpath 'wyp.txt' into table wyp;
2)從HDFS上導(dǎo)入數(shù)據(jù)到Hive表
[wyp@master /home/q/hadoop-2.2.0]$ bin/hadoop fs -cat /home/wyp/add.txt
hive> load data inpath '/home/wyp/add.txt' into table wyp;
3)從別的表中查詢出相應(yīng)的數(shù)據(jù)并導(dǎo)入到Hive表中
hive> create table test(
> id int, name string
> ,tel string)
> partitioned by
> (age int)
> ROW FORMAT DELIMITED
> FIELDS TERMINATED BY '\t'
> STORED AS TEXTFILE;
注:test表里面用age作為了分區(qū)字段,分區(qū):在Hive中,表的每一個分區(qū)對應(yīng)表下的相應(yīng)目錄,所有分區(qū)的數(shù)據(jù)都是存儲在對應(yīng)的目錄中。
比如wyp表有dt和city兩個分區(qū),則對應(yīng)dt=20131218city=BJ對應(yīng)表的目錄為/user/hive/warehouse/dt=20131218/city=BJ,
所有屬于這個分區(qū)的數(shù)據(jù)都存放在這個目錄中。
hive> insert into table test
> partition (age='25')
> select id, name, tel
> from wyp;
也可以在select語句里面通過使用分區(qū)值來動態(tài)指明分區(qū):
hive> set hive.exec.dynamic.partition.mode=nonstrict;
hive> insert into table test
> partition (age)
> select id, name,
> tel, age
> from wyp;
Hive也支持insert overwrite方式來插入數(shù)據(jù)
hive> insert overwrite table test
> PARTITION (age)
> select id, name, tel, age
> from wyp;
Hive還支持多表插入
hive> from wyp
> insert into table test
> partition(age)
> select id, name, tel, age
> insert into table test3
> select id, name
> where age>25;
4)在創(chuàng)建表的時候通過從別的表中查詢出相應(yīng)的記錄并插入到所創(chuàng)建的表中
hive> create table test4
> as
> select id, name, tel
> from wyp;
8.查看建表語句
hive> show create table test3;
9.表重命名
hive> ALTER TABLE events RENAME TO 3koobecaf;
10.表增加列
hive> ALTER TABLE pokes ADD COLUMNS (new_col INT);
11.添加一列并增加列字段注釋
hive> ALTER TABLE invites ADD COLUMNS (new_col2 INT COMMENT 'a comment');
12.刪除表
hive> DROP TABLE pokes;
13.top n
hive> select * from test order by key limit 10;
14.創(chuàng)建數(shù)據(jù)庫
Create Database baseball;
14.alter table tablename change oldColumn newColumn column_type 修改列的名稱和類型
alter table yangsy CHANGE product_no phone_no string
15.導(dǎo)入.sql文件中的sql
spark-sql --driver-class-path /home/hadoop/hive/lib/mysql-connector-java-5.1.30-bin.jar -f testsql.sql
insert into table CI_CUSER_20141117154351522 select mainResult.PRODUCT_NO,dw_coclbl_m02_3848.L1_01_02_01,dw_coclbl_d01_3845.L2_01_01_04 from (select PRODUCT_NO from CI_CUSER_20141114203632267) mainResult left join DW_COCLBL_M02_201407 dw_coclbl_m02_3848 on mainResult.PRODUCT_NO = dw_coclbl_m02_3848.PRODUCT_NO left join DW_COCLBL_D01_20140515 dw_coclbl_d01_3845 on dw_coclbl_m02_3848.PRODUCT_NO = dw_coclbl_d01_3845.PRODUCT_NO
insert into CI_CUSER_20141117142123638 ( PRODUCT_NO,ATTR_COL_0000,ATTR_COL_0001) select mainResult.PRODUCT_NO,dw_coclbl_m02_3848.L1_01_02_01,dw_coclbl_m02_3848.L1_01_03_01 from (select PRODUCT_NO from CI_CUSER_20141114203632267) mainResult left join DW_COCLBL_M02_201407 dw_coclbl_m02_3848 on mainResult.PRODUCT_NO = dw_coclbl_m02_3848.PRODUCT_NO
CREATE TABLE ci_cuser_yymmddhhmisstttttt_tmp(product_no string) row format serde 'com.bizo.hive.serde.csv.CSVSerde' ;
LOAD DATA LOCAL INPATH '/home/ocdc/coc/yuli/test123.csv' OVERWRITE INTO TABLE test_yuli2;
創(chuàng)建支持CSV格式的testfile文件
CREATE TABLE test_yuli7 row format serde 'com.bizo.hive.serde.csv.CSVSerde' as select * from CI_CUSER_20150310162729786;
不依賴CSVSerde的jar包創(chuàng)建逗號分隔的表
"create table " +listName+ " ROW FORMAT DELIMITED FIELDS TERMINATED BY ','" +
" as select * from " + listName1;
create table aaaa ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' STORED AS TEXTFILE as select * from
ThriftServer 開啟FAIR模式
SparkSQL Thrift Server 開啟FAIR調(diào)度方式:
1. 修改$SPARK_HOME/conf/spark-defaults.conf,新增
2. spark.scheduler.mode FAIR
3. spark.scheduler.allocation.file /Users/tianyi/github/community/apache-spark/conf/fair-scheduler.xml
4. 修改$SPARK_HOME/conf/fair-scheduler.xml(或新增該文件), 編輯如下格式內(nèi)容
5. <?xml version="1.0"?>
6. <allocations>
7. <pool name="production">
8. <schedulingMode>FAIR</schedulingMode>
9. <!-- weight表示兩個隊(duì)列在minShare相同的情況下,可以使用資源的比例 -->
10. <weight>1</weight>
11. <!-- minShare表示優(yōu)先保證的資源數(shù) -->
12. <minShare>2</minShare>
13. </pool>
14. <pool name="test">
15. <schedulingMode>FIFO</schedulingMode>
16. <weight>2</weight>
17. <minShare>3</minShare>
18. </pool>
19. </allocations>
20. 重啟Thrift Server
21. 執(zhí)行SQL前,執(zhí)行
22. set spark.sql.thriftserver.scheduler.pool=指定的隊(duì)列名
等操作完了 create table yangsy555 like CI_CUSER_YYMMDDHHMISSTTTTTT 然后insert into yangsy555 select * from yangsy555
創(chuàng)建一個自增序列表,使用row_number() over()為表增加序列號 以供分頁查詢
create table yagnsytest2 as SELECT ROW_NUMBER() OVER() as id,* from yangsytest;

Sparksql的解析與Hiveql的解析的執(zhí)行流程:
