日期:2014-05-16  浏览次数:20486 次

一个jdbc链接hive的小例子

?

先启动Hive远程服务接口

hive --service hiveserver

?

import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import org.apache.hadoop.hive.jdbc.HiveDriver;;
public class HiveTest {

	public static void main(String[] args) throws Exception {  
        
        Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");  
          
        //String dropSQL="drop table javabloger";  
        //String createSQL="create table javabloger (key int, value string)";  
        //String insterSQL="LOAD DATA LOCAL INPATH '/work/hive/examples/files/kv1.txt' OVERWRITE INTO TABLE javabloger";  
        //String querySQL="SELECT a.* FROM javabloger a";  
        String querySQL="SELECT * FROM person";  
          
        Connection con = DriverManager.getConnection("jdbc:hive://127.0.0.1:10000/default", "", "");  
        Statement stmt = con.createStatement();  
        ResultSet rs = null;
        
        
       // rs = stmt.executeQuery("select * from rc_box_office");
        ResultSetMetaData meta2 = stmt.executeQuery("SELECT * FROM person").getMetaData();
        
        System.out.println(meta2.getColumnCount()+"  *0");
        
        System.out.println(meta2.getColumnName(1)+"  *1");
        
        System.out.println(meta2.getColumnType(1)+"  *2");
        System.out.println(meta2.getColumnType(2)+"  *3");
        System.out.println(meta2.getColumnType(3)+"  *4");
        //System.out.println(meta2.getCatalogName(1)+"  *4");
        
        
        //        stmt.executeQuery(dropSQL);  
//        stmt.executeQuery(createSQL);  
//        stmt.executeQuery(insterSQL);  
       /* ResultSet res = stmt.executeQuery(querySQL);  
          
        while (res.next()) {  
            System.out.println("Result: key:"+res.getString(1) +"  –>  value:" +res.getString(2));  
        } */ 
    }  
}  

?那天写个同步oracle和hive表的程序试试