为什么我不能连接到我的hive db?

pnwntuvh  于 2021-05-30  发布在  Hadoop
关注(0)|答案(2)|浏览(449)

**结束。**此问题需要详细的调试信息。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

6年前关门了。
改进这个问题
我是新来的Hive。我正在尝试这个代码输入链接描述在这里
这是我的密码

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()执行时,它将永远运行。所以它不会给出任何异常或错误。所以请帮我解决这个问题。

atmip9wb

atmip9wb1#

在命令行上启动hiveserver服务,然后尝试运行hive server hive--service hiveserver&

66bbxpm5

66bbxpm52#

最后我得到了答案。我使用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);
        }
    }
}

以下是所需的jar。

相关问题