大家都知道spark 1.6.0版本比較穩定,也比較流行。
我們項目組也是,最初用的就是這個版本。
這段時間,項目組引入spark 2.1.0版本,我想嘗嘗鮮。
Pom中剛剛換了dependency馬上編譯失敗了。
首先是在1.6中用的最多的trait之一org.apache.spark.Logging 在2.1中變成了org.apache.spark.internal.Logging
看着internal就覺得不對勁,細看定義果然:
private[spark] trait Logging {…}
而1.6中的定義是這樣兒的
@DeveloperApi
trait Logging {…}
看不懂二者的區別沒關系,因為當你把
import org.apache.spark.Logging
改成
import org.apache.spark.internal.Logging
IDEA會提醒你“Symbol Logging is inaccessible from this place”
簡單說你不能用。
還是得回去理解源碼啊。
1.6中的注解 @DeveloperApi就不用解釋什么了,開發者API
2.1中的private[spark]是啥意思呢?
private[SomePackage] means, it is visible inside the package only (no modifier in Java)
protected[SomePackage] means, it can be seen in sub-classes but only if they are inside SomePackage
好吧,只能在package中用…
我不死心,又去官方API 看了下,赫然寫着:
“NOTE: DO NOT USE this class outside of Spark. It is intended as an internal utility. This will likely be changed or removed in future releases.”
人家早就打算只是內部使用了。。。
那就沒有替代方案了嗎。。。
目前看來只能老老實實地像下面這樣使用了:
protected final val logger : Logger= LoggerFactory.getLogger(this.getClass())
…
logger.info("handline file:{}",f.getPath)
另外,版本升級是一個漸進的過程,pom.xml中可能既存在1.6的包又存在2.1的包,以下這個異常可能會讓人迷惑:
java.lang.NoClassDefFoundError: org/codehaus/commons/compiler/UncheckedCompileException java.lang.ClassNotFoundException: org.codehaus.commons.compiler.UncheckedCompileException
罪魁禍首是庫共享編譯器。
把以下dependency添加到pom.xml中就OK了:
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>commons-compiler</artifactId>
<version>2.7.8</version>
</dependency>
等有其他的東西,再補充。
參考:
http://spark.apache.org/docs/1.6.0/api/scala/index.html#org.apache.spark.Logging
https://stackoverflow.com/questions/42352091/spark-sql-fails-with-java-lang-noclassdeffounderror-org-codehaus-commons-compil?s=1|20.5422