如何将变量传递给 SQL 字符串

How to pass variable into SQL string

我有一个接受值的 spark sql 查询。

Dataset getQuery = spark.sql("select * from trafficdata where message_time between 1486036800000 and 1486108800000 ")

我希望这个时间像

这样的变量
Long val1 = 1486036800000
Long val2 = 1486108800000
Dataset getQuery = spark.sql("select * from trafficdata where message_time between $val1 and $val2 ")

我试过 $val1 但它不起作用。谁能建议,如何在 java 中做到这一点?

我认为您应该将查询写成

"select * from trafficdata where message_time between '" + val1 + "' and '" + val2 + "'"

试试这样:

getQuery = spark.sql(String.format("select * from trafficdata where message_time between %d and %d ", val1, val2));