【问题标题】:Cannot pass variables to a spark sql query in pyspark无法将变量传递给 pyspark 中的 spark sql 查询
【发布时间】:2018-01-03 18:48:03
【问题描述】:

我有一个日期数据类型的 python 变量(我正在使用 pyspark): 变量值为2016-10-31

print type(load_dt)

 >> <type 'datetime.date'>

我很难将它传递给 sparksql 查询:

    hive_context.sql("select * from  tbl t1 where cast (substring(t1.dt,1,10) as date) ={0}".format(load_dt));

    Error:

    u"cannot resolve '(cast(substring(dt,1,10) as date) = ((2016 - 10) - 31))' due to data type mismatch: differing types in '(cast(substring(period_dt,1,10) as date) = ((2016 - 10) - 31))'
 (date and int)

【问题讨论】:

    标签: python sql apache-spark pyspark


    【解决方案1】:

    添加引号:

    "select * from  tbl t1 where cast (substring(t1.dt,1,10) as date) = '{0}'"
    

    否则日期将转换为2016-10-31字符串并解释为算术表达式:

    2016 - 10 - 31 
    

    【讨论】:

      猜你喜欢
      • 2021-11-12
      • 2019-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多