简体   繁体   English

使用 Scala 将数据帧的结果作为另一个数据帧的变量

[英]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.

相关问题 使用Scala重命名相对于另一个数据框的数据框的列名 - Rename column names of a dataframe with respect to another dataframe using scala 如何使用 spark/scala 从不在另一个数据帧中的数据帧中获取行 - how to take lines from a dataframe that are not in another dataframe using spark/scala Spark Scala:使用另一个 dataframe 使用 function 构建新列 - Spark Scala: build a new column using a function using another dataframe 使用 Spark Dataframe (Scala) 中的另一列数组创建一列数组 - Creating a column of array using another column of array in a Spark Dataframe (Scala) 如何使用spark scala在dataframe的另一列中找到substring的position - How to find position of substring in another column of dataframe using spark scala 使用Spark Scala检查一个数据框列中的值是否在另一数据框列中存在 - Check if value from one dataframe column exists in another dataframe column using Spark Scala Scala Spark数据框联接结果未按首选顺序排列 - Scala Spark dataframe join result not in preferred order 在 Dataframe 列中添加函数的结果 [Spark Scala] - Adding the result of a function in a Dataframe column [Spark Scala] 使用 Scala 将 JavapairRDD 转换为数据帧 - transform JavapairRDD to dataframe using scala 使用 Scala 对数据帧进行 MinMax 转换 - MinMax transformation on dataframe using Scala
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM