Support Questions
Find answers, ask questions, and share your expertise
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.

Can't connect to Hive Apache Using JAVA - Using Port Forwarding

Highlighted

Can't connect to Hive Apache Using JAVA - Using Port Forwarding

New Contributor

Hi all

Guys, I need your support . I am getting stacked here.

I have a host which is connected to Hive Apache Impala Database , and also Kerberos is connected .

I want to connect my laptop to the Hive Database using port forwarding in the Host : That means : I want to connect to some port on some host and this Host will direct the tunnel to the Hive Server .

I have made port forwarding on the Host using a java program and it is working fine( the forwarding ). But the main java program can not connect to HiveServer . Here is the main Java program which is giving me Error :

Peer indicated failure: Unsupported mechanism type PLAIN


// Fig. 28.28: DisplayQueryResultsIMPALA.java
// Display the contents of the Authors table in the books database.

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.*;
import org.apache.poi.ss.usermodel.*;
import com.jidesoft.hssf.*;
import org.apache.hadoop.security.*;
import org.apache.hadoop.*;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.conf.*;
import java.util.Random;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;
import java.util.regex.PatternSyntaxException;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.JTable;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.table.TableRowSorter;
import javax.swing.table.TableModel;
import javax.swing.SwingWorker;
import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
import java.awt.Dimension;
import javax.swing.ImageIcon;
import java.io.File;
import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;

import java.nio.*;
import java.nio.file.Files;
import java.io.File;

import javax.swing.JOptionPane;
import java.util.Random;
import java.io.*;

import java.io.InputStream;
import java.io.*;

import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;

public class DisplayQueryResultsIMPALA extends JFrame
{
// database URL, username and password
Random rand = new Random();
public String code = String.format("%s",rand.nextInt()) ;
public static final String CLIENT_PROPERTY_EXCEL_OUTPUT_FORMAT = "EXCEL_OUTPUT_FORMAT_2007" ;
public String command ;
boolean s = false ;
static final String DATABASE_URL = "jdbc:hive2://10.32.68.86:2530/default;principal=hive/perfrm_bld2@HADOOP.COM" ; //"jdbc:hive2://172.29.44.51:28041/default";



// "jdbc:hive2://10.32.68.86:2530/default;principal=perfrm_bld2@HADOOP.COM" // "jdbc:hive2://10.32.68.86:2530/default" ;
// "jdbc:hive2://10.32.68.86:2530/default";

static final String USERNAME = "perfrm_bld2@HADOOP.COM";
static final String PASSWORD = "Stc_12345!";


JButton submitButton = new JButton( "Submit Query" );
JButton exportButton = new JButton( "Export To Excel" );


// default query retrieves all data from Authors table
static final String DEFAULT_QUERY = "SELECT TOP 10 * FROM SYSTABLE";
private ResultSetTableModelIMPALA tableModel;
public JTable resultTable ;
private JTextArea queryArea;

// create ResultSetTableModelIMPALA and GUI
public DisplayQueryResultsIMPALA()
{
super( "Telecom UX Example - IMPALA Server : Author - TARIQ RAHMANI" );
setIconImage(imgicon.getImage());

// create ResultSetTableModelIMPALA and display database table
try
{

// create TableModel for results of query SELECT * FROM Authors
tableModel = new ResultSetTableModelIMPALA( DATABASE_URL, DEFAULT_QUERY );

// set up JTextArea in which user types queries
queryArea = new JTextArea( DEFAULT_QUERY, 20, 100 );
queryArea.setWrapStyleWord( true );
queryArea.setLineWrap( true );

JScrollPane scrollPane = new JScrollPane( queryArea,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );

// set up JButton for submitting queries

// create Box to manage placement of queryArea and
// submitButton in GUI
Box boxNorth = Box.createHorizontalBox();
boxNorth.add( scrollPane );
boxNorth.add( submitButton );
boxNorth.add( exportButton );




// create JTable based on the tableModel
JTable resultTable = new JTable( tableModel );

resultTable.setCellSelectionEnabled(true);
resultTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);



JLabel filterLabel = new JLabel( "Filter:" );
final JTextField filterText = new JTextField();
JButton filterButton = new JButton( "Apply Filter" );
Box boxSouth = Box.createHorizontalBox();

boxSouth.add( filterLabel );
boxSouth.add( filterText );
boxSouth.add( filterButton );

// place GUI components on content pane
add( boxNorth, BorderLayout.NORTH );

add( new JScrollPane( resultTable , ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED , ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED ), BorderLayout.CENTER );

add( boxSouth, BorderLayout.SOUTH );

// create event listener for submitButton
submitButton.addActionListener(

new ActionListener()
{
// pass query to table model
public void actionPerformed( ActionEvent event )
{


SimulatedActivity activity = new SimulatedActivity(100);
activity.execute();



} // end actionPerformed



} // end ActionListener inner class
); // end call to addActionListener






exportButton.addActionListener(

new ActionListener()
{
// pass query to table model
public void actionPerformed( ActionEvent event )
{

SimulatedAct act = new SimulatedAct(100);
act.execute();



} // end actionPerformed



} // end ActionListener inner class
); // end call to addActionListener








final TableRowSorter< TableModel > sorter =
new TableRowSorter< TableModel >( tableModel );
resultTable.setRowSorter( sorter );

setSize( 500, 250 ); // set window size
setExtendedState(this.MAXIMIZED_BOTH);

// create listener for filterButton
filterButton.addActionListener(
new ActionListener()
{
// pass filter text to listener
public void actionPerformed( ActionEvent e )
{
String text = filterText.getText();

if ( text.length() == 0 )

sorter.setRowFilter( null );
else
{
try
{
sorter.setRowFilter(
RowFilter.regexFilter( text ) );

} // end try
catch ( PatternSyntaxException pse )
{
JOptionPane.showMessageDialog( null,
"Bad regex pattern", "Bad regex pattern",
JOptionPane.ERROR_MESSAGE );
} // end catch
} // end else
} // end method actionPerfomed
} // end annonymous inner class
); // end call to addActionLister
} // end try
catch ( SQLException sqlException )
{
JOptionPane.showMessageDialog( null, sqlException.getMessage(),
"Database error", JOptionPane.ERROR_MESSAGE );

// ensure database connection is closed
tableModel.disconnectFromDatabase();

System.exit( 1 ); // terminate application
} // end catch

// dispose of window when user quits application (this overrides
// the default of HIDE_ON_CLOSE)
setDefaultCloseOperation( HIDE_ON_CLOSE ); // it was DISPOSE_ON_CLOSE

// ensure database connection is closed when user quits application
addWindowListener(

new WindowAdapter()
{
// disconnect from database and exit when window has closed
public void windowClosed( WindowEvent event )
{
tableModel.disconnectFromDatabase();
System.exit( 0 );
} // end method windowClosed

} // end WindowAdapter inner class

); // end call to addWindowListener
} // end DisplayQueryResultsIMPALA constructor

