欢迎使用CSDN-markdown编辑器

来源:互联网 发布:杭州淘宝模特培训学校 编辑:程序博客网 时间:2024/05/20 06:05

         摘要:本文将要说明如何使用Spark来对Hive进行操作

1、打jar包,提交Spark任务

      通过提交spark任务的方式,如下面的scala代码。之后需要将成代码打包成一个jar包,然后提交到spark中去.一般情况下生产上建议使用这种方法,可以灵活控制sql里的各项参数

[plain] view plain copy
print?
  1. val conf = new SparkConf()  
  2. val sc = new SparkContext(conf)  
  3. val hiveContext = new HiveContext(sc)  
  4. hiveContext.setConf(“hive.support.concurrency”, “true”)  
  5. hiveContext.setConf(“hive.exec.parallel”, “true”)  
  6. hiveContext.setConf(“hive.exec.dynamic.partition”, “true”)  
  7. hiveContext.setConf(“hive.exec.dynamic.partition.mode”, “nonstrict”)  
  8. hiveContext.setConf(“hive.exec.max.dynamic.partitions.pernode”, “10000”)  
  9. hiveContext.sql(s”insert overwrite table mtagoPblIdxd_table partition(statis_time)  select  mtagoPbl,pv,uv,uvNewVisitor,uvRegUcnt,uvCrdUcnt,uvActUcnt,regUcnt,crdApplyUcnt,crdPassUcnt,crdAmt,bindCardUcnt,bowCnt,bowUcnt,bowAmt,ordCnt,ordUcnt,ordAmt,ordDiscountAmt,ordPayCnt,ordPayUcnt,ordPayAmt,ordUsecrdAmt,loanUcnt,repayCnt,repayUcnt,repayAmt,createTime,updateDate,updateTime,statisTime from temp_MtagoPblIdxd_table”)  
    val conf = new SparkConf()    val sc = new SparkContext(conf)    val hiveContext = new HiveContext(sc)    hiveContext.setConf("hive.support.concurrency", "true")    hiveContext.setConf("hive.exec.parallel", "true")    hiveContext.setConf("hive.exec.dynamic.partition", "true")    hiveContext.setConf("hive.exec.dynamic.partition.mode", "nonstrict")    hiveContext.setConf("hive.exec.max.dynamic.partitions.pernode", "10000")    hiveContext.sql(s"insert overwrite table $mtagoPblIdxd_table partition(statis_time)  select  mtagoPbl,pv,uv,uvNewVisitor,uvRegUcnt,uvCrdUcnt,uvActUcnt,regUcnt,crdApplyUcnt,crdPassUcnt,crdAmt,bindCardUcnt,bowCnt,bowUcnt,bowAmt,ordCnt,ordUcnt,ordAmt,ordDiscountAmt,ordPayCnt,ordPayUcnt,ordPayAmt,ordUsecrdAmt,loanUcnt,repayCnt,repayUcnt,repayAmt,createTime,updateDate,updateTime,statisTime from $temp_MtagoPblIdxd_table")

之后就是使用命令来提交任务

[plain] view plain copy
print?
  1. ./bin/spark-submit –master  。。。 –class 。。。–executor-memory 。。。  
./bin/spark-submit --master  。。。 --class 。。。--executor-memory 。。。


好处:

(1)可以结合rdd来一起使用,比如使用这样写:

val result1 = hivecontext.sql(“select * from …”).rdd.filter.reduceBykey()…..

(2)可以传递不用的参数,如

