Expandmenu Shrunk


  • HiveSQL JDBC示例

     // run as Java Applicaition
    
    package hive;
    import java.sql.SQLException;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.sql.DriverManager;
    
    public class hive_test {
    	public static void main(String[] args) throws SQLException{
    		//注册JDBC驱动
    		try{
    			Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");
    		}catch(ClassNotFoundException e){
    			e.printStackTrace();
    			System.exit(1);
    		}
    		Connection con=DriverManager.getConnection("jdbc:hive://192.168.0.201:8888/default","","");
    		Statement stmt=con.createStatement();
    		String tableName="user_loc";
    		stmt.executeQuery("drop table "+ tableName);
    		ResultSet res= stmt.executeQuery("create table " + tableName +"(userid int, username string)" + " row format delimited fields terminated by "+" '\t'");
    	/*
    	 *
    
    	 */
    	
    		//show table语句
    		String sql="show tables";
    		System.out.println("Running " +sql +":");
    		res=stmt.executeQuery(sql);
    		while(res.next()){
    			System.out.println(res.getString(1));	//显示第一列
    	    }
    	    //Describe table语句
    		sql="describe  " + tableName;
    		System.out.println("Running"+ sql+":");
    		res=stmt.executeQuery(sql);
    		while(res.next()){
    			System.out.println(res.getString(1)+ "\t" + res.getString(2));
    		}	
    		//Load语句
    		String filepath="/home/hadoop/loc.txt";
    		sql="load data local inpath  '" + filepath + "'  overwrite  into table  " + tableName;
    		System.out.println("Running:" + sql+ ":");
    		stmt.executeQuery(sql); 
    		//Select 语句
    		sql="select * from " +tableName + "   limit 3";
    	    System.out.println("Running :" +sql +":");
    	    res=stmt.executeQuery(sql);
    	    while(res.next()){
    	    	System.out.println(res.getString(1) + "\t" + res.getString(2));
    	    }
    	    //统计记录数量
    		
    	    sql="select count(*) from " +tableName ;
    	    System.out.println("Running :" +sql +":");
    	    res=stmt.executeQuery(sql);
    	    while(res.next()){
    	    	System.out.println(res.getString(1));
    	    }
    	}
    }
    
    
    hive> show tables;
    OK
    pokes
    user_config
    user_info
    user_loc
    user_xueke
    vxueke
    Time taken: 0.057 seconds, Fetched: 6 row(s)
    hive> desc user_loc;
    OK
    userid                  int                                         
    username                string                                      
    Time taken: 0.947 seconds, Fetched: 2 row(s)
    
    



香港马会开奖记录|香港马会开奖资料|香港马会开奖现场|香港马会走势图|香港马会开奖结果直播|香港马会n730|