2016年08月04日 11:07:35
阅读数:4630
昨晚想通过调试的方式阅读一下Spark SQL的源码, 用spark-sql命令行需要跨进程调试,太麻烦,于是尝试了一下在IDEA中运行和调试.
结果不出所料,虽然最终可以在IDEA中成功运行了,不过中间还是遇到了不少问题,在这篇文章中总结一下.
应用程序比较简单:
-
case class Person(name: String, value: String)
-
object SparkSQLTest {
-
def main(args: Array[String]) {
-
val conf = new SparkConf()
-
conf.set("spark.master", "local")
-
conf.setAppName("JavaWordCount");
-
val sc = new SparkContext(conf)
-
val sqlContext = new org.apache.spark.sql.hive.HiveContext(sc)
-
import sqlContext.implicits._
-
sqlContext.sql("select * from ewaplog where value in ('yes', null)").collect().foreach(println)
-
}
-
}
点击Run -> Run SparkSQLTest; IDEA开始用maven方式编译了一会......出错了!
1 出现第一个问题:
Error:(146, 9) value q is not a member of StringContext
Note: implicit class Evaluate2 is not applicable here because it comes after the application point and it lacks an explicit result type
q"""
^
编译报出上述错误.
我使用的是spark-1.4.1, scala_2.10; q"""这种语法, 是2.11才正式支持的,2.10如果也想支持,就需要一些特殊的组件安装和处理.
但是这都不是问题的本质, 本质是: 虽然我的IDEA空间里有spark的源码project, 我编译的是应用程序,为什么IDEA连着把spark-1.4.1的源码都编译了?
Eclipse中是可以很轻松地禁止编译所依赖的项目的, 我记得是Properties -> Java Build Path -> Order and Export -> 取消勾选Maven Dependencies;
但是IDEA中,我找了半天都没找到如何禁掉编译依赖的项目的开关;最后只好这样处理: Debug Configurations -> Before launch, 删除了make选项;
这样的问题就是: 每次修改程序要运行前, 都需要手动"Build -> Compile 'SparkSQLTest.scala'"一次!
程序终于编译通过了,开始运行。。。。报错!
2 出现第二个问题:
Exception in thread "main" org.apache.spark.sql.AnalysisException: no such table ewaplog; line 1 pos 14
at org.apache.spark.sql.catalyst.analysis.package$AnalysisErrorAt.failAnalysis(package.scala:42)
at org.apache.spark.sql.catalyst.analysis.Analyzer$ResolveRelations$.getTable(Analyzer.scala:225)
at org.apache.spark.sql.catalyst.analysis.Analyzer$ResolveRelations$$anonfun$apply$7.applyOrElse(Analyzer.scala:233)
at org.apache.spark.sql.catalyst.analysis.Analyzer$ResolveRelations$$anonfun$apply$7.applyOrElse(Analyzer.scala:229)
at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$3.apply(TreeNode.scala:222)
at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$3.apply(TreeNode.scala:222)
at org.apache.spark.sql.catalyst.trees.CurrentOrigin$.withOrigin(TreeNode.scala:51)
at org.apache.spark.sql.catalyst.trees.TreeNode.transformDown(TreeNode.scala:221)
at org.apache.spark.sql.catalyst.trees.TreeNode$$anonfun$4.apply(TreeNode.scala:242)
看样子是没有找到hive库地址; 使用spark-sql时,spark-sql脚本会知道要在Spark安装目录中,去找conf/hive-site.xml; 但是IDEA运行时,就不知道如何去找哪里找hive-site.xml了.
先要知道spark如何使用hive-site.xml文件,才能知道应该如何将这个文件"送"给IEDA;
在 HiveContext.scala源码中:
-
def newTemporaryConfiguration(): Map[String, String] = {
-
val tempDir = Utils.createTempDir()
-
val localMetastore = new File(tempDir, "metastore").getAbsolutePath
-
val propMap: HashMap[String, String] = HashMap()
-
// We have to mask all properties in hive-site.xml that relates to metastore data source
-
// as we used a local metastore here.
-
HiveConf.ConfVars.values().foreach { confvar =>
-
if (confvar.varname.contains("datanucleus") || confvar.varname.contains("jdo")) {
-
propMap.put(confvar.varname, confvar.defaultVal)
-
}
-
}
原来Spark SQL是复用了Hive的HiveConf类来读取hive-site.xml中! 这我就比较熟了: HiveConf是通过在Classpath中依次寻找hive-site.xml的。于是在IDEA中显式制定classpath:
File->Project Structure->Modules->Dependencies->Add->Jars or Directories, 选中spark安装目录下的conf目录:
选择Classes,然后将Scope修改为"Runtime";
再次运行程序, 又出错了!
3 出现第三个问题:
Caused by: org.datanucleus.store.rdbms.connectionpool.DatastoreDriverNotFoundException: The specified datastore driver ("com.mysql.jdbc.Driver") was not found in the CLASSPATH. Please check your CLASSPATH specification, and the name of the driver.
at org.datanucleus.store.rdbms.connectionpool.AbstractConnectionPoolFactory.loadDriver(AbstractConnectionPoolFactory.java:58)
at org.datanucleus.store.rdbms.connectionpool.BoneCPConnectionPoolFactory.createConnectionPool(BoneCPConnectionPoolFactory.java:54)
at org.datanucleus.store.rdbms.ConnectionFactoryImpl.generateDataSources(ConnectionFactoryImpl.java:238)
又想了一下: Spark连接Mysql的jar包还没有加入到IDEA的classpath,所以Spark无法连接Hive的Metastore!
于是又为这个jar包设置了一次classpath:
别忘了修改Scope为“Runtime”;
再次运行。。。。。。又报错了!
4 出现第四个问题:
java.lang.OutOfMemoryError: PermGen space
这个问题,需要设置IEDA的JVM参数: -Xms256m -Xmx512m -XX:PermSize=256m -XX:MaxPermSize=256M
再次运行程序。。。。。终于成功了!!!!!!