我如何打印出一个spark.sql对象?

问题描述 投票:0回答:1

我有一个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) 却没有返回查询。

有这样的相关 疑问但它没有答案。

我怎样才能打印出查询结果呢?

apache-spark pyspark apache-zeppelin
1个回答
1
投票

最简单的方法是将你的查询存储到一个叫做 variable 然后 印制 出变量来获取查询。

  • 使用 variablespark.sql

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)

1
投票

这是在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)

这里是onlnie运行的版本。https:/repl.itreplsFeistyVigorousSpyware。

© www.soinside.com 2019 - 2024. All rights reserved.