E-MapReduce银行员工信息查询示例
段落1创建临时表
- [backcolor=transparent]%[backcolor=transparent]spark
- [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
- [backcolor=transparent]import[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]net[backcolor=transparent].[backcolor=transparent]URL
- [backcolor=transparent]import[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]nio[backcolor=transparent].[backcolor=transparent]charset[backcolor=transparent].[backcolor=transparent]Charset
- [backcolor=transparent]// Zeppelin creates and injects sc (SparkContext) and sqlContext (HiveContext or SqlContext)
- [backcolor=transparent]// So you don't need create them manually
- [backcolor=transparent]// load bank data
- [backcolor=transparent]val bankText [backcolor=transparent]=[backcolor=transparent] sc[backcolor=transparent].[backcolor=transparent]parallelize[backcolor=transparent](
- [backcolor=transparent] [backcolor=transparent]IOUtils[backcolor=transparent].[backcolor=transparent]toString[backcolor=transparent](
- [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]),
- [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]))
- [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])
- [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](
- [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]
- [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]),
- [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]),
- [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]),
- [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
- [backcolor=transparent] [backcolor=transparent])
- [backcolor=transparent]).[backcolor=transparent]toDF[backcolor=transparent]()
- [backcolor=transparent]bank[backcolor=transparent].[backcolor=transparent]registerTempTable[backcolor=transparent]([backcolor=transparent]"bank"[backcolor=transparent])
段落2查询表结构
- [backcolor=transparent]%[backcolor=transparent]sql
- [backcolor=transparent]desc bank
段落3查询年龄小于30各年龄段员工人数
- [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岁的员工信息
- [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
收起
0
条回答
写回答
取消
提交回答