【问题标题】:Spark scala running火花斯卡拉运行
【发布时间】:2016-04-27 07:10:21
【问题描述】:

您好,我是 spark 和 scala 的新手。我在 spark scala 提示符下运行 scala 代码。该程序很好,它显示“定义的模块 MLlib”,但它没有在屏幕上打印任何内容。我做错了什么?有没有其他方法可以在 scala shell 中运行这个程序 spark 并获得输出?

 import org.apache.spark.{SparkConf, SparkContext}
    import org.apache.spark.mllib.classification.LogisticRegressionWithSGD
    import org.apache.spark.mllib.feature.HashingTF
    import org.apache.spark.mllib.regression.LabeledPoint

object MLlib {

  def main(args: Array[String]) {
    val conf = new SparkConf().setAppName(s"Book example: Scala")
    val sc = new SparkContext(conf)

    // Load 2 types of emails from text files: spam and ham (non-spam).
    // Each line has text from one email.
    val spam = sc.textFile("/home/training/Spam.txt")
    val ham = sc.textFile("/home/training/Ham.txt")

    // Create a HashingTF instance to map email text to vectors of 100 features.
    val tf = new HashingTF(numFeatures = 100)
    // Each email is split into words, and each word is mapped to one feature.
    val spamFeatures = spam.map(email => tf.transform(email.split(" ")))
    val hamFeatures = ham.map(email => tf.transform(email.split(" ")))

    // Create LabeledPoint datasets for positive (spam) and negative (ham) examples.
    val positiveExamples = spamFeatures.map(features => LabeledPoint(1, features))
    val negativeExamples = hamFeatures.map(features => LabeledPoint(0, features))
    val trainingData = positiveExamples ++ negativeExamples
    trainingData.cache() // Cache data since Logistic Regression is an iterative algorithm.

    // Create a Logistic Regression learner which uses the LBFGS optimizer.
    val lrLearner = new LogisticRegressionWithSGD()
    // Run the actual learning algorithm on the training data.
    val model = lrLearner.run(trainingData)

    // Test on a positive example (spam) and a negative one (ham).
    // First apply the same HashingTF feature transformation used on the training data.
    val posTestExample = tf.transform("O M G GET cheap stuff by sending money to ...".split(" "))
    val negTestExample = tf.transform("Hi Dad, I started studying Spark the other ...".split(" "))
    // Now use the learned model to predict spam/ham for new emails.
    println(s"Prediction for positive test example: ${model.predict(posTestExample)}")
    println(s"Prediction for negative test example: ${model.predict(negTestExample)}")

    sc.stop()
  }
}

【问题讨论】:

    标签: scala apache-spark apache-spark-mllib


    【解决方案1】:

    有几点:

    您在 Spark shell 中定义了 object,因此不会立即调用 main 类。定义 object 后,您必须显式调用它:

    MLlib.main(Array())

    事实上,如果您继续在 shell/REPL 上工作,您可以完全取消该对象;你可以直接定义函数。例如:

    import org.apache.spark.{SparkConf, SparkContext}
    import org.apache.spark.mllib.classification.LogisticRegressionWithSGD
    import org.apache.spark.mllib.feature.HashingTF
    import org.apache.spark.mllib.regression.LabeledPoint
    
    def MLlib {
        //the rest of your code
    }
    

    但是,您不应该在 shell 中初始化 SparkContext 它。来自documentation

    在 Spark shell 中,一个特殊的解释器感知 SparkContext 是 已经在名为 sc 的变量中为您创建了。制作你自己的 SparkContext 将不起作用

    因此,您必须从代码中删除该位,或者将其编译到 jar 中并使用 spark-submit 运行它

    【讨论】:

    • 感谢您的回复!我试过 MLlib.main(Array()) 但它不工作
    • 当您说它不起作用时,除非您说出您尝试过什么以及遇到了什么错误,否则我将无法帮助您。请提供更多信息,以便我能尽力帮助您。
    • 很好,您使用了 MLib.main,但您并没有单独离开 SparkContext。您不需要声明 conf 和 sc 的两行代码。每个 spark shell 已经有一个 spark shell sc
    猜你喜欢
    • 2020-11-05
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 2019-03-07
    • 1970-01-01
    相关资源
    最近更新 更多