spark:attributeerror:'sqlcontext'对象没有属性'createdataframe'

7kqas0il  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(390)

如何从dict创建Dataframe?我使用以下代码并遇到错误。

from pyspark import SparkContext, SQLContext
sc = SparkContext.getOrCreate()
spark = SQLContext(sc)

result_dict = {'a':3,'b':44}
data = list(map(list, result_dict.items()))
f_rdd = spark.createDataFrame(data, ["A", "B"]).repartition(1)

错误:

AttributeError                      Traceback (most recent call last)
<ipython-input-10-a25453caa1c3> in <module>
      5 result_dict = {'a':3,'b':44}
      6 data = list(map(list, result_dict.items()))
----> 7 f_rdd = spark.createDataFrame(data, ["A", "B"]).repartition(1)

AttributeError: 'SQLContext' object has no attribute 'createDataFrame'
q3aa0525

q3aa05251#

你可以这样试试

from pyspark.sql import SparkSession

    spark = SparkSession.builder \
        .appName('so')\
        .getOrCreate()

    sc= spark.sparkContext

    map = {'a':3,'b':44}
    data = sc.parallelize([(k,v) for k,v in map.items()]).toDF(['A','B'])

    data.show()

    # +---+---+
    # |  A|  B|
    # +---+---+
    # |  a|  3|
    # |  b| 44|
    # +---+---+

相关问题