未绑定方法createDataFrame()

Jac*_*iel 3 apache-spark pyspark

尝试从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
Run Code Online (Sandbox Code Playgroud)

错误:

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)
Run Code Online (Sandbox Code Playgroud)

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

Dan*_*ula 6

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

sqlContext = sql.SQLContext(sc)
df = sqlContext.createDataFrame(rdd, ["id", "score"]).collect()
Run Code Online (Sandbox Code Playgroud)

pyspark文档中有更多详细信息。