【问题标题】:value read is not a member of org.apache.spark.SparkContext读取的值不是 org.apache.spark.SparkContext 的成员
【发布时间】:2017-04-18 20:10:50
【问题描述】:

scala 的版本是 2.11.8 ; jdk 是 1.8 ;火花是 2.0.2

我尝试在the offical site of spark apache 中运行 LDA 模型的示例,我收到以下句子的错误消息:

val dataset = spark.read.format("libsvm")
  .load("data/libsvm_data.txt")

错误消息是:

错误:(49, 25) 读取的值不是 org.apache.spark.SparkContext 的成员 val 数据集 = spark.read.format("libsvm") ^

不知道怎么解决。

【问题讨论】:

    标签: scala apache-spark


    【解决方案1】:

    看起来您正试图通过SparkContext 调用read,而不是SQLContextSparkSession

    // New 2.0.+ API: create SparkSession and use it for all purposes:
    val session = SparkSession.builder().appName("test").master("local").getOrCreate()
    session.read.load("/file") // OK
    
    // Old <= 1.6.* API: create SparkContext, then create a SQLContext for DataFrame API usage:
    val sc = new SparkContext("local", "test") // used for RDD operations only
    val sqlContext = new SQLContext(sc) // used for DataFrame / DataSet APIs
    
    sqlContext.read.load("/file") // OK
    sc.read.load("/file") // NOT OK
    

    【讨论】:

    • 几乎接近答案,但在你的 asnwer 中需要更多的改进我试过并得到了以下 scala> val session = SparkSession.builder().appName("test").master(" local").getOrCreate() :30: error: not found: value SparkSession val session = SparkSession.builder().appName("test").master("local").getOrCreate()
    • 不确定我是否理解。但也许你错过了相关的进口? import org.apache.spark.sql.SparkSession
    【解决方案2】:

    添加这些行:

    import org.apache.spark.sql.SparkSession
    
    val session = SparkSession.builder().appName("app_name").master("local").getOrCreate()
    
    val training = session.read.format("format_name").load("path_to_file")
    

    【讨论】:

      【解决方案3】:

      sqlcontext 函数的完整语法如下

      val df = sqlContext
                      .read()
                      .format("com.databricks.spark.csv")
                      .option("inferScheme","true")
                      .option("header","true")
                      .load("path to/data.csv");
      

      如果您正在读取/写入 csv 文件

      【讨论】:

        猜你喜欢
        • 2016-02-07
        • 1970-01-01
        • 2015-01-20
        • 1970-01-01
        • 2017-11-19
        • 2020-07-17
        • 2017-01-05
        • 2013-10-17
        • 2016-02-15
        相关资源
        最近更新 更多