2016-10-18 35 views
-1
import java.util.List;  
import org.apache.hadoop.hive.service.HiveServerException;  
import org.apache.hadoop.hive.service.ThriftHive;  
import org.apache.hadoop.hive.service.ThriftHive.Client;  
import org.apache.thrift.TException;  
import org.apache.thrift.protocol.TBinaryProtocol;  
import org.apache.thrift.transport.TSocket;  
public class Hive_demo_02 { 
/** 
* @param args 
*/ 
static TSocket transport; 

static Client client=null; 

private static Client getClient(String hiveServer, Integer hivePort)  
{ 

    final int SOME_BIG_NUMBER = 999999993;  
    try { 
     transport = new TSocket(hiveServer, hivePort);  
    transport.setTimeout(SOME_BIG_NUMBER);  
    transport.open();  
    TBinaryProtocol protocol = new TBinaryProtocol(transport);  
    client = new ThriftHive.Client(protocol);  
    System.out.println("Connection is established");  
    return client;  
    }  
    catch (Exception e) {  
    e.printStackTrace();  
    return null;  
    }  
    } 

} 
private void show_databases(Client c1) 

{ 

    if(c1!=null) 

    { 

try { 

try { 

    c1.execute("show databases;"); 

} catch (TException e) { 

// TODO Auto-generated catch block 

e.printStackTrace(); 

} 

List<String> li = null; 

System.out.println("hai"); 

try { 

li = c1.fetchAll(); 

System.out.println(li); 

System.out.println("hai"); 

} catch (TException e) { 

// TODO Auto-generated catch block 

e.printStackTrace(); 

} 

System.out.println(" *** The Databases List *** "); 

for (String string : li) { 

System.out.println(string); 

} 

System.out.println(" -------------------------------- "); 

Client c2=c1; 

} 
finally{ 

} 

    } 

} 

private void create_databases(Client c1) 

{ 

try { 

c1.execute("create database IF NOT EXISTS jdbc_demo"); 

} catch (HiveServerException e) { 

// TODO Auto-generated catch block 

e.printStackTrace(); 

} catch (TException e) { 

// TODO Auto-generated catch block 

e.printStackTrace(); 

} 

System.out.println(" *** jdbc_demo Databases is created *** "); 

System.out.println(" -------------------------------- "); 

Client c2=c1; 

} 

private void use_databases(Client c1,String db_name) 

{ 

try { 

c1.execute("use "+db_name); 

} catch (HiveServerException e) { 

// TODO Auto-generated catch block 

e.printStackTrace(); 

} catch (TException e) { 

// TODO Auto-generated catch block 

e.printStackTrace(); 

} 

System.out.println(" *** Changed Database to "+db_name+" *** "); 

System.out.println(" -------------------------------- "); 

Client c2=c1; 

//return c2; 

} 

public static void main(String[] args) { 

// TODO Auto-generated method stub 

String HIVE_SERVER = "localhost"; 

Integer HIVE_PORT = new Integer(54310); 

Client client = getClient(HIVE_SERVER, HIVE_PORT); 

Hive_demo_02 obj=new Hive_demo_02(); 

obj.show_databases(client); 

System.out.println(" Before Creating the Database jdbc_demo "); 

obj.create_databases(client); 

System.out.println(" After Creating the Database jdbc_demo "); 

obj.show_databases(client); 

System.out.println(" Changing to jdbc_demo Database "); 

obj.use_databases(client,"jdbc_demo"); 

transport.close(); 

} 

} 

我在此程序中遇到了破碎的管道套接字異常。任何人都可以幫助我嗎?在以下程序中出現破損管道錯誤

+0

這是很難遵循什麼程序做的,因爲沒有壓痕蜂巢服務器。另外,請爲潛在的回答者提供更多幫助。該計劃應該做什麼?它如何不能做到這一點?你嘗試過什麼,結果是什麼? –

+0

它試圖連接到Hive服務器,並嘗試在Hive中創建,顯示和更改數據庫。 –

回答

0

我找到了解決方案。設置端口號爲 出口HIVE_PORT = 10000 然後開始使用 ./hive --service hiveserver

相關問題