Spark學習之第一個程序打包、提交任務到集群
ssh-keygen
cd .ssh
touch authorized_keys
cat id_rsa.pub > authorized_keys
chmod 600 authorized_keys
2、環境工具
2.1環境
系統 urbuntu jdk 1.7.0_79
scala 2.10.4
hadoop 2.6.0
spark 1.6.2
2.2打包工具
IDEA + sbt1.2打包工具
3.打包
3.1安裝插件
需要預先安裝scala插件,點擊File ->Setting ->Plugins ->輸入框輸入scala->install
安裝完成需要重啟IDE
3.2創建項目
File -> New Project ->Scala -> SBT 選擇相應版本 ->finish
3.3編寫代碼
build.sbt 添加spark相關依賴
name := "demoPro"
version := "1.0"
scalaVersion := "2.10.4"
libraryDependencies += "org.apache.spark" % "spark-core_2.10" % "1.6.2"
創建WordCount.scala,編寫如下代碼
import org.apache.spark.{SparkContext, SparkConf} /** * Created by Administrator on 2018/2/20. */ object WordCount { def main(args: Array[String]) { val conf = new SparkConf().setAppName("wordcount") val sc = new SparkContext(conf) val input = sc.textFile("/home/dell/helloSpark.txt") val lines = input.flatMap(line => (line.split(" "))) val count = lines.map(word => (word, 1)).reduceByKey { case (x, y) => x + y } val output=count.saveAsTextFile("/home/dell/helloSparkRes") } }
3.4打包
File -> Project Structure -> Aritifacts -> 點擊+號 ->jar -> 第二個 -> 指定Module和 MainClass -> JAR files from libraries 選擇第二個 ->點擊ok
主題欄點擊Build -> Build Aritifacts - Build
在工程目下out目錄中生成相應jar包即打包成功
4.提交任務
4.1啟動hadoop
#進入sbin目錄
cd $Hadoop_HOME/sbin
#啟動hadoop集群
start-all.sh
4.2上傳測試文件到hdfs
hadoop fs -put test.txt /test/test.txt
4.3上傳程序jar包
是同filelize 或者sftp 或者 rz -y命令上傳程序jar
4.4 提交任務
4.4.1啟動Master
sudo ./start-master.sh
訪問localhost:8080 獲取spark://xxx:7077
4.4.2啟動Worker
sudo ./bin/spark-class org.apache.spark.deploy.worker.Worker spark://dell:7077
4.4.3提交作業
sudo ./bin/spark-submit --master spark://dell:7077 --class WordCount /home/dell/demopro.jar
5、查看測試程序是否正確
5.1、查看 是否生成文件夾 進入文件查看程序是否正確
5.2、進入文件查看程序是否正確
Spark學習之第一個程序打包、提交任務到集群