Java: pass parameters in spark sql query

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Java: pass parameters in spark sql query

Mann Du
Hello there,

I am trying to  pass parameters in spark.sql query in Java code, the same as in this link

The link suggested to use 's' before 'select' as - 
val param = 100
 spark.sql(s""" select * from table where parameter = $param """)

it works for scala and python, but it doesn't work for Java. I also tried .format() but failed as well. 

Does anyone know how to dynamically pass parameters in sql using Java? Greatly appreciate your help.

Best,

Mann

Reply | Threaded
Open this post in threaded view
|

Re: Java: pass parameters in spark sql query

Ramandeep Singh
That's string interpolation. You could create your own for example :bind and then do replaceall, to replace named parameter. 

On Wed, Nov 28, 2018, 18:55 Mann Du <[hidden email] wrote:
Hello there,

I am trying to  pass parameters in spark.sql query in Java code, the same as in this link

The link suggested to use 's' before 'select' as - 
val param = 100
 spark.sql(s""" select * from table where parameter = $param """)

it works for scala and python, but it doesn't work for Java. I also tried .format() but failed as well. 

Does anyone know how to dynamically pass parameters in sql using Java? Greatly appreciate your help.

Best,

Mann

965
Reply | Threaded
Open this post in threaded view
|

回复:Java: pass parameters in spark sql query

965
In reply to this post by Mann Du
Hi I think java's native grammer is not  support that ,you should do it by declaring   a  variable  with java ' way like 
int parameter = 100;
 spark.sql(" select * from table where parameter = "+parameter)


------------------ 原始邮件 ------------------
发件人: "Mann Du"<[hidden email]>;
发送时间: 2018年11月29日(星期四) 上午7:55
收件人: "user"<[hidden email]>;
主题: Java: pass parameters in spark sql query

Hello there,

I am trying to  pass parameters in spark.sql query in Java code, the same as in this link

The link suggested to use 's' before 'select' as - 
val param = 100
 spark.sql(s""" select * from table where parameter = $param """)

it works for scala and python, but it doesn't work for Java. I also tried .format() but failed as well. 

Does anyone know how to dynamically pass parameters in sql using Java? Greatly appreciate your help.

Best,

Mann