IDEA MAVEN SPARK SCALA打包办法

采用jar提交集群模式流程为:

本地完成代码开发 –> 本地编译打包 -> 提交集群执行

创建三层包

需要先创建三层package(eg:cn.nokia.bigdata),然后在package下创建object,如下图

IDEA MAVEN SPARK SCALA打包办法_第1张图片
Paste_Image.png

稍微修改了下官方例子

package cn.nokia.bigdata

import org.apache.spark.{SparkConf, SparkContext}
// $example on$
import org.apache.spark.mllib.classification.{LogisticRegressionModel, LogisticRegressionWithLBFGS}
import org.apache.spark.mllib.evaluation.MulticlassMetrics
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.util.MLUtils
// $example off$

object Test {
  
  def main(args: Array[String]): Unit = {
    // val conf = new SparkConf().setAppName("LogisticRegressionWithLBFGSExample")

    val conf = new SparkConf().setAppName("LogisticRegressionWithLBFGSExample").setMaster("local[*]")
    val sc = new SparkContext(conf)

    // $example on$
    // Load training data in LIBSVM format.
    //val data = MLUtils.loadLibSVMFile(sc, "file:///usr/local/spark-2.1.0/data/mllib/sample_libsvm_data.txt")

    val data = MLUtils.loadLibSVMFile(sc, "D:\\spark\\data\\mllib\\sample_libsvm_data.txt")
    // Split data into training (60%) and test (40%).
    val splits = data.randomSplit(Array(0.6, 0.4), seed = 11L)
    val training = splits(0).cache()
    val test = splits(1)

    // Run training algorithm to build the model
    val model = new LogisticRegressionWithLBFGS()
      .setNumClasses(10)
      .run(training)

    // Compute raw scores on the test set.
    val predictionAndLabels = test.map { case LabeledPoint(label, features) =>
      val prediction = model.predict(features)
      (prediction, label)
    }

    // Get evaluation metrics.
    val metrics = new MulticlassMetrics(predictionAndLabels)
    val accuracy = metrics.accuracy
    println(s"Accuracy = $accuracy")

    // Save and load model
    model.save(sc, "target/tmp/scalaLogisticRegressionWithLBFGSModl")
    val sameModel = LogisticRegressionModel.load(sc,
      "target/tmp/scalaLogisticRegressionWithLBFGSModel")
    // $example off$

    sc.stop()
  }
}
// scalastyle:on println

当前项目结构

IDEA MAVEN SPARK SCALA打包办法_第2张图片
Paste_Image.png

打开项目结构

File -> Project Structure:

IDEA MAVEN SPARK SCALA打包办法_第3张图片
Paste_Image.png

快捷按钮

IDEA MAVEN SPARK SCALA打包办法_第4张图片
Paste_Image.png

artifact => + => jar

IDEA MAVEN SPARK SCALA打包办法_第5张图片
Paste_Image.png

选择主类:

IDEA MAVEN SPARK SCALA打包办法_第6张图片
Paste_Image.png

输出设置

IDEA MAVEN SPARK SCALA打包办法_第7张图片
Paste_Image.png

编译

IDEA MAVEN SPARK SCALA打包办法_第8张图片
Paste_Image.png
IDEA MAVEN SPARK SCALA打包办法_第9张图片
Paste_Image.png
  • build(首次打包)
  • rebuild(重新打包)
  • clean(清理当前内容)

打包完后,可以在如下目录中找到对应jar包:

IDEA MAVEN SPARK SCALA打包办法_第10张图片
Paste_Image.png

本地提交

D:\spark\bin>spark-submit --class cn.nokia.bigdata.Test spark.jar local
IDEA MAVEN SPARK SCALA打包办法_第11张图片
Paste_Image.png
IDEA MAVEN SPARK SCALA打包办法_第12张图片
Paste_Image.png

你可能感兴趣的:(IDEA MAVEN SPARK SCALA打包办法)