2016-04-26 32 views
1

首先抱歉問這裏的基本疑問,但是下面的解釋還是可以肯定的..獨立Scala程序會利用分佈式/並行處理嗎?還是火花Scala需要單獨的代碼?

我對scala和火花很陌生,所以我的疑問是如果我寫一個獨立的scala程序,並在火花上執行它(1個主人3個工作人員),scala程序是否會利用受干擾/並行處理,或者我需要編寫一個單獨的程序以獲得分佈式處理的優勢?

例如,我們有一個scala代碼,將特定的格式化文件處理爲逗號分隔文件,它將一個目錄作爲輸入並解析所有文件並將輸出寫入單個文件(每個文件通常爲100-200MB) 。所以這裏是代碼。

import scala.io.Source 
import java.io.File 
import java.io.PrintWriter 
import scala.collection.mutable.ListBuffer 
import java.util.Calendar 

//import scala.io.Source 
//import org.apache.spark.SparkContext 
//import org.apache.spark.SparkContext._ 
//import org.apache.spark.SparkConf 


object Parser { 

    def main(args:Array[String]) { 

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

    var inp = new File(args(0)) 
    var ext: String = "" 
    if(args.length == 1) 
    { ext = "log" } else { ext = args(1) } 

    var files: List[String] = List("") 

    if (inp.exists && inp.isDirectory) { 
     files = getListOfFiles(inp,ext) 
     } 
    else if(inp.exists) { 
     files = List(inp.toString) 
    } 
else 
{ 
    println("Enter the correct Directory/File name"); 
    System.exit(0); 
} 

    if(files.length <=0) 
    { 
    println(s"No file found with extention '.$ext'") 
    } 
    else{ 
      var out_file_name = "output_"+Calendar.getInstance().getTime.toString.replace(" ","-").replace(":","-")+".log" 
      var data = getHeader(files(0)) 
      var writer=new PrintWriter(new File(out_file_name)) 
      var record_count = 0 

      //var allrecords = data.mkString(",")+("\n") 
      //writer.write(allrecords) 
      for(eachFile <- files) 
       { 
        record_count += parseFile(writer,data,eachFile) 
       } 
      writer.close() 
      println(record_count +s" processed into $out_file_name") 

    } 
//all func are defined here. 
} 

從特定的目錄文件正在使用scala.io Source.fromFile(文件).getLines

所以我的疑問是將上面的代碼(單機PRG)可以分佈火花系統上執行讀?我會得到並行處理的優勢嗎?

OK,怎麼樣使用SC讀取文件,將它然後使用,如果我編輯的頂級代碼使用SC的,然後它會處理分佈式處理的

val conf = new SparkConf().setAppName("fileParsing").setMaster("local[*]") 
val sc = new SparkContext(conf) 
... 
... 
for(eachFile <- files) 
       { 
        record_count += parseFile(sc,writer,data,eachFile) 
       } 

------------------------------------ 
def parseFile(......) 
sc.textFile(file).getLines 

所以分配火花系統。

回答

0

如果您運行使用​​提供的應用程序,那麼您將使用而不是完全使用Spark羣集。你必須重寫它以使用SparkContext。請通讀Spark Programming Guide

+0

謝謝你,所以如果我用SC來讀取文件,那麼,程序會使用火花處理功能嗎? –

+0

如果您按照編程指南建議的方式使用SC,那麼是的,它將使用Spark。 – climbage

1

不,它不會。要使用Spark使用分佈式計算,您需要使用SparkContext。

0

在Youtube上觀看一些介紹性視頻以瞭解Apache Spark如何工作是非常有幫助的。

例如,這些: https://www.youtube.com/watch?v=7k4yDKBYOcw https://www.youtube.com/watch?v=rvDpBTV89AM&list=PLF6snu5Jy-v-WRAcCfWNHks7lcNO-zrTI&index=4

是非常重要的,瞭解其使用的火花。

使用星火可以給你多服務器集羣上分佈處理的優勢「分佈式處理的優勢。」因此,如果您稍後將您的應用程序移至羣集,則使用Spark模型和相應的API開發應用程序是有意義的。

那麼,您可以在本地機器上本地運行Spark應用程序,但在這種情況下,您將無法獲得Spark提供的所有優勢。

無論如何,正如之前所說,Spark是一個特殊的框架,它有自己的開發庫。所以你必須使用Spark上下文和Spark API來重寫你的應用程序,也就是像RDD或Dataframes這樣的特殊對象和相應的方法。