1.启动hiveserver2
2.代码实现
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class HiveTest {
private Connection conn ;
@Before
public void initConn() throws Exception {
Class.forName("org.apache.hive.jdbc.HiveDriver");
conn = DriverManager.getConnection("jdbc:hive2://192.168.100.1:10000/myhive", "hadoop", "hadoop");
}
@Test
public void query() throws SQLException {
PreparedStatement ps = conn.prepareStatement("select id, name, age from myhive.u");
ResultSet rs = ps.executeQuery();
while(rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println(id+" : "+name+"