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();
}
}
我在这个程序中遇到Broken pipe socket异常。任何人都可以帮助我吗?
答案 0 :(得分:0)
我找到了解决方案。将端口号设置为 export HIVE_PORT = 10000 然后使用启动配置单元服务器 ./hive --service hiveserver