开发者社区> 问答> 正文

E-MapReduce银行员工信息查询示例



段落1创建临时表

  1. [backcolor=transparent]%[backcolor=transparent]spark
  2. [backcolor=transparent]import[backcolor=transparent] org[backcolor=transparent].[backcolor=transparent]apache[backcolor=transparent].[backcolor=transparent]commons[backcolor=transparent].[backcolor=transparent]io[backcolor=transparent].[backcolor=transparent]IOUtils
  3. [backcolor=transparent]import[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]net[backcolor=transparent].[backcolor=transparent]URL
  4. [backcolor=transparent]import[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]nio[backcolor=transparent].[backcolor=transparent]charset[backcolor=transparent].[backcolor=transparent]Charset
  5. [backcolor=transparent]// Zeppelin creates and injects sc (SparkContext) and sqlContext (HiveContext or SqlContext)
  6. [backcolor=transparent]// So you don't need create them manually
  7. [backcolor=transparent]// load bank data
  8. [backcolor=transparent]val bankText [backcolor=transparent]=[backcolor=transparent] sc[backcolor=transparent].[backcolor=transparent]parallelize[backcolor=transparent](
  9. [backcolor=transparent]    [backcolor=transparent]IOUtils[backcolor=transparent].[backcolor=transparent]toString[backcolor=transparent](
  10. [backcolor=transparent]        [backcolor=transparent]new[backcolor=transparent] URL[backcolor=transparent]([backcolor=transparent]"http://emr-sample-projects.oss-cn-hangzhou.aliyuncs.com/bank.csv"[backcolor=transparent]),
  11. [backcolor=transparent]        [backcolor=transparent]Charset[backcolor=transparent].[backcolor=transparent]forName[backcolor=transparent]([backcolor=transparent]"utf8"[backcolor=transparent])).[backcolor=transparent]split[backcolor=transparent]([backcolor=transparent]"\n"[backcolor=transparent]))
  12. [backcolor=transparent]case[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]Bank[backcolor=transparent]([backcolor=transparent]age[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]Integer[backcolor=transparent],[backcolor=transparent] job[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent],[backcolor=transparent] marital[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent],[backcolor=transparent] education[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent],[backcolor=transparent] balance[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]Integer[backcolor=transparent])
  13. [backcolor=transparent]val bank [backcolor=transparent]=[backcolor=transparent] bankText[backcolor=transparent].[backcolor=transparent]map[backcolor=transparent]([backcolor=transparent]s [backcolor=transparent]=>[backcolor=transparent] s[backcolor=transparent].[backcolor=transparent]split[backcolor=transparent]([backcolor=transparent]";"[backcolor=transparent])).[backcolor=transparent]filter[backcolor=transparent]([backcolor=transparent]s [backcolor=transparent]=>[backcolor=transparent] s[backcolor=transparent]([backcolor=transparent]0[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]!=[backcolor=transparent] [backcolor=transparent]"\"age\""[backcolor=transparent]).[backcolor=transparent]map[backcolor=transparent](
  14. [backcolor=transparent]    s [backcolor=transparent]=>[backcolor=transparent] [backcolor=transparent]Bank[backcolor=transparent]([backcolor=transparent]s[backcolor=transparent]([backcolor=transparent]0[backcolor=transparent]).[backcolor=transparent]toInt[backcolor=transparent],[backcolor=transparent]
  15. [backcolor=transparent]            s[backcolor=transparent]([backcolor=transparent]1[backcolor=transparent]).[backcolor=transparent]replaceAll[backcolor=transparent]([backcolor=transparent]"\""[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]""[backcolor=transparent]),
  16. [backcolor=transparent]            s[backcolor=transparent]([backcolor=transparent]2[backcolor=transparent]).[backcolor=transparent]replaceAll[backcolor=transparent]([backcolor=transparent]"\""[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]""[backcolor=transparent]),
  17. [backcolor=transparent]            s[backcolor=transparent]([backcolor=transparent]3[backcolor=transparent]).[backcolor=transparent]replaceAll[backcolor=transparent]([backcolor=transparent]"\""[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]""[backcolor=transparent]),
  18. [backcolor=transparent]            s[backcolor=transparent]([backcolor=transparent]5[backcolor=transparent]).[backcolor=transparent]replaceAll[backcolor=transparent]([backcolor=transparent]"\""[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]""[backcolor=transparent]).[backcolor=transparent]toInt
  19. [backcolor=transparent]        [backcolor=transparent])
  20. [backcolor=transparent]).[backcolor=transparent]toDF[backcolor=transparent]()
  21. [backcolor=transparent]bank[backcolor=transparent].[backcolor=transparent]registerTempTable[backcolor=transparent]([backcolor=transparent]"bank"[backcolor=transparent])


段落2查询表结构

  1. [backcolor=transparent]%[backcolor=transparent]sql
  2. [backcolor=transparent]desc bank


段落3查询年龄小于30各年龄段员工人数

  1. [backcolor=transparent]%[backcolor=transparent]sql [backcolor=transparent]select[backcolor=transparent] age[backcolor=transparent],[backcolor=transparent] count[backcolor=transparent]([backcolor=transparent]1[backcolor=transparent])[backcolor=transparent] value [backcolor=transparent]from[backcolor=transparent] bank [backcolor=transparent]where[backcolor=transparent] age [backcolor=transparent]<[backcolor=transparent] [backcolor=transparent]30[backcolor=transparent] [backcolor=transparent]group[backcolor=transparent] [backcolor=transparent]by[backcolor=transparent] age order [backcolor=transparent]by[backcolor=transparent] age


段落4 查询年龄小于等于20岁的员工信息

  1. [backcolor=transparent]%[backcolor=transparent]sql [backcolor=transparent]select[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent] [backcolor=transparent]from[backcolor=transparent] bank [backcolor=transparent]where[backcolor=transparent] age [backcolor=transparent]<=[backcolor=transparent] [backcolor=transparent]20

展开
收起
nicenelly 2017-10-27 15:54:54 1757 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
大数据解决方案构建详解 以阿里云E-MapReduce为例 立即下载
《E-MapReduce on ACK 全新发布,助力企业高效构建大数据平台》 立即下载
基于E-MapReduce梨视频推荐系统 立即下载