`

hive之jdbc

阅读更多
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestHive {
// hive --service hiveserver2 --hiveconf hive.server2.thrift.port=10001
// if you want to change a port to start the hive2, then add the parameters

// private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
// this driver class is for hive, not hive2. please be noted
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
Connection con = DriverManager.getConnection("jdbc:hive2://192.168.1.50:10001/default",
"hadoop", "");
Statement stmt = con.createStatement();
String sql1 = "show tables";
String sql2 = "show databases";
String sql3 = "select count(1) from hive_user_info";
ResultSet rs = stmt.executeQuery(sql1);
while(rs.next()){
String s = rs.getString(1);
System.out.println(s);
}
con.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

}
}


ps:
the jdbc user should have permission of the hive execution. or it will fail.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics