【问题标题】:why can I not connect to my hive DB? [closed]为什么我无法连接到我的配置单元数据库? [关闭]
【发布时间】:2014-12-11 07:00:39
【问题描述】:

我是蜂巢新手。我正在尝试这段代码enter link description here

这是我的代码

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;

public class HiveJdbcClient {
    private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";

    public static void main(String[] args) throws SQLException {
        try {
            Class.forName(driverName);
            System.out.println("Drive loaded...");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.exit(1);
        }
        System.out.println("Before Connecting to hive...");
        try {
            Connection con = DriverManager.getConnection("jdbc:hive://servername:10001/test","hive","hive");
            System.out.println("Connected to hive..");
            Statement stmt = con.createStatement();
            String tableName = "testHiveDriverTable";
            ResultSet res = stmt.executeQuery("create table " + tableName + " (key int, value string)");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            System.exit(1);
        }

    }
}

当我运行程序时,它只执行驱动程序加载步骤,但是当 DriverManager.getConnection() 执行时,它会永远运行。所以它不会给出任何异常或错误。所以请帮我解决这个问题。

【问题讨论】:

  • 可能阻塞,等待连接 - 尝试调试
  • 在调试模式下它也永远运行
  • 是的,但它在哪里阻塞?
  • 阻塞意味​​着它没有获得连接。但是 Hive 服务器正在运行。
  • 所以将您的问题编辑为 - 为什么我无法连接到我的配置单元数据库?或者更确切地说,寻找答案。也许是防火墙(客户端或主机),也许是 hive db 设置?

标签: java hadoop jdbc hive hadoop2


【解决方案1】:

在命令行启动 hiveserver 服务,然后尝试, 运行配置单元服务器 hive --service hiveserver &

【讨论】:

    【解决方案2】:

    我终于得到了答案。 我使用 hiveserver2。

    这是我的有效代码。

        import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.Statement;
    
    public class HiveJdbcClient {
        private static String driverName = "org.apache.hive.jdbc.HiveDriver";
    //  org.apache.hadoop.hive.jdbc.HiveDriver 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);
            }
            System.out.println("Before Connecting to hive...");
            try {
                Connection con = DriverManager.getConnection("jdbc:hive2://<Server_name>:10001/test","hive_username","hive_password");
                System.out.println("Connected to hive..");
    
                // Select query
                Statement stmt = con.createStatement();
                String sql = "select * from test.emp";
                System.out.println("Running: " + sql);
                ResultSet res = stmt.executeQuery(sql);
                while (res.next()) {
                    System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
                }
    
                // load data into table
                // NOTE: filepath has to be local to the hive server
                // NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line
                String tableName = "test.emp";
                String filepath = "D:\\datatoload\\emp1.txt";
                sql = "load data local inpath '" + filepath + "' into table " + tableName;
                System.out.println("Running: " + sql);
                boolean result = stmt.execute(sql);
                if(!result)
                    System.out.println("Data load successfully!!!");
                else
                    System.out.println("Failed to load data!!!");
    
                // regular hive query
                sql = "select count(*) from " + tableName;
                System.out.println("Running: " + sql);
                res = stmt.executeQuery(sql);
                while (res.next()) {
                    System.out.println(res.getString(1));
                }
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
                System.exit(1);
            }
        }
    }
    

    以下是所需的罐子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-11
      • 2018-03-01
      • 1970-01-01
      • 2015-06-20
      相关资源
      最近更新 更多