hive创建外表操作,在操作之前,需要先导入相关包,下载地址见另一篇博文,下载相关包之后,导入到工程里去,
即可。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class HiveUtil {
static Connection conn=null;
//建外表操作
public void CreateExternalTable(String tablename) throws SQLException{
//hiveserver 是org.apache.hadoop.hive.jdbc.HiveDriver,0.11之后改为如下形式
try {
Class.forName("org.apache.hive.jdbc.HiveDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//0.11之前是(jdbc:hive://202.193.59.62:10000/default","","")
conn = DriverManager.getConnection("jdbc:hive2://202.193.59.62:10000/default","","");
/* */
System.out.println("连接成功!");
Statement stmt = conn.createStatement();
stmt.execute("set mapred.reduce.tasks=4") ;
String sql="create external table mobile_info(key string,city string,dealtime string,imsi string,manufacturer string,meid string,model string,province string, regtime string,version string)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
with serdeproperties ("hbase.columns.mapping" = ":key,fr:CITY,fr:DEALTIME,fr:IMSI,fr:MANUFACTURER,fr:MEID,fr:MODEL,fr:PROVINCE,fr:REGTIME,fr:VERSION") tblproperties("hbase.table.name" = "mobile_info");"
}
}