案例: jdbc 操作hive数据库

迷南。 2022-01-15 02:11 426阅读 0赞

1.依赖jar包:注意版本冲突的问题

  1. hive-exec-0.13.1.jar
  2. hive-jdbc-0.13.1.jar
  3. hive-metastore-0.13.1.jar
  4. hive-service-0.13.1.jar
  5. hadoop-client-1.2.1.jar
  6. hadoop-common-2.3.0.jar
  1. 编写 jdbc 代码:

    public class HiveJdbcTest {

  1. static {
  2. try {
  3. Class.forName("org.apache.hive.jdbc.HiveDriver");
  4. } catch (ClassNotFoundException e) {
  5. e.printStackTrace();
  6. System.exit(1);
  7. }
  8. }
  9. public static void main(String[] args) throws SQLException {
  10. try {
  11. Statement stmt = null;
  12. try {
  13. Connection con = DriverManager.getConnection("jdbc:hive2://hive数据库主机:hive数据库端口/hive数据库名称", "用户名", "密码");
  14. stmt = con.createStatement();
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. return;
  18. }
  19. String createSQL = "create table table01(id string,name string )";
  20. boolean res = stmt.execute(createSQL);
  21. System.out.println(" createTableInHive: end exec !! res:" + !res);
  22. // String sql =
  23. // "create view if not exists 库名.view03 (id ,name) comment '批量' as select id,name from 库名.table01";
  24. // System.out.println("Running3: " + sql);
  25. // boolean temp = stmt.execute(sql);
  26. // System.out.println(" createViewInHive: end exec !! temp:" + !temp);
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }

发表评论

表情:
评论列表 (有 0 条评论,426人围观)

还没有评论,来说两句吧...

相关阅读