Cloudera Labs
Provide feedback on Cloudera Labs
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

Using PreparedStatement with Phoenix

Using PreparedStatement with Phoenix

Contributor

I want to use PreparedStatement when setting a query to Phoenix record reader/ calculating input splits. I am not able to find other means than this -

 

PhoenixConfigurationUtil.setInputQuery(conf, query);

 

which takes in a raw String sql query. How do I change this to PreparedStatement rather than a hard coded Statement?

 

Thanks,
Sumit

3 REPLIES 3

Re: Using PreparedStatement with Phoenix

Contributor

I researched a bit more. However, I am unable to find any means to use Prep Statements with phoenix input format.

Anyone has contrary comments?

Highlighted

Re: Using PreparedStatement with Phoenix

Explorer

You can use prepared statement. For example something like this works pretty well. 

 

Connection con = DriverManager.getConnection("jdbc:phoenix:localhost");
PreparedStatement statement = con.prepareStatement("select * from TABLE");
rset = statement.executeQuery();
while (rset.next()) {
System.out.println(rset.getString("COLUMN_A"));
}
statement.close();
con.close();

 

However, this is limited. I want to run '!columns TABLE' from java, but it gives unexpected character '!'. Please let me know anyone has figured out a way to do this.

Re: Using PreparedStatement with Phoenix

Contributor

If you are only looking to get the list of columns, you can use a degenerate query like -

SELECT * FROM TBL WHERE 1 = 0

 

And then use java.sql.ResultSetMetaData to iterate over the columns.