问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

hive建表时partitioned分区字段类型只能用string吗?可以用其他的字段类型吗?

发布网友 发布时间:2022-04-07 16:47

我来回答

3个回答

懂视网 时间:2022-04-07 21:08

然后我们和操作普通数据库一样,先加载驱动,然后建立连接,接着创建一个statement,然后执行查询,然会结果集。代码如下(一定要写对sql语句,要仔细,下面的需要注意的地方我已经标示出来了:)

package playHive;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class HiveJdbcClient {

	private final static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
	private final static String localFilePath="/home/hadoop/test/hive/test.txt";
	private final static String hdfsFilePath="hdfs://192.168.0.1:9000/user/hadoop/";
	private final static String tableName="testHiveDriverTable";
	private final static String partitionName="testHiveDriverPartition";
	private final static String bucketName="testHiveDriverBucket";
	private static String sql = "";
	private static Connection connection ;
	private static Statement statement;
	private static ResultSet resultSet;

	static {
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			System.out.println(e);
			System.exit(1);
		}
		try {
			 connection = DriverManager.getConnection(
					"jdbc:hive://192.168.0.1:50000/default", "hive", "hadoop"); //首先要打开hiveserver服务:hive --service hiveserver
			statement = connection.createStatement();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void createTable() throws SQLException {
		sql = "drop table " + tableName;
		System.out.println("delete table****");
		statement.executeQuery(sql);
		sql = "create table " + tableName + " (key int,value String)"
				+ " row format delimited fields terminated by '	'";
		System.out.println("create table:"+tableName);
		statement.executeQuery(sql);
		showTable();
		describeTable();
	}

	public static void showTable() throws SQLException {
		sql = "show tables " + tableName;
		System.out.println("show table:"+tableName);
		resultSet=statement.executeQuery(sql);
		while(resultSet.next()){
			System.out.println(resultSet.getString(1));
		}
	}
	
	public static void describeTable() throws SQLException{
		sql="describe "+tableName;
		System.out.println("describe table:"+tableName);
		resultSet=statement.executeQuery(sql);
		while(resultSet.next()){
			System.out.println(resultSet.getString(1)+"	"+resultSet.getString(2));
		}
	}
	
	public static void loadDataToTable(boolean isLocal) throws SQLException{
		sql=isLocal?"load data local inpath '"+localFilePath+"' overwrite into table "+tableName:
			 "load data inpath '"+hdfsFilePath+"' overwrite into table "+tableName;
		System.out.println("load data into table:"+tableName);
		statement.executeQuery(sql);
	}
	
	public static void queryTable() throws SQLException{
		sql="select * from "+tableName;
		System.out.println("execute query:select *query");
		resultSet=statement.executeQuery(sql);
		while(resultSet.next()){
			System.out.println(resultSet.getString(1)+'	'+resultSet.getString(2));
		}
	}
	
	public static void regularTableQuery() throws SQLException{
		//sql="select count(1) from "+tableName+";";
		sql="select key,max(n) from (select key,count(value) as n from "+tableName+" group by key)sbuq group by key";
		System.out.println("execute query:");
		resultSet =statement.executeQuery(sql);
		while(resultSet.next()){
			System.out.println(resultSet.getString(1));
		}
	}
	
	public static void createPartition() throws SQLException{
		sql="drop table "+partitionName;
		System.out.println("delete partition");
		statement.execute(sql);
		sql="create table "+partitionName+"(key int) partitioned by (value string) "
				+ "row format delimited fields terminated by '	'";
		System.out.println("create partition:"+partitionName);
		statement.execute(sql);
	}
	
	public static void insertDataToPartition() throws SQLException{
		//这里一定是select key from "+tableName;key不可以写成value,否则不能插入进去,插入的值将会是null
		sql="insert overwrite table "+partitionName+" partition (value='qinqin') select key from "+tableName;
		statement.execute(sql);
		System.out.println("insert data to "+partitionName+" success");
	}
	
	public static void selectFromPartition() throws SQLException{
		sql="select * from "+partitionName+" where value='qinqin'";
		System.out.println("query in partition:select * in "+partitionName);
		resultSet=statement.executeQuery(sql);
		while(resultSet.next()){
			System.out.println(resultSet.getString(1));
		}
	}
	
	public static void createBucket() throws SQLException{
		sql="drop table "+bucketName;
		System.out.println("delete bucket");
		statement.executeQuery(sql);
		sql="create table "+bucketName+"(key int,value string) clustered by(key) into 3 buckets "
				+ "row format delimited fields terminated by '	'";
		System.out.println("create bucket:"+bucketName);
		statement.execute(sql);
	}
	
	public static void insertDataToBucket() throws SQLException{
		sql="insert overwrite table "+bucketName+" select key,value from "+tableName;
		System.out.println("insert data into bucket:"+bucketName);
		statement.executeQuery(sql);
	}
	
	public static void selectFromBucket() throws SQLException{
		sql="select * from "+bucketName+" tablesample(bucket 1 out of 3 on key)";
		System.out.println("select from bucket:"+bucketName);
		resultSet=statement.executeQuery(sql);
		while(resultSet.next()){
			System.out.println(resultSet.getString(1)+"	" +resultSet.getString(2));
		}
	}
	
	public static void closeConnection(){
		try {
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
	
	public static void tableOperation() throws SQLException{
		HiveJdbcClient.createTable();
		HiveJdbcClient.loadDataToTable(true);
		HiveJdbcClient.queryTable();
		HiveJdbcClient.regularTableQuery();
	}
	
	public static void partitionOperation() throws SQLException{
		HiveJdbcClient.createPartition();
		HiveJdbcClient.insertDataToPartition();
		HiveJdbcClient.selectFromPartition();
	}
	
	public static void bucketOperation() throws SQLException{
		HiveJdbcClient.createBucket();
	 HiveJdbcClient.insertDataToBucket();
	 HiveJdbcClient.selectFromBucket();
	}
	
	public static void main(String[] args) {
		try {
			System.out.println("table Operation***********************************************");
			//HiveJdbcClient.tableOperation();
			System.out.println("partition Operation***********************************************");
			HiveJdbcClient.partitionOperation();
			System.out.println("bucket Operation***********************************************");
			//HiveJdbcClient.bucketOperation();
			HiveJdbcClient.closeConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}




hive通过jdbc创建表,分区,桶

标签:hadoop   hive   jdbc   数据库   sql   

热心网友 时间:2022-04-07 18:16

可以呀,根据自己的需求指定就行了!但是一般情况用string的!
比如:
use dw;
drop table if exists xxxxx;
CREATE TABLE xxxxx (
time string,
user_id int,
keyword string
)
comment "记录表"
partitioned by (ds string, hour string, type int)
stored as textfile;追问我用partitioned by (DT timestemp),但是DT字段值是2016-12-23,然后运行存储过程王表中插数时,DT字段值全为空,该咋办?

热心网友 时间:2022-04-07 19:34

我分区的类型就是int 按照year month day 分的,可行
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
说课包括哪些方面 说课内容包括()。 如何在手机百度上删除对话记录? 结核病是什么样的疾病? 曹丕17岁得了肺痨,明知自己命不长久,还要强争王位,是不是很自私呢?_百... 古代小说常出现的病名 急求一篇"生活小窍门"(500字)的作文 至今最有什么小妙招 健康的戒烟方法 笔记本电池锁死是什么原因引起的? 中国工商银行股份有限公司景洪北路支行怎么样? 第27届香港金像奖结果 第29届香港电影金像奖的提名名单 儿子结婚,妈妈的祝福语 求第27届香港电影金像奖提名名单 在儿子婚礼上父母要讲什么话 儿子结婚母亲简短的祝福词语 明日战记为何没上映 儿子结婚妈妈的祝福语 屁股老是长包怎么办? 祝孩子新婚快乐的祝福语简短 肉丝面肉丝怎么做好吃 我男朋友屁股上长个包.怎么办呢? 儿子即将结婚妈妈的祝福语 男生,屁股上长了火包,破了,流了雪,该怎么办? 屁股上长了个又大又硬的包是怎么办? 祝福儿子领结婚证的祝福语 屁股上长了很多包,怎么办能消掉呢?求解 妈妈对儿子结婚的祝福语 今天是儿子的结婚一周年我想给他们祝福语怎么说好呢! 你知道明星最尴尬的事有哪些? 08年台湾金马奖得主有谁? 求哪位大神有寻秦记(2021)古天乐/林峯主演的免费高清的百度云资源链接地址感谢 寻秦记电影版上映时间_寻秦记高清网盘完整在线观看 西点一般分为哪三大类 甜点分类有多少种? 30届香港电影金像奖最佳男主角分别是哪些人 今天必胜客怎么登陆不了网上订餐呢 2009金像奖提名名单 在必胜客app点餐,支付宝已经扣钱,但是说订餐不成功,怎么办 明日战记电影上映具体时间是什么时候 电影明日战记 西点分为那几类? 怎么在必胜客点餐? 生日有一份免费的披萨必胜客这个为什么点不起呢 必胜客网上订餐说我的地址不在送的范围内是怎么回事? 必胜客可以手机自助点餐嘛 必胜客不能送餐了 微信回复嗯哈代表什么含义 手机必胜客App点餐,只能货到付款,不能网上支付的吗? 嗯哈是什么意思?用来回复别人会不会有敷衍的感觉?