1

我有Intellij IDEA社區版。我需要爲配置用於IntellJ的Apache Spark提供幫助。我想Scala..I使用crealytics獲得來自GA數據..Intellij安裝scala和spark

我build.sbt:

name := "scala-project-test" 

version := "1.0" 

scalaVersion := "2.11.8" 

libraryDependencies ++= Seq(
    "org.apache.spark" %% "spark-core" % "1.6.1", 
    "org.apache.spark" %% "spark-sql" % "1.6.1", 
    "com.crealytics" % "spark-google-analytics_2.11" % "0.8.1" 
) 

和analytics.scala

import org.apache.spark.sql.SQLContext 

object analytics { 


    val sqlContext = new SQLContext(sc) 
    val df = sqlContext.read 
    .format("com.crealytics.google.analytics") 
    .option("serviceAccountId", "[email protected]") 
    .option("keyFileLocation", "/Users/userABC/IdeaProjects/scala-project-test/xxxx.p12") 
    .option("ids", "ga:xxxxxx") 
    .option("startDate", "7daysAgo") 
    .option("endDate", "yesterday") 
    .option("dimensions", "date,browser,city") 
    .option("queryIndividualDays", "true") 
    .load() 

    df.select("browser", "users").show() 
} 

當我運行對象分析,有是錯誤: 找不到:值sc

我認爲有關於Spark配置的問題,因爲sc是SparkContext,但我不知道它在哪裏。

任何提示?

回答

0

簡短的回答

val conf = createSparkConf 
    val sc = new SparkContext(conf) 
    val sqlContext = new SQLContext(sc) 

.....

一般來說,我使用SparkContextFactory

object SparkContextFactory { 

    val conf = createSparkConf 
    val sc = new SparkContext(conf) 

    def createSparkSQLContext: SQLContext = { 
    val sc = new SparkContext(conf) 
    val sQLContext = new SQLContext(sc) 
    sQLContext 
    } 

    def stopSparkContext() = sc.stop() 

    private def createSparkConf: SparkConf = { 
    val conf = ConfigLoader.conf 

    val masterPath = conf.getString("spark.master.path") 

    new SparkConf() 
     .setMaster(masterPath).setAppName("SparkContext") 
     .set("spark.driver.allowMultipleContexts" , "true") 
    } 
} 

在配置文件中,你需要設置一個主URL這是個什麼樣子like for local box

spark.master.path="local[*]" 

set("spark.driver.allowMultipleContexts" , "true") 

僅僅是本地測試我使用工廠這樣

val sqlc = SparkContextFactory.createSparkSQLContext 
+0

此外,如果你不想給VM參數或進入配置文件中像spark.master.path = 「local [*]」,同時創建SparkConf,你也可以指定你的配置。 val conf = new SparkConf()。setAppName(「App Name」)。setMaster(「local」)。 – Nishan