Spark SQL的代码示例分析
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章跟大家分析一下"Spark SQL的代码示例分析"。内容详细易懂,对"Spark SQL的代码示例分析"感兴趣的朋友可以跟着小编的思路慢慢深入来阅读一下,希望阅读后能够对大家有所帮助。下面跟着
千家信息网最后更新 2026年11月01日Spark SQL的代码示例分析
这篇文章跟大家分析一下"Spark SQL的代码示例分析"。内容详细易懂,对"Spark SQL的代码示例分析"感兴趣的朋友可以跟着小编的思路慢慢深入来阅读一下,希望阅读后能够对大家有所帮助。下面跟着小编一起深入学习"Spark SQL的代码示例分析"的知识吧。
参考官网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)关于Spark SQL的代码示例分析就分享到这里啦,希望上述内容能够让大家有所提升。如果想要学习更多知识,请大家多多留意小编的更新。谢谢大家关注一下网站!
分析
代码
示例
内容
知识
跟着
学习
一行
例子
兴趣
函数
办法
原因
后代
命令
大小
思路
数组
日志
易懂
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
中国互联网科技工作委员会毛军
新华互联网科技学校
互联网科技保险
互联网科技财经
科技互联网政务服务
互联网 的科技创新
银优互联网科技有限公司
沃里互联网科技
赫美联合互联网科技
互联网科技公司介绍
美国互联网前沿科技
互联网新科技动漫ppt素材
洛阳共同富裕互联网科技有限公司
阿里互联网科技企业
互联网金融科技元年
南通创西澳互联网科技有限公司
互联网科技机房
华人运通互联网科技公司
优雇互联网科技有限公司
如何强化互联网金融科技
中智农互联网科技
壹账通互联网科技
科技互联网创业计划书
中国最先进互联网科技公司
科技赋能互联网健康
互联网汽车科技小镇
互联网科技博主是什么意思
浙南科技城互联网大厦建设工程
上海互联网科技公司ppt
半导体行业属于互联网高科技类吗