在尝试从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)我在火花外壳中完成了同样的任务,在该任务中,最后三行代码将直接打印值。我主要怀疑import语句,因为这就是IDE和Shell之间的区别所在。
发布于 2016-09-15 08:04:32
您需要使用SQLContext实例。因此,您可以尝试如下所示:
sqlContext = sql.SQLContext(sc)
df = sqlContext.createDataFrame(rdd, ["id", "score"]).collect()关于电火花文档的更多细节。
https://stackoverflow.com/questions/39505943
复制相似问题