问题描述
如果将ssh放入presto emr群集的主节点上,则可以运行查询.但是,我希望能够在连接到emr群集的本地计算机上从Java源代码运行查询.我使用默认配置设置了presto emr群集.
If I ssh onto the master node of my presto emr cluster, I can run queries. However, I would like to be able to run queries from java source code on my local machine that connects to the emr cluster. I set up my presto emr cluster with default configurations.
我尝试了端口转发,但是它似乎仍然无法正常工作.创建连接时,我将其打印出来,它是"com.facebook.presto.jdbc.PrestoConnection@XXXXXXX",但是我仍然怀疑它是否已真正连接,因为我无法执行任何查询,并且总是超时.
I have tried port forwarding, but it still does not seem to work. When I create the connection, I print it out and it is "com.facebook.presto.jdbc.PrestoConnection@XXXXXXX" but I still have doubts if it is actually connected since I can't execute any queries and it always times out.
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.*;
public class PrestoJDBC {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.facebook.presto.jdbc.PrestoDriver";
//static final String JDBC_DRIVER = "com.teradata.presto.jdbc42.Driver";
static final String DB_URL = "jdbc:presto://ec2-XX-XX-XXX-XX.us-east-2.compute.amazonaws.com:8889/hive/default";
// Database credentials
static final String USER = "hadoop";
static final String PASS = "";
public static void main(String[] args) throws URISyntaxException {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName(JDBC_DRIVER);
//STEP 3: Open a connection
//conn = DriverManager.getConnection(DB_URL,USER,PASS);
conn = DriverManager.getConnection(DB_URL,USER,PASS);
//STEP 4: Execute a query
stmt = conn.createStatement();
System.out.println(conn);
String sql;
sql = "select * from onedaytest where readOnly=true;";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while(rs.next()){
//Display values
System.out.println(rs.getString(3));
}
//STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
se.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Done");
}
}
我得到一个java.sql.SQLEXception:执行查询时出错,以及一个java.net.SocketTimeoutException.我想知道是否还有其他配置或需要设置才能查询的内容.
I get a java.sql.SQLEXception:Error executing query, and a java.net.SocketTimeoutException. I was wondering if there are any other configurations or things I need to setup to be able to query.
推荐答案
这可能是由于防火墙所致,它可能不允许您连接到服务器.您必须在主"组中创建一个入站规则.
This could be because of firewall, which might not be allowing you to connect to the server. You would have to create an inbound rule in the Master group.
- 为此,首先选择您的集群.
- 在安全和访问"标题下,单击主服务器的安全组"链接.
- 现在选择名称为"ElasticMapReduce-master"的安全组,然后单击编辑入站规则".
- 在底端,单击添加规则",然后在类型中选择所有TCP",然后在源字段中选择我的ip",或在要访问的位置添加ip地址.
- 单击保存规则.
再试一次.会的!!
这篇关于无法使用Java JDBC从AWS EMR上的Presto连接/查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!