Miss Zhu 2022-02-13 08:38:45 阅读数:663
need hive-jdbc-3.1.2-standalone.jar package , be located hive\apache-hive-3.1.2-bin\jdbc Under the directory of :
stay eclipse in , Create a new one Java Project, Create a new lib Folder , Will be more than jar Copy the package in , And then in Java Bulid Path Add the library to the , The project structure is as follows :
To write Java Code , Inquire about the in the previous article word_count What's in the table :
package a;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestHive {
public static void main(String[] args) {
// register jdbc drive
try {
Class.forName("org.apache.hive.jdbc.HiveDriver");
// Create connection
Connection conn = DriverManager.getConnection("jdbc:hive2://192.168.150.31:10000/hive", "root", "Hive123+");
// establish SQL actuator
Statement st = conn.createStatement();
// perform SQL sentence , Get the result set
String sql = "select * from word_count";
ResultSet rs = st.executeQuery(sql);
// Processing results
while (rs.next()) {
System.out.println(rs.getString("word") + " " + rs.getInt("count"));
}
// close resource
rs.close();
st.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Run the program , The results can be seen in the console :
copyright:author[Miss Zhu],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130838428938.html