[plain] view plain copy
print?
  1. hiveContext.sql(s”insert overwrite table mtagoPblIdxd_。。。)这种参数。可以动态注入&nbsp;&nbsp;</span></span></li></ol></div><pre code_snippet_id="2216048" snippet_file_name="blog_20170220_3_4296816" name="code" class="plain" style="display: none;">hiveContext.sql(s"insert overwrite tablemtagoPblIdxd_。。。)这种参数。可以动态注入
    [plain] view plain copy
    print?
    1. <span style=”font-family: Arial, Helvetica, sans-serif;”>(3)方便调试,代码也比较容易组织</span>  
    (3)方便调试,代码也比较容易组织
    [plain] view plain copy
    print?
    1. 缺点:前期开发准备工作要花费一定时间。  
    缺点:前期开发准备工作要花费一定时间。

    2、使用spark-shell命令行

    使用方法如下:

    先进入spark交互界面


    默认情况会启动一下sqlcontext,创建一个hivecontext


    [plain] view plain copy
    print?
    1. 然后就可以使用hiveContext来提交sql了!  
    然后就可以使用hiveContext来提交sql了!
    [plain] view plain copy
    print?
    1. <pre code_snippet_id=”2216048” snippet_file_name=”blog_20170220_7_8484701” name=”code” class=”plain”>hiveContext.sql(“select chnl_code chnl_code, count(1)  bow_cnt,sum(case when bow_amt is null then 0 else bow_amt end)  bow_amt from ods_uba.bus_req where req_date = ‘2016-10-23’ and bus_type = ‘bow’ group by chnl_code”).rdd.collect().foreach(println);</pre><div style=”text-align: center;”><img src=”http://img.blog.csdn.net/20170215204435272” style=”font-family: Arial, Helvetica, sans-serif;” alt=”“></div>  
    [plain] view plain copy
    print?
    1. hiveContext.sql("select chnl_code chnl_code, count(1)  bow_cnt,sum(case when bow_amt is null then 0 else bow_amt end)  bow_amt from ods_uba.bus_req where req_date = '2016-10-23' and bus_type = 'bow' group by chnl_code").rdd.collect().foreach(println);  
    hiveContext.sql("select chnl_code chnl_code, count(1)  bow_cnt,sum(case when bow_amt is null then 0 else bow_amt end)  bow_amt from ods_uba.bus_req where req_date = '2016-10-23' and bus_type = 'bow' group by chnl_code").rdd.collect().foreach(println);
    优点:

    上手快,能很快验证sql的准确性或查询一些数据

    缺点:

    不好将数据进行进一步的处理,或者存储到其它地方

    3、通过spark-sql

    spark是也提供了直接调用shell脚本的功能,

    编写如下clean.sh脚本

    [plain] view plain copy
    print?
    1. #!/bin/sh    
    2. /app/hadoop/spark/bin/spark-sql –master spark://sach01-005:7077  -i /<span style=”font-family: Arial, Helvetica, sans-serif;”>/app/hadoop</span><span style=”font-family: Arial, Helvetica, sans-serif;”>/linlin.sql - –executor-memory 1g  -e ”\</span>  
    3. insert overwrite table bus_req_prc partition (req_date, bus_type, bus_subtype)   
    4.    select cr.req_no, cr.user_id, cr.mbl_nbr, cr.uuid_type, cr.uuid, cr.openid_type, cr.openid, cr.chnl_code,   
    5.          cr.mtago_p, cr.mtago_pb, cr.mtago_pbl,  
    6.          cr.mtagi_p, cr.mtagi_pb, cr.mtagi_pbl,  
    7.          cr.mtagc_p, cr.mtagc_pb, cr.mtagc_pbl,  
    8.          cr.fr_page_type, cr.fr_page_id, cr.fr_evt_type, cr.fr_evt_id,  
    9.          cr.req_time, rrs.customer_no,  
    10.   
    11.          null, null,    – register  
    12.          null, null, null,  – bind card  
    13.   
    14.          null, null, null, null, null, null, null, 0, – credit  
    15.   
    16.          null, 0, 0, 0, null, null, – borrow      
    17.   
    18.          null, 0, 0, null, null, – ebp order  
    19.   
    20.          0, 0, 0, 0, 0, null, null,  – loan  
    21.            
    22.          rrs.application_form_id,   
    23.          rrs.actual_repay_amount,   
    24.          rrs.req_date,   
    25.          substr(rrs.request_date, 11),   
    26.   
    27.          from_unixtime(unix_timestamp(), ‘yyyy-MM-dd HH:mm:ss’),  
    28.          from_unixtime(unix_timestamp(), ‘yyyy-MM-dd’),  
    29.          from_unixtime(unix_timestamp(), ‘HH:mm:ss’),  
    30.   
    31.          ’<span style=”font-family: Arial, Helvetica, sans-serif;”>2016-17-12</span><span style=”font-family: Arial, Helvetica, sans-serif;”>’, ‘repay’, ‘rbs’</span>  
    32.          from repayment_request_summary as rrs  
    33.          join  chnl_req as cr on (cr.req_no = rrs.trans_ref_no)  
    34.          where rrs.req_date=’2016-17-12’;  
    35.   ”\  
    #!/bin/sh  /app/hadoop/spark/bin/spark-sql --master spark://sach01-005:7077  -i //app/hadoop/linlin.sql - --executor-memory 1g  -e "\insert overwrite table bus_req_prc partition (req_date, bus_type, bus_subtype)    select cr.req_no, cr.user_id, cr.mbl_nbr, cr.uuid_type, cr.uuid, cr.openid_type, cr.openid, cr.chnl_code,          cr.mtago_p, cr.mtago_pb, cr.mtago_pbl,         cr.mtagi_p, cr.mtagi_pb, cr.mtagi_pbl,         cr.mtagc_p, cr.mtagc_pb, cr.mtagc_pbl,         cr.fr_page_type, cr.fr_page_id, cr.fr_evt_type, cr.fr_evt_id,         cr.req_time, rrs.customer_no,         null, null,    -- register         null, null, null,  -- bind card         null, null, null, null, null, null, null, 0, -- credit         null, 0, 0, 0, null, null, -- borrow             null, 0, 0, null, null, -- ebp order         0, 0, 0, 0, 0, null, null,  -- loan         rrs.application_form_id,          rrs.actual_repay_amount,          rrs.req_date,          substr(rrs.request_date, 11),          from_unixtime(unix_timestamp(), 'yyyy-MM-dd HH:mm:ss'),         from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),         from_unixtime(unix_timestamp(), 'HH:mm:ss'),         '2016-17-12', 'repay', 'rbs'         from repayment_request_summary as rrs         join  chnl_req as cr on (cr.req_no = rrs.trans_ref_no)         where rrs.req_date='2016-17-12';  "\

    然后执行sh clean.sh即可

    -i 表示执行指定后缀为.sql的脚本

    -e 表示执行后面的内容

    优点:

    可以直接将原本用Hive执行MapReduce执行的shell脚本,sql脚本基本不用做太大的改动就可以使用spark来跑批(根据我们使用的经验,速度提交至少都是2倍以上)

    缺点:

    还是无法将数据进行进一步的加工处理,如何使用rdd的函数等,并且维护的成本也不低。