【问题标题】:How do I print out a spark.sql object?如何打印出 spark.sql 对象?
【发布时间】:2020-09-03 17:05:08
【问题描述】:

我有一个包含几个变量的 spark.sql 对象。

import com.github.nscala_time.time.Imports.LocalDate

val first_date = new LocalDate(2020, 4, 1)
val second_date = new LocalDate(2020, 4, 7)

val mydf = spark.sql(s"""
        select *
        from tempView
        where timestamp between '{0}' and '{1}'
""".format(start_date.toString, end_date.toString))

我想打印出mydf,因为我运行了mydf.count,结果为0。

我跑了mydf 又回来了mydf: org.apache.spark.sql.DataFrame = [column: type]

我也试过println(mydf),但没有返回查询。

有这个相关的question,但是没有答案。

如何打印查询?

【问题讨论】:

    标签: apache-spark pyspark apache-zeppelin


    【解决方案1】:

    最简单的方法是将您的查询存储到 variable,然后打印变量以获取查询。

    • spark.sql中使用variable

    Example:

    In Spark-scala:

    val start_date="2020-01-01"
    val end_date="2020-02-02"
    val query=s"""select * from tempView where timestamp between'${start_date}' and '${end_date}'"""
    print (query)
    //select * from tempView where timestamp between'2020-01-01' and '2020-02-02'
    
    spark.sql(query)
    

    In Pyspark:

    start_date="2020-01-01"
    end_date="2020-02-02"
    query="""select * from tempView where timestamp between'{0}' and '{1}'""".format(start_date,end_date)
    
    print(query)
    #select * from tempView where timestamp between'2020-01-01' and '2020-02-02'
    
    #use same query in spark.sql
    spark.sql(query)
    

    【讨论】:

    • 我尝试了第二个选项。我在“query=”之前添加了“val”,打印输出显示select * from tempView where timestamp between'{0}' and '{1}'
    • 我认为您正在使用 scala 解释器,然后使用 val query=s"""select * from tempView where timestamp between'${start_date}' and '${end_date}'"""
    【解决方案2】:

    它在 PySpark 中。

    start_date="2020-01-01"
    end_date="2020-02-02"
    q="select * from tempView where timestamp between'{0}' and '{1}'".format(start_date,end_date)
    
    print(q)
    

    这里是在线运行版本:https://repl.it/repls/FeistyVigorousSpyware

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      相关资源
      最近更新 更多