0

我正在尝试连接到 Hive2 数据库,但出现以下错误:

java.lang.NoSuchFieldError:HIVE_SERVER2_TRANSPORT_MODE

这是我要运行的代码。基本上,我只是想连接到 hive2 数据库并使用用户定义的查询的结果填充 jTable:

import java.sql.Connection;
import org.apache.hadoop.hive.jdbc.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import javax.swing.JTable;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.Cursor;
import javax.swing.*;
import net.proteanit.sql.DbUtils;
import org.apache.hive.jdbc.*;
import org.apache.hive.jdbc.Utils;
import org.apache.hive.*;
import org.apache.hive.service.*;
import org.apache.http.*;
import org.apache.hive.service.CompositeService;
import org.apache.hive.service.cli.CLIService;
import org.apache.hive.service.cli.thrift.*;
import org.apache.hive.service.server.*;
import org.apache.hadoop.hive.conf.HiveConf;

public class SQLSelect extends javax.swing.JFrame {

    /**
     * Creates new form SQLSelect
     */

    ResultSet r;
    Statement s;

    public SQLSelect() {
        initComponents();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
                Cursor cursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR); 
                setCursor(cursor);

                try {
            Class.forName("org.apache.hive.jdbc.HiveDriver");
                        //Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.getMessage();
        }
        try {

                String locationString = "jdbc:hive2://" + jTextField1.getText() + ":" + jTextField2.getText();
                String userString = jTextField3.getText();
                char password[] = jPasswordField1.getPassword();
                String pString = new String(password);

                System.out.println(locationString + userString + pString);

                Connection conn = DriverManager.getConnection(locationString,userString,pString);

        //Connection conn = DriverManager.getConnection("jdbc:hive://76.99.192.2:9999","user","password");
        s = conn.createStatement();
        r = s.executeQuery(jTextArea1.getText());
        //System.out.println(r.getNString(1));
        }
        catch (Exception e)
        {
                        setCursor(Cursor.DEFAULT_CURSOR);
                        JOptionPane.showMessageDialog(null, e.getMessage(), "SQL Error", JOptionPane.ERROR_MESSAGE);
            jLabel9.setText(e.getMessage());
        }
                try {
                MainPanel resultsPanel = new jdbctohive.MainPanel();
                resultsPanel.jTable1.setModel(DbUtils.resultSetToTableModel(r));
                resultsPanel.setTitle("Hive SQL Management Studio");
                resultsPanel.setVisible(true);

                setCursor(Cursor.DEFAULT_CURSOR);
                this.setVisible(false);
                }

                catch (Exception e)
                {
                    setCursor(Cursor.DEFAULT_CURSOR);
                }

任何帮助将非常感激!

4

1 回答 1

0

我终于可以连接了。我之前的声明建议使用“;auth=noSasl”,结果证明是错误的。经过多次尝试,这是最终起作用的代码。当我在 Windows 中工作时,我遇到了需要 winutils.exe 的 Hadoop Win 版本的问题。由于 HortonWorks 的产品 1.3 有 Win 实现,我下载了它并从 .MSI 文件中提取了文件。如果在 linux 中工作,您可能不需要这个。我发现的关键之一是实现 jdbc 和 hadoop 日志记录,因为没有这些,我简单地卡在 getConnection 永远不会返回......但是,有一些潜在的错误,我只是假设 word return 作为堆栈跟踪,但没有。祝你好运。

帮助记录的文件:C:/temp/log.properties .level=SEVERE

oracle.jdbc.level=信息

oracle.jdbc.handlers=java.util.logging.ConsoleHandler

java.util.logging.ConsoleHandler.level=INFO

java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter

    Logger logger = org.apache.log4j.Logger.getRootLogger();

    logger.setLevel(Level.INFO);
    try {
        Class.forName("org.apache.hive.jdbc.HiveDriver");

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        System.exit(1);
    } catch (Exception e){
        e.printStackTrace();
    }
    try{
        File propFile = new File("C:/temp/log.properties");
        LogManager logManager = LogManager.getLogManager();
        logManager.readConfiguration(new FileInputStream(propFile));

        DriverManager.setLogWriter(new PrintWriter(System.err));

        Logger l2 = Logger.getLogger("org.apache.hadoop.util.Shell");
        Logger l3 = Logger.getLogger("java.sql.DriverManager");
        Layout simple = new SimpleLayout();
        Appender newAppender = new ConsoleAppender(simple);
        logger.addAppender(newAppender);
        logger.setLevel(Level.ALL);
        l2.setLevel(Level.ALL);
        l2.addAppender(newAppender);
        l3.setLevel(Level.ALL);
        l3.addAppender(newAppender);

        System.setProperty("HADOOP_HOME", "C:\\Development\\hadoopCore\\hadoop-2.2.0");
        logger.debug("Logging works");
        Connection con = DriverManager.getConnection("jdbc:hive2://192.168.218.152:10000/default", "", "");//Hortonworks Hive ODBC Driver
        if (con != null)
            con.close();
        System.out.println("got the connection");
于 2013-10-29T17:05:32.940 回答