spark 问题锦集

1、spark程序jar与spark lib jar冲突,加载顺序

提交spark作业经常会遇到jar包中明显存在某个类,但任务提交到yarn运行时却找不到类或方法(java.lang.NoSuchMethodError)的jar包冲突问题
比如spark程序用到了guava的Splitter类:

import com.google.common.base.Splitter;
...
 List list = Splitter.on("/").splitToList(output);

程序在本地模式下能正常运行,打好的jar包也的确包含Splitter类(可以使用jar -tf xxx.jar | grep xxxx 查询),但是在yarn-cluster模式时,会出现“java.lang.NoSuchMethodError: com.google.common.base.Splitter.splitToList” 的错误,具体错误如下:

 ERROR User class threw exception: java.lang.NoSuchMethodError: com.google.common.base.Splitter.splitToList(Ljava/lang/CharSequence;)Ljava/util/List; (org.apache.spark.deploy.yarn.ApplicationMaster)
java.lang.NoSuchMethodError: com.google.common.base.Splitter.splitToList(Ljava/lang/CharSequence;)Ljava/util/List;
    at cn.creditease.bdp.jsoncook.core.string.Variable.<init>(Variable.java:14)
    at cn.creditease.bdp.jsoncook.core.string.StringCompiler.compileVariable(StringCompiler.java:63)
    at cn.creditease.bdp.jsoncook.core.string.StringCompiler.compile(StringCompiler.java:73)
    at cn.creditease.bdp.jsoncook.core.Compiler.compile(Compiler.java:49)
    at cn.creditease.bdp.jsoncook.core.Compiler.compileMap(Compiler.java:82)
    at cn.creditease.bdp.jsoncook.core.Compiler.compile(Compiler.java:67)
    at cn.creditease.bdp.sparkcook.step.CookStep.<init>(CookStep.java:62)
    at cn.creditease.bdp.sparkcook.step.LoadSQLStep.<init>(LoadSQLStep.java:15)

那是什么原因造成spark程序本地能运行,yarn上就“ NoSuchMethodError”了呢?
原因是本地的jar包被SPARK_HOME/lib中的jar覆盖。spark程序在提交到yarn时,除了上传用户程序的jar,还会上传SPARK_HOME的lib目录下的所有jar包(参考附录2 )。如果你程序用到的jar与SPARK_HOME/lib下的jar发生冲突,那么默认会优先加载SPARK_HOME/lib下的jar,而不是你程序的jar,所以会发生“ NoSuchMethodError”。
上面的例子就是因为我程序的jar用的是guava18版本(mvn dependency:tree可查出版本),但是SPARK_HOME/lib下用的是guava14版本。lib下的guava14覆盖了用户的guava18,而guava14中并没有splitToList()方法, 所以报错。

解决方案

由于默认情况下,优先级SPARK_HOME/lib/jar包 > 用户程序中的jar包, 如果想让用户程序jar优先执行,那么要使用 spark.yarn.user.classpath.first (spark1.3以前)或者 spark.executor.userClassPathFirst 和spark.driver.userClassPathFirst 参数。
这些参数能让用户的jar覆盖SPARK_HOME/lib的jar。在spark conf中将他们设置为"true"即可。

如下是“spark.yarn.user.classpath.first” 参数的官方解释:

(Experimental) Whether to give user-added jars precedence over Spark's own jars when loading 
classes in the the driver. This feature can be used to mitigate conflicts between Spark's dependencies 
and user dependencies. It is currently an experimental feature. This is used in cluster mode only.

解决方案二:–driver-class-path优先于spark_home/lib

附录2 查看spark程序在yarn中的jar包使用情况
在启动spark程序后,在yarn的Environment UI子页面可查看到spark 程序用到的jar包。
比如我们上面的spark例子程序跑起来后,可在页面上查到一共有两类guava jar包,
一类是SPARK_HOME/lib中的guava-14:
在这里插入图片描述
第二类是common、yarn、hdfs等hadoop相关的guava-18:
在这里插入图片描述

第三类是用户的guava-18, 由于用户程序的guava-18整合在了打包好的jar包中,所以在UI页面上看不到。

参考:https://www.jianshu.com/p/0fe48bc43a8c

2、spark-sql读取hive创建表读取数据不一致或者为空null

Tez引擎:
spark.hadoop.mapreduce.input.fileinputformat.input.dir.recursive=true spark.hive.mapred.supports.subdirectories=true
文件为Parquet格式:
set mapred.input.dir.recursive=true;
set spark.sql.hive.convertMetastoreParquet=false;
文件为orc格式:
set mapred.input.dir.recursive=true;
set spark.sql.hive.convertMetastoreOrc=false;

3、spark-submit进行debug模式调试

  1. 修改spark-submit 提交命令
// 普通的提交任务的shell脚本
./spark-submit   \
--class com.hrong.job.WordCount \
--master yarn \
--deploy-mode client \
--executor-memory 2G  \
--num-executors 2  \
hdfs://n151:9000/jars/wordcount/sparkjob.jar  hdfs://n151:9000/data/wordcount/word.text hdfs://n151:9000/data/wordcount/result

//debug代码的shell脚本
./spark-submit   \
--class com.hrong.job.WordCount \
--master yarn \
--deploy-mode client \
--executor-memory 2G  \
--num-executors 2  \
--driver-java-options "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=10002" \
hdfs://n151:9000/jars/wordcount/sparkjob.jar  hdfs://n151:9000/data/wordcount/word.text hdfs://n151:9000/data/wordcount/result
  1. 对idea进行配置
    在这里插入图片描述
  2. 集群提交spark任务
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值