第一步:创建项目 并导入hive相关jar包
第二步:编写代码如下:
import java.sql.Statement;import java.sql.DriverManager;
import org.apache.tools.ant.taskdefs.Echo;
public class TestHive2 {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
Connection con = DriverManager.getConnection("jdbc:hive2://192.168.222.3:10000/default", "hive", "");
Statement stmt = con.createStatement();
String sql = "SELECT * FROM people_new";
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println("id: "+String.valueOf(res.getString(1)) + "\t" + "name: "+String.valueOf(res.getString(2)) + "\t" + "hobby: "+String.valueOf(res.getString(3)));
System.out.println("\n");
}
}
}
第三步:运行结果如下: