【问题标题】:pyspark: How to obtain the Spark SQLContext of the spark dataframe?pyspark:如何获取 spark 数据帧的 Spark SQLContext?
【发布时间】:2020-07-03 06:07:30
【问题描述】:

我有一个接受 spark DataFrame 的函数,我想获取 DataFrame 所在的 Spark 上下文。

原因是我想得到SQLContext 这样我就可以运行一些 SQL 查询

sql_Context = SQLContext(output_df.sparkContext())
sql_Context.registerDataFrameAsTable(output_df, "table1")
sql_Context.sql("select * from table1") # or some more complicated query

当然output_df.sparkContext() 不起作用。获取 Spark DataFrame 的 sparkContext 的正确方法是什么?

【问题讨论】:

    标签: apache-spark pyspark


    【解决方案1】:

    output_df.rdd.context 完成这项工作

    【讨论】:

    • 我已经编辑了答案。该属性实际上称为context 而不是sparkContext。对此感到抱歉
    【解决方案2】:

    output_df.sql_ctx 就是答案

    【讨论】:

      猜你喜欢
      • 2020-02-04
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 2016-11-24
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多