在MyEclipse10上远程连接虚拟机上的Hive,报错:Could not establish connection to 192.168.1.100:10000/log: java.net.Co
我是在window10上开发的,远程连接虚拟机上的hive,结果报错:Exception in thread "main" java.sql.SQLException:Could not establish connection to 192.168.1.100:10000/log: java.net.ConnectException: Connection refused: connec
我是在window10上开发的,远程连接虚拟机上的hive,结果报错:
Exception in thread "main" java.sql.SQLException: Could not establish connection to 192.168.1.100:10000/log: java.net.ConnectException: Connection refused: connect
解决方法:在虚拟机上的终端上输入#hive - -service hiveserver
注意:当你输入上面命令后,出现Starting Hive Thrift Server等等两三行字,代表启动成功了,然后光标会一直闪烁,这时千万别切换回命令行界面,切记!切记!切记!(重要的事情说三遍,因为hive的thrift远程服务似乎是实时性的,返回命令行界面意味着退出thrift服务);现在运行myeclipse中的代码就不会报错了。当你运行成功后,你就可以切换回命令行界面了
下面贴上我的完整代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 将nginx日志数据导入到hive表中,统计每个用户进入url的次数(使用JDBC执行以上HQL)
* @author 远
*
*/
public class UvHiveJdbc {
private static Connection conn=null;
public static Connection getHiveConn(){
if(conn==null){
try {
System.out.println("==============================");
Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");
System.out.println("------------------------------");
conn=DriverManager.getConnection("jdbc:hive://192.168.1.100:10000/log","","");
System.out.println("ok");
} catch (Exception e) {
e.printStackTrace();
}
}
return conn;
}
public static void closeHiveConn() throws SQLException{
if(conn!=null){
conn.close();
}
}
public static void execDDL(String sql)throws SQLException{
Connection conn=getHiveConn();
Statement stmt=conn.createStatement();
stmt.execute(sql);
}
public static ResultSet queryData(String sql)throws SQLException{
Connection conn=getHiveConn();
Statement stmt=conn.createStatement();
ResultSet res=stmt.executeQuery(sql);
return res;
}
public static void main(String[] args) {
try {
System.out.println("start");
String sql="create external table access(ip string,day string,url string,refurl string)" +
" row format delimited fields terminated by ',' location '/uvout/'";
System.out.println(sql);
execDDL(sql);
sql="create external table userip(ip string,username string)" +
" row format delimited fields terminated by ' ' location '/userlog/'";
System.out.println(sql);
execDDL(sql);
sql="select a.day,b.username,a.url,count(a.url) from access a join userip b on a.ip=b.ip group by " +
"b.username,a.url,a.day";
System.out.println(sql);
ResultSet res=queryData(sql);
while(res.next()){
System.out.println(res.getString(1)+" "+res.getString(2)+" "+res.getString(3)+
" "+res.getString(4));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
//关闭hive连接
closeHiveConn();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
更多推荐
所有评论(0)