您好,登錄后才能下訂單哦!
這篇文章主要介紹了Spark中會遇到什么坑,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
1.1 Scala與Intellij集成報錯
在Scala安裝成功后,準備到Intellij上寫Scala代碼,發現Scala都配好了(關于如何配置,網上資料很多),結果運行Scala程序時報錯。
錯誤:
Error:scalac: Multiple 'scala-library*.jar' files (scala-library.jar, scala-library.jar, scala-library.jar) in Scala compiler classpath in Scala SDK scala-sdk-2.12.2
解決方法:在OverStackflow上找到了思路。在Intellij中打開project structure,刪除已有的Scala的路徑(我的Scala是安裝在/usr/local/Cellar/scala/2.12.2路徑下的),重新添加/usr/local/Cellar/scala/2.12.2/idea/lib目錄即可。
改動前
改動后
1.2 Scala語法Intellij不認
在Intellij中寫了一個Scala的HelloWorld,代碼如下
/** * Created by jackie on 17/5/7. */ package com.jackie.scala.s510 object HelloWorld { def main(args: Array[String]): Unit = { println("hello world") println(increaseAnother(5)); println(Array(1,2,3,4).map{(x:Int)=>x+1}.mkString(",")); println(Array(1,2,3,4) map{(x:Int)=>x+1} mkString(",")); println(Array(1,2,3,4) map{(x:Int)=>x+1} mkString(",")); // test object var person = new Person() person.name_=("john") // name_=()對應java中的setter方法 println("Person name:" + person.name) person.name = "Jackie" println("Person name:" + person.name) var mp = new MyPerson() mp.name_("alihaha") println("MyPerson name:" + person.name) var pwp = new PersonWithParam("Jackie", 18) println("PersonWithParam:" + pwp.toString()) } def increaseAnother(x: Int): Int = x + 1 }
運行的時候,報錯mkString無法識別。
錯誤:mkString can't be resolved
解決方法:需要交代下我各個環境的版本參數,Intellij-14.0, jdk-8, scala-2.12.2。但是在Intellij中能選擇的Scala***版本只有2.11,所有后來將Intellij升級到2017.1版本,這時候還報錯Error:scalac: Error: org.jetbrains.jps.incremental.scala.remote.ServerException,然后在Intellij中打開project structure,將scala由2.12.2換成2.11.7,問題解決。
1.3 Spark與Intellij集成的問題
Spark環境都安裝好了,所以想在Intellij中運行Spark程序,但是在添加了Spark的相關依賴后,發現無法編譯通過。
錯誤:
Exception NoSuchMethodError: com.google.common.collect.MapMaker.keyEquivalence
解決方法:實現聲明,之前在maven中一直引用的都是spark-core2.10,這時候報錯,我定位問題出在Guava上,然后找到所有間接依賴了Guava的jar,都exclude,問題還是沒有解決。期間添加了Spark的很多依賴,試了都不行,***試了下Spark-core2.11,問題解決(有的時候版本的兼容性真的很坑)。
1.4 hadoop上傳本地文件到HDFS
如果想將本地文件上傳到HDFS,使用hadoop fs -put localDir hdfsDir,前提是保證hadoop啟動。
錯誤:
jackie@jackies-MacBook-Pro:~|⇒ hadoop fs -put ~/Documents/doc/README.md / 17/05/13 10:56:39 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable 17/05/13 10:56:40 WARN ipc.Client: Failed to connect to server: localhost/127.0.0.1:8020: try once and fail. java.net.ConnectException: Connection refused at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) at org.apache.hadoop.net.SocketIOWithTimeout.connect(SocketIOWithTimeout.java:206) at org.apache.hadoop.net.NetUtils.connect(NetUtils.java:531) at org.apache.hadoop.net.NetUtils.connect(NetUtils.java:495) at org.apache.hadoop.ipc.Client$Connection.setupConnection(Client.java:681) at org.apache.hadoop.ipc.Client$Connection.setupIOstreams(Client.java:777) at org.apache.hadoop.ipc.Client$Connection.access$3500(Client.java:409) at org.apache.hadoop.ipc.Client.getConnection(Client.java:1542) at org.apache.hadoop.ipc.Client.call(Client.java:1373) at org.apache.hadoop.ipc.Client.call(Client.java:1337) at org.apache.hadoop.ipc.ProtobufRpcEngine$Invoker.invoke(ProtobufRpcEngine.java:227) at org.apache.hadoop.ipc.ProtobufRpcEngine$Invoker.invoke(ProtobufRpcEngine.java:116) at com.sun.proxy.$Proxy10.getFileInfo(Unknown Source) at org.apache.hadoop.hdfs.protocolPB.ClientNamenodeProtocolTranslatorPB.getFileInfo(ClientNamenodeProtocolTranslatorPB.java:787) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.apache.hadoop.io.retry.RetryInvocationHandler.invokeMethod(RetryInvocationHandler.java:398) at org.apache.hadoop.io.retry.RetryInvocationHandler$Call.invokeMethod(RetryInvocationHandler.java:163) at org.apache.hadoop.io.retry.RetryInvocationHandler$Call.invoke(RetryInvocationHandler.java:155) at org.apache.hadoop.io.retry.RetryInvocationHandler$Call.invokeOnce(RetryInvocationHandler.java:95) at org.apache.hadoop.io.retry.RetryInvocationHandler.invoke(RetryInvocationHandler.java:335) at com.sun.proxy.$Proxy11.getFileInfo(Unknown Source) at org.apache.hadoop.hdfs.DFSClient.getFileInfo(DFSClient.java:1700) at org.apache.hadoop.hdfs.DistributedFileSystem$27.doCall(DistributedFileSystem.java:1436) at org.apache.hadoop.hdfs.DistributedFileSystem$27.doCall(DistributedFileSystem.java:1433) at org.apache.hadoop.fs.FileSystemLinkResolver.resolve(FileSystemLinkResolver.java:81) at org.apache.hadoop.hdfs.DistributedFileSystem.getFileStatus(DistributedFileSystem.java:1433) at org.apache.hadoop.fs.Globber.getFileStatus(Globber.java:64) at org.apache.hadoop.fs.Globber.doGlob(Globber.java:282) at org.apache.hadoop.fs.Globber.glob(Globber.java:148) at org.apache.hadoop.fs.FileSystem.globStatus(FileSystem.java:1685) at org.apache.hadoop.fs.shell.PathData.expandAsGlob(PathData.java:326) at org.apache.hadoop.fs.shell.CommandWithDestination.getRemoteDestination(CommandWithDestination.java:195) at org.apache.hadoop.fs.shell.CopyCommands$Put.processOptions(CopyCommands.java:256) at org.apache.hadoop.fs.shell.Command.run(Command.java:164) at org.apache.hadoop.fs.FsShell.run(FsShell.java:315) at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:76) at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:90) at org.apache.hadoop.fs.FsShell.main(FsShell.java:378) put: Call From jackies-macbook-pro.local/192.168.73.56 to localhost:8020 failed on connection exception: java.net.ConnectException: Connection refused; For more details see: http://wiki.apache.org/hadoop/ConnectionRefused
解決方法:進入hadoop安裝目錄(我的是/usr/local/Cellar/hadoop)進入sbin下執行./start-all.sh啟動hadoop服務。
1.5 Spark啟動
上篇在配置Spark時沒有配置spark-defaults.conf文件,所以在Spark安裝目錄下(我的是/usr/local/Spark)啟動./start-all.sh出錯。
錯誤:
spark-shell Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties Setting default log level to "WARN". To adjust logging level use sc.setLogLevel(newLevel). 17/05/13 13:42:49 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable 17/05/13 13:42:51 WARN StandaloneAppClient$ClientEndpoint: Failed to connect to master 192.168.73.56:7077 org.apache.spark.SparkException: Exception thrown in awaitResult at org.apache.spark.rpc.RpcTimeout$$anonfun$1.applyOrElse(RpcTimeout.scala:77) at org.apache.spark.rpc.RpcTimeout$$anonfun$1.applyOrElse(RpcTimeout.scala:75) at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36) at org.apache.spark.rpc.RpcTimeout$$anonfun$addMessageIfTimeout$1.applyOrElse(RpcTimeout.scala:59) at org.apache.spark.rpc.RpcTimeout$$anonfun$addMessageIfTimeout$1.applyOrElse(RpcTimeout.scala:59) at scala.PartialFunction$OrElse.apply(PartialFunction.scala:167) at org.apache.spark.rpc.RpcTimeout.awaitResult(RpcTimeout.scala:83) at org.apache.spark.rpc.RpcEnv.setupEndpointRefByURI(RpcEnv.scala:88) at org.apache.spark.rpc.RpcEnv.setupEndpointRef(RpcEnv.scala:96) at org.apache.spark.deploy.client.StandaloneAppClient$ClientEndpoint$$anonfun$tryRegisterAllMasters$1$$anon$1.run(StandaloneAppClient.scala:106) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: java.io.IOException: Failed to connect to /192.168.73.56:7077
解決方法:將Spark安裝目錄下的conf中的spark-defaults.conf.template拷貝一份出來,重命名為spark-defaults.conf,按照https://sanwen8.cn/p/3bac5Bj.html配置好,再啟動Spark,發現還是報錯
https://sanwen8.cn/p/3bac5Bj.html Using Spark's default log4j profile: org/apache/spark/log4j-defaults.properties Setting default log level to "WARN". To adjust logging level use sc.setLogLevel(newLevel). 17/05/13 14:19:12 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable 17/05/13 14:19:15 ERROR SparkContext: Error initializing SparkContext. java.net.ConnectException: Call From jackies-MacBook-Pro.local/192.168.73.56 to 192.168.73.56:8021 failed on connection exception: java.net.ConnectException: Connection refused; For more details see: http://wiki.apache.org/hadoop/ConnectionRefused at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
于是按照StackOverflow,將spark-defaults.conf中的spark.eventLog.enabled由true改為false,之后再啟動成功。
注意:這里我反復配置了localhost和自己的ip,來回切換,最終證明只要在/etc/hosts中配置好ip對應映射的名稱,可以直接用名稱即可,不用寫ip,而且要保持hadoop中的配置文件和spark中的配置文件要一致,否則針對會精疲力盡。
1.6 將運算任務交給Spark運行的報錯
運行下面的一個Demo程序
package com.jackie.scala.s513; import org.apache.spark.SparkConf; import org.apache.spark.api.java.JavaPairRDD; import org.apache.spark.api.java.JavaRDD; import org.apache.spark.api.java.JavaSparkContext; import org.apache.spark.api.java.function.FlatMapFunction; import org.apache.spark.api.java.function.Function2; import org.apache.spark.api.java.function.PairFunction; import scala.Tuple2; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.regex.Pattern; /** * Created by jackie on 17/5/13. */ public class Simple { private static final Pattern SPACE = Pattern.compile(" "); public static void main(String[] args) throws Exception { //創建一個RDD對象 SparkConf conf=new SparkConf().setAppName("Simple").setMaster("local"); //創建spark上下文對象,是數據的入口 JavaSparkContext spark=new JavaSparkContext(conf); //獲取數據源 JavaRDD<String> lines = spark.textFile("hdfs://jackie:8020/"); /** * 對于從數據源得到的DStream,用戶可以在其基礎上進行各種操作, * 對于當前時間窗口內從數據源得到的數據首先進行分割, * 然后利用Map和ReduceByKey方法進行計算,當然***還有使用print()方法輸出結果; */ JavaRDD<String> words = lines.flatMap(new FlatMapFunction<String, String>() { @Override public Iterator<String> call(String s) { return Arrays.asList(SPACE.split(s)).iterator(); } }); //使用RDD的map和reduce方法進行計算 JavaPairRDD<String, Integer> ones = words.mapToPair( new PairFunction<String, String, Integer>() { @Override public Tuple2<String, Integer> call(String s) { return new Tuple2<String, Integer>(s, 1); } }); JavaPairRDD<String, Integer> counts = ones.reduceByKey( new Function2<Integer, Integer, Integer>() { @Override public Integer call(Integer i1, Integer i2) { return i1 + i2; } }); List<Tuple2<String, Integer>> output = counts.collect(); for (Tuple2<?,?> tuple : output) { //輸出計算結果 System.out.println(tuple._1() + ": " + tuple._2()); } spark.stop(); } }
這個程序需要讀取HDFS上根目錄下的README.md文件,但是在此之前我執行了"hadoop namenode -format"(注意,這個操作引起了后面的一系列問題)。所以就準備重新使用hadoop fs -put localDir hdfsDir上傳README.md,結果這時候報錯
錯誤:
hadoop fs -put /Users/jackie/Documents/doc/README.md / 17/05/13 15:47:15 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable 17/05/13 15:47:16 WARN hdfs.DataStreamer: DataStreamer Exception org.apache.hadoop.ipc.RemoteException(java.io.IOException): File /README.md._COPYING_ could only be replicated to 0 nodes instead of minReplication (=1). There are 0 datanode(s) running and no node(s) are excluded in this operation. at org.apache.hadoop.hdfs.server.blockmanagement.BlockManager.chooseTarget4NewBlock(BlockManager.java:1733) at org.apache.hadoop.hdfs.server.namenode.FSDirWriteFileOp.chooseTargetForNewBlock(FSDirWriteFileOp.java:265) at org.apache.hadoop.hdfs.server.namenode.FSNamesystem.getAdditionalBlock(FSNamesystem.java:2496) at org.apache.hadoop.hdfs.server.namenode.NameNodeRpcServer.addBlock(NameNodeRpcServer.java:828)
后來發現是datanode沒有啟動,然后開始找datanode沒有啟動的原因,在這里http://www.aboutyun.com/thread-7931-1-1.html
文中解釋:當我們執行文件系統格式化時,會在namenode數據文件夾(即配置文件中dfs.name.dir在本地系統的路徑)中保存一個current/VERSION文件,記錄namespaceID,標識了所格式化的 namenode的版本。如果我們頻繁的格式化namenode,那么datanode中保存(即配置文件中dfs.data.dir在本地系統的路徑)的current/VERSION文件只是你***次格式化時保存的namenode的ID,因此就會造成datanode與namenode之間的id不一致。
解決方法:采取的做法是根據執行hadoop namenode –format得到成功的提示。
這時候再執行jps命令,我們就可以看到datanode了
類似的,同樣是在執行hadoop fs -put /Users/jackie/Documents/doc/README.md /是報錯如下
hadoop fs -put /Users/jackie/Documents/doc/README.md / 17/05/15 09:51:04 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable 17/05/15 09:51:05 WARN ipc.Client: Failed to connect to server: jackie/192.168.73.56:8020: try once and fail. java.net.ConnectException: Connection refused at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) at org.apache.hadoop.net.SocketIOWithTimeout.connect(SocketIOWithTimeout.java:206) at org.apache.hadoop.net.NetUtils.connect(NetUtils.java:531) at org.apache.hadoop.net.NetUtils.connect(NetUtils.java:495) at org.apache.hadoop.ipc.Client$Connection.setupConnection(Client.java:681) at org.apache.hadoop.ipc.Client$Connection.setupIOstreams(Client.java:777) at org.apache.hadoop.ipc.Client$Connection.access$3500(Client.java:409)
一開始以為是ip的配置問題,但是反復修改無果,后來發現使用jps時,沒有啟動namenode,于是在網上找http://blog.csdn.net/bychjzh/article/details/7830508
于是在/usr/local/Cellar/hadoop/hdfs下刪除原來在core-site.xml中配置的tmp目錄,然后新建了hadoop_tmp目錄,并在core-site.xml中修改成
<property> <name>hadoop.tmp.dir</name> <value>/usr/local/Cellar/hadoop/hdfs/hadoop_tmp</value> <description>A base for other temporary directories.</description> </property>
并執行hadoop namenode –format,***在使用start-all.sh啟動所有的服務,執行上傳文件成功
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Spark中會遇到什么坑”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。