【问题标题】:Moving Spark DataFrame from Python to Scala whithn Zeppelin使用 Zeppelin 将 Spark DataFrame 从 Python 迁移到 Scala
【发布时间】:2016-09-12 19:07:18
【问题描述】:

我在 Zeppelin 的 Python 段落中创建了一个 spark DataFrame。

sqlCtx = SQLContext(sc)
spDf = sqlCtx.createDataFrame(df)

df 是一个熊猫数据框

print(type(df))
<class 'pandas.core.frame.DataFrame'>

我想要做的是将spDf 从一个 Python 段落移动到另一个 Scala 段落。看起来合理的做法是使用z.put

z.put("spDf", spDf)

我得到了这个错误:

AttributeError: 'DataFrame' object has no attribute '_get_object_id'

有什么建议可以解决这个错误吗?或者有什么移动spDf的建议?

【问题讨论】:

    标签: python scala apache-spark apache-spark-sql apache-zeppelin


    【解决方案1】:

    您可以put 内部 Java 对象而不是 Python 包装器:

    %pyspark
    
    df = sc.parallelize([(1, "foo"), (2, "bar")]).toDF(["k", "v"])
    z.put("df", df._jdf)
    

    然后确保使用正确的类型:

    val df = z.get("df").asInstanceOf[org.apache.spark.sql.DataFrame]
    // df: org.apache.spark.sql.DataFrame = [k: bigint, v: string]
    

    但最好注册临时表:

    %pyspark
    
    # registerTempTable in Spark 1.x
    df.createTempView("df")
    

    并使用SQLContext.table 阅读:

    // sqlContext.table in Spark 1.x
    val df = spark.table("df")
    
    df: org.apache.spark.sql.DataFrame = [k: bigint, v: string]
    

    要反向转换,请参阅Zeppelin: Scala Dataframe to python

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-09
      • 2018-09-01
      • 1970-01-01
      • 2018-09-26
      • 2016-02-27
      • 2019-10-22
      • 2020-05-22
      • 2016-11-29
      相关资源
      最近更新 更多