// execute application
public String iconPath = ".\\favicon.png" ;
File icon = new File(iconPath);
ImageIcon imgicon = new ImageIcon(iconPath);


class SimulatedActivity extends SwingWorker<Void, Integer> {



public SimulatedActivity(int t) {

current = 0 ;
target = t ;
}


public Void doInBackground() throws Exception{

// perform a new query
try
{
current = 10 ;
publish(current);
command = queryArea.getText() ;
tableModel.setQuery( queryArea.getText() );





} // end try
catch ( SQLException sqlException )
{
JOptionPane.showMessageDialog( null,
sqlException.getMessage(), "Database error",
JOptionPane.ERROR_MESSAGE );

// try to recover from invalid user query
// by executing default query
try
{
tableModel.setQuery( command );
queryArea.setText( command );
} // end try
catch ( SQLException sqlException2 )
{
JOptionPane.showMessageDialog( null,
sqlException2.getMessage(), "Database error",
JOptionPane.ERROR_MESSAGE );

// ensure database connection is closed
// tableModel.disconnectFromDatabase();

// System.exit( 1 ); // terminate application
} // end inner catch
} // end outer catch

return null;
}




protected void done() {



submitButton.setEnabled(true);


// add here read the file code and print it , the file is "localResultFile".concat(code);
// add also the error sResult if any,



}










private int current ;
private int target ;
}





class SimulatedAct extends SwingWorker<Void, Integer> {
public String fileName ;


public SimulatedAct(int t) {

current = 0 ;
target = t ;
}


public Void doInBackground() throws Exception{

try {

new ExportToXLS().writeToExcell(tableModel , String.format("./Export%s.xls",code));


Runtime.getRuntime().exec(String.format("cmd /c start .\\Export%s.xls",code));


}
catch (IOException ee) {}
catch (Exception ex) { }

return null;



}




public void done() {





}










private int current ;
private int target ;
}








public void displayQueryGUIIMPALA()
{


org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
conf.set("hadoop.security.authentication" , "Kerberos");
UserGroupInformation.setConfiguration(conf);
try {
UserGroupInformation.loginUserFromKeytab("perfrm_bld2@HADOOP.COM",".\\user.keytab");
}
catch (IOException ec) {}

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
setPreferredSize( new Dimension(400, 400) ) ;
setVisible( true ); // display window
new DisplayQueryResultsIMPALA();

}
});






} // end main
} // end class DisplayQueryResultsIMPALA