我们如何在现有的数据帧sparksql中传递动态库?

时间:2017-04-11 10:26:52

标签: apache-spark apache-spark-sql

我想在数据框中的现有sql查询中添加一个包含当前时间戳的新列。我不想直接在sql查询中编写函数。

 val sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 val currentTime= new Date();
 val strDate = sdfDate.format(currentTime);
 val resultDF = sqlContentext.sql("""SELECT
                                          id,
                                          name,
                                          city,
                                         "$strDate" AS process_time 
                                          FROM TEMP""".stripMargin).show(1)

             +-----------------  +--------+-------+----------+
             |         id|             name  |    city|   process_time|
             +--------------------+----------+--------------------+----
             |dfgsywo/yyMSv...|  Surender|     CHN|    $strDate|

我也尝试了$ strDate和'$ strDate'。但他们给出了错误。

有人可以帮我解决这个问题

1 个答案:

答案 0 :(得分:2)

试试这个:

val resultDF = sqlContentext.sql(s"""SELECT
                                          id,
                                          name,
                                          city,
                                         "$strDate" AS process_time 
                                          FROM TEMP""".stripMargin).show(1)