pom.xml
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-metastore</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-service</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0</version>
</dependency>
代码
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.sql.*;
@SpringBootApplication
public class ImpalademoApplication {
public static void main(String[] args) {
//SpringApplication.run(ImpalademoApplication.class, args);
test();
}
// set the impalad host
private static final String IMPALAD_HOST = "192.168.91.143";
// port 21050 is the default impalad JDBC port
private static final String IMPALAD_JDBC_PORT = "10000";
private static final String CONNECTION_URL = "jdbc:hive2://" + IMPALAD_HOST + ':' + IMPALAD_JDBC_PORT + "/myhive";
private static final String JDBC_DRIVER_NAME = "org.apache.hive.jdbc.HiveDriver";
public static void test() {
System.out.println("\n=============================================");
System.out.println("Cloudera Impala JDBC Example");
System.out.println("Using Connection URL: " + CONNECTION_URL);
Connection con = null;
try {
Class.forName(JDBC_DRIVER_NAME);
con = DriverManager.getConnection(CONNECTION_URL,"hive","");
insert(con);
PreparedStatement stmt = con.prepareStatement("SELECT * FROM student2");
ResultSet rs = stmt.executeQuery();
System.out.println("\n== Begin Query Results ======================");
// print the results to the console
while (rs.next()) {
// the example query returns one String column
System.out.print(rs.getString("id") + "\t");
System.out.print(rs.getString("name") + "\t");
System.out.println(rs.getString("address"));
}
System.out.println("== End Query Results =======================\n\n");
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (Exception e) {
// swallow
}
}
}
public static void insert(Connection con) {
String sql = " insert into student2 select id,name,address from student";
try {
PreparedStatement stmt = con.prepareStatement(sql);
stmt.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}