【问题标题】:Compare return of count from sql query in python/databricks?比较python/databricks中sql查询的计数返回?
【发布时间】:2021-09-03 20:54:10
【问题描述】:

我想返回sql计数结果,如果计数大于0我想诱导一个失败函数。

但是,我遇到了一个错误。我认为这是因为 id_of_zero_count 是一个字符串,不能与整数进行比较?

id_of_zero_count = sql(""" SELECT count(*) cnt FROM schema.table where ID = 0 """.format(val))
#BU.collect[0][0] returns the value of the first row & first column

display(id_of_zero_count)

if id_of_zero_count > 0:
  print("Quality check not passed")
  induce_fail_func()

这是返回的错误:

TypeError: '>' not supported between instances of 'DataFrame' and 'int'

TypeError                                 Traceback (most recent call last)
<command-873419207778593> in <module>
----> 1 if id_of_zero_count > 0:
      2   print("Quality check not passed")
      3   induce_fail_func()

TypeError: '>' not supported between instances of 'DataFrame' and 'int'

【问题讨论】:

    标签: python apache-spark-sql databricks


    【解决方案1】:

    sql 查询返回一个数据框,您应该在显示调用中看到它。

    您可以改为获取第一行的第一个元素:

    if id_of_zero_count.first()[0] > 0:
      print("Quality check not passed")
      induce_fail_func()
    

    【讨论】:

    • 返回 TypeError: 'DataFrame' object is not callable
    猜你喜欢
    • 1970-01-01
    • 2020-03-09
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多