博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spark SQL demo
阅读量:7297 次
发布时间:2019-06-30

本文共 3466 字,大约阅读时间需要 11 分钟。

hot3.png

参考官网Spark SQL的例子——,自己写了一个脚本:

val sqlContext = new org.apache.spark.sql.SQLContext(sc)import sqlContext.createSchemaRDDcase class UserLog(userid: String, time1: String, platform: String, ip: String, openplatform: String, appid: String)// Create an RDD of Person objects and register it as a table.val user = sc.textFile("/user/hive/warehouse/api_db_user_log/dt=20150517/*").map(_.split("\\^")).map(u => UserLog(u(0), u(1), u(2), u(3), u(4), u(5)))user.registerTempTable("user_log")// SQL statements can be run by using the sql methods provided by sqlContext.val allusers = sqlContext.sql("SELECT * FROM user_log")// The results of SQL queries are SchemaRDDs and support all the normal RDD operations.// The columns of a row in the result can be accessed by ordinal.allusers.map(t => "UserId:" + t(0)).collect().foreach(println)

结果执行出错:

org.apache.spark.SparkException: Job aborted due to stage failure: Task 1 in stage 50.0 failed 1 times, most recent failure: Lost task 1.0 in stage 50.0 (TID 73, localhost): java.lang.ArrayIndexOutOfBoundsException: 5        at $iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$anonfun$2.apply(
:30)        at $iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$anonfun$2.apply(
:30)        at scala.collection.Iterator$$anon$11.next(Iterator.scala:328)        at org.apache.spark.util.Utils$.getIteratorSize(Utils.scala:1319)        at org.apache.spark.rdd.RDD$$anonfun$count$1.apply(RDD.scala:910)        at org.apache.spark.rdd.RDD$$anonfun$count$1.apply(RDD.scala:910)        at org.apache.spark.SparkContext$$anonfun$runJob$4.apply(SparkContext.scala:1319)        at org.apache.spark.SparkContext$$anonfun$runJob$4.apply(SparkContext.scala:1319)        at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:61)        at org.apache.spark.scheduler.Task.run(Task.scala:56)        at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:196)        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)        at java.lang.Thread.run(Thread.java:745)

从日志可以看出,是数组越界了。

用命令

sc.textFile("/user/hive/warehouse/api_db_user_log/dt=20150517/*").map(_.split("\\^")).foreach(x => println(x.size))

发现有一行记录split出来的大小是“5”

666666666615/05/21 20:47:37 INFO Executor: Finished task 0.0 in stage 2.0 (TID 4). 1774 bytes result sent to driver6666665615/05/21 20:47:37 INFO Executor: Finished task 1.0 in stage 2.0 (TID 5). 1774 bytes result sent to driver

原因是这行记录有空值“44671799^2015-03-27 20:56:05^2^117.93.193.238^0^^”

网上找到了解决办法——使用split(str,int)函数。修改后代码:

val sqlContext = new org.apache.spark.sql.SQLContext(sc)import sqlContext.createSchemaRDDcase class UserLog(userid: String, time1: String, platform: String, ip: String, openplatform: String, appid: String)// Create an RDD of Person objects and register it as a table.val user = sc.textFile("/user/hive/warehouse/api_db_user_log/dt=20150517/*").map(_.split("\\^", -1)).map(u => UserLog(u(0), u(1), u(2), u(3), u(4), u(5)))user.registerTempTable("user_log")// SQL statements can be run by using the sql methods provided by sqlContext.val allusers = sqlContext.sql("SELECT * FROM user_log")// The results of SQL queries are SchemaRDDs and support all the normal RDD operations.// The columns of a row in the result can be accessed by ordinal.allusers.map(t => "UserId:" + t(0)).collect().foreach(println)

转载于:https://my.oschina.net/u/592278/blog/417951

你可能感兴趣的文章
多媒体音视频处理及FFmpeg使用技巧总结
查看>>
eclipse中js文件的默认编码格式修改。
查看>>
获取元素到body/html的距离函数
查看>>
2018ICPC徐州区域赛网络赛B(逆序枚举或者正序深度搜索)
查看>>
一个页面从输入URL到页面加载显示完成,这个过程中都发生了什么?
查看>>
jQuery中deferred的对象使用
查看>>
vscode 调试python内置库断不下来的问题
查看>>
DDR的前世与今生(二)
查看>>
BLOG的下面部分内容怎么去掉?没有找到相关的设置!大家帮忙看看!
查看>>
Microsoft Visual C++ Redistributable Package下载
查看>>
简单数论总结1——gcd与lcm
查看>>
javascript创建对象方法总结
查看>>
蓝桥杯【入门训练】 Fibonacci数列
查看>>
poj - 2406 Power Strings 【KMP】
查看>>
bzoj2616: SPOJ PERIODNI——笛卡尔树+DP
查看>>
[学习笔记]博弈论
查看>>
[ZJOI2019]线段树
查看>>
App Store有哪些原因会影响app应用上架呢?(分享)
查看>>
C# Expression 树转化为SQL与语句(二)--解决参数问题
查看>>
POJ 1191 棋盘分割
查看>>