【问题标题】:unbound method createDataFrame()未绑定方法 createDataFrame()
【发布时间】:2016-09-15 08:03:02
【问题描述】:

我在尝试从 RDD 创建 DataFrame 时遇到错误。
我的代码:

from pyspark import SparkConf, SparkContext
from pyspark import sql


conf = SparkConf()
conf.setMaster('local')
conf.setAppName('Test')
sc = SparkContext(conf = conf)
print sc.version

rdd = sc.parallelize([(0,1), (0,1), (0,2), (1,2), (1,10), (1,20), (3,18), (3,18), (3,18)])

df = sql.SQLContext.createDataFrame(rdd, ["id", "score"]).collect()

print df

错误:

df = sql.SQLContext.createDataFrame(rdd, ["id", "score"]).collect()
TypeError: unbound method createDataFrame() must be called with SQLContext 
           instance as first argument (got RDD instance instead)

我在 spark shell 中完成了相同的任务,其中直接的最后三行代码将打印值。我主要怀疑 import 语句,因为这就是 IDE 和 Shell 之间的区别。

【问题讨论】:

    标签: apache-spark pyspark


    【解决方案1】:

    您需要使用 SQLContext 的实例。因此,您可以尝试以下方法:

    sqlContext = sql.SQLContext(sc)
    df = sqlContext.createDataFrame(rdd, ["id", "score"]).collect()
    

    更多详情请见pyspark documentation

    【讨论】:

      猜你喜欢
      • 2010-11-04
      • 2017-11-06
      • 2013-12-25
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      • 2011-04-03
      • 1970-01-01
      相关资源
      最近更新 更多