我想在数据框中的现有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'。但他们给出了错误。
有人可以帮我解决这个问题
答案 0 :(得分:2)
试试这个:
val resultDF = sqlContentext.sql(s"""SELECT
id,
name,
city,
"$strDate" AS process_time
FROM TEMP""".stripMargin).show(1)