标签:presto
----------------------------------------------程序1: 遍历某个表----------------------------------------------
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 {
//设置时区,这里必须要设置
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
try {
Class.forName("com.facebook.presto.jdbc.PrestoDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
PrestoConnection connection = null;
try {
//连接字符串中的hive是catalog名字,sys是schema名字,ddd是用户名,这个用户名根据实际业务自己设定,用来标示执行sql的用户,但是不会通过该用户名进行身份认证,但是必须要写。密码直接指定为null
connection = (PrestoConnection) DriverManager.getConnection(
"jdbc:presto://Coordinator IP地址:Coordinator端口号/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();
}
}
----------------------------------------------程序2: SQL查询并发性说明----------------------------------------------
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地址:端口/hive/employees", "Presto用户名", null);
connect.setTimeZoneId("UTC");
String sql1 = "show tables";
String sql2 = "select * from dept_emp limit 5";
Statement stat = connect.createStatement();
//对以下代码说明如下:
//如果不通过两个while循环一次对rs1和rs2进行遍历,而只是执行ResultSet rs1 = stat.executeQuery(sql1);和ResultSet rs2 = stat.executeQuery(sql2); 那么这两个查询会并行执行。
//下面使用两个while循环的目的是为了保证sql2在sql1执行完毕之后在执行。
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();
}
}
}
本文出自 “smile” 博客,请务必保留此出处http://lvxin1986.blog.51cto.com/4953500/1662886
标签:presto
原文地址:http://lvxin1986.blog.51cto.com/4953500/1662886