----------------------------------------------Program 1: Traverse a table----------------------------------------------
Package jd.com.dawn;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import Java.util.TimeZone;
Import com.facebook.presto.jdbc.PrestoConnection;
Import com.facebook.presto.jdbc.PrestoStatement;
public class Prestoclient {
public static void Printrow (ResultSet rs,int[]types) throws SQLException
{
for (int i=0;i<types.length;i++)
System.out.print (Rs.getobject (i+1));
System.out.println ("");
}
public static void Connect () throws SQLException {
Set the time zone where you have to set
Timezone.setdefault (Timezone.gettimezone ("Asia/shanghai"));
try {
Class.forName ("Com.facebook.presto.jdbc.PrestoDriver");
} catch (ClassNotFoundException e) {
E.printstacktrace ();
}
Prestoconnection connection = null;
try {
Hive in the connection string is the catalog name, SYS is the schema name, and DDD is the user name, which is set by the actual business and is used to identify the user who executes the SQL, but does not authenticate with the user name, but it must be written. Password is directly specified as null
Connection = (prestoconnection) drivermanager.getconnection (
"Jdbc:presto://coordinator IP Address: Coordinator port number/hive/sys", "ddd", null);
} catch (SQLException e) {
E.printstacktrace ();
}
Prestostatement statement = null;
try {
statement = (prestostatement) connection.createstatement ();
} catch (SQLException e) {
E.printstacktrace ();
}
String query = "SELECT * from Node";
ResultSet rs = null;
try {
rs = statement.executequery (query);
} catch (SQLException e) {
E.printstacktrace ();
}
int Cn=rs.getmetadata (). getColumnCount ();
Int[] Types=new INT[CN];
for (int i=1;i<=cn;i++)
{
Types[i-1]=rs.getmetadata (). Getcolumntype (i);
}
try {
while (Rs.next ()) {
Printrow (rs,types);
}
} catch (SQLException e) {
E.printstacktrace ();
}
}
public static void Main (string[] args) throws ClassNotFoundException,
SQLException {
Connect ();
}
}
----------------------------------------------Program 2:sql query concurrency Description----------------------------------------------
Package test;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Import java.util.Properties;
Import com.facebook.presto.jdbc.PrestoConnection;
public class Prestojdbctest
{
public static void Main (string[] args)
{
Properties Properties = new properties ();
Properties.setproperty ("User", "HADP");
try {
Prestoconnection connect = (prestoconnection) drivermanager.getconnection ("Jdbc:presto://coordinator Address: Port/hive/ Employees "," Presto user name ", NULL);
Connect.settimezoneid ("UTC");
String SQL1 = "Show Tables";
String SQL2 = "SELECT * FROM Dept_emp limit 5";
Statement stat = connect.createstatement ();
The following code is illustrated below:
If you do not traverse rs1 and rs2 one time through the two while loop, just perform resultset rs1 = Stat.executequery (SQL1), and resultset rs2 = Stat.executequery (SQL2) ; Then these two queries are executed in parallel.
The purpose of using the two while loop below is to ensure that the SQL2 is executed after SQL1 execution is complete.
ResultSet rs1 = Stat.executequery (SQL1);
while (Rs1.next ()) {
String int1 = rs1.getstring (1);
System.out.println (INT1);
}
ResultSet rs2 = Stat.executequery (SQL2);
while (Rs2.next ()) {
String str = rs2.getstring (1);
System.out.println (str);
}
}
catch (SQLException e) {
E.printstacktrace ();
}
}
}
This article is from the "Smile" blog, make sure to keep this source http://lvxin1986.blog.51cto.com/4953500/1662886
Presto using JDBC link cluster code-Lu Xin (original)