2013-10-28 81 views
0

我試圖連接到一個數據庫Hive2但我發現了以下錯誤:Hive2 JDBC錯誤

java.lang.NoSuchFieldError的:HIVE_SERVER2_TRANSPORT_MODE

這裏是我試圖運行代碼。基本上,我只是想用一個用戶自定義查詢的結果連接到hive2 DATABSE並填充一個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); 
       } 

任何幫助,將不勝感激!

回答

0

我終於能夠連接。我以前的聲明建議使用「; auth = noSasl」,結果證明是錯誤的。經過多次嘗試,這是最終工作的代碼。正如我在Windows中工作,我遇到了需要winutils.exe的Hadoop Win版本的問題。由於HortonWorks對其產品的1.3版本進行了Win實現,我下載了它並從.MSI文件中提取文件。如果你在linux下工作,你可能不需要這個。我發現的一個關鍵是實現jdbc和hadoop日誌記錄,因爲如果沒有這些,我簡單地被卡在getConnection永遠不會返回...但是,有一些潛在的錯誤,我只是把字返回爲stacktrace,但沒有。祝你好運。 C::/temp/log.properties .LEVEL =嚴重

oracle.jdbc.level = INFO

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");