[英]Result of Dataframe as a variable to another dataframe using Scala
In my scenario, I need to get a result from a dataframe and use that result as a variable to another dataframe.在我的场景中,我需要从数据帧中获取结果并将该结果用作另一个数据帧的变量。
val newdate = spark.sqlContext.sql("select interval_startdt from FROMCOSMOS order by interval_startdt ASC limit 1")
.collectAsList.toString.replaceAll("[\\[\\]]","'")
RESULT : newdate: String = ''2014-06-27 00:00:00''
val finalresult = spark.sqlContext.sql("select * from Table2 where interval_startdt='$newdate'").show
The above one doesn't give me any values but when I insert the actual date it gives me the result.上面的没有给我任何值,但是当我插入实际日期时,它给了我结果。
Can anyone help me to solve this one.谁能帮我解决这个问题。
I see a couple of problems with your query:我发现您的查询存在一些问题:
First, you're trying to use string interpolation ( '$newdate'
), but you didn't put s"
in front of your sql string, so value of newdate
is not in the query actually.首先,您尝试使用字符串插值(
'$newdate'
),但您没有将s"
放在 sql 字符串前面,因此newdate
值实际上不在查询中。
Second, as far as I understand, in Table2
, column interval_startdt
is probably of type "date" or "timestamp".其次,据我了解,在
Table2
,列interval_startdt
可能是“日期”或“时间戳”类型。 So you should probably rewrite your queue like "select * from Table2 where interval_startdt= to_timestamp('$newdate', "YYYY-MM-DD hh:mm:ss")"
所以你应该重写你的队列,比如
"select * from Table2 where interval_startdt= to_timestamp('$newdate', "YYYY-MM-DD hh:mm:ss")"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.