• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
导航菜单
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • windows
  • 服务器硬件
  • 服务器运维
  • 云计算
  • 虚拟化
  • IIS教程
  • Linux
  • Apache
  • Ftp
  • DNS
  • Nginx
您的位置:首页 > 服务器 >云计算 > hive通过jdbc创建表,分区,桶,hivejdbc

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

作者:网友 字体:[增加 减小] 来源:互联网

本文主要包含hive jdbc,hive for jdbc driver,hiveserver1 jdbc,jdbc连接hive,java hive jdbc等服务器相关知识,网友希望可以进行参考

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


首先我们需要打开hiveserver服务:hive --service hiveserver

然后我们和操作普通数据库一样,先加载驱动,然后建立连接,接着创建一个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 '\t'";
		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)+"\t"+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)+'\t'+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 '\t'";
		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 '\t'";
		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)+"\t" +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();
		}
	}
}




分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

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

相关文章

  • flume 组件概述与列表
  • Spark Core and Cluster Managers(翻译自Learning.Spark.Lightning-Fast.Big.Data.Analysis),
  • 【Spark】弹性分布式数据集RDD概述,sparkrdd概述
  • Spark如何读写hive
  • java保留两位小数,java两位小数
  • spark core源码分析14 参数配置,sparkcore
  • AMQP server on c ontroller:5672 is unreachable: [Errno 113] EHOSTUNREACH. Trying again in 2 seconds.,ehostunreach
  • 机器学习数学基础- gradient descent算法(下),gradientdescent
  • hive使用技巧(一)自动化动态分配表分区及修改hive表字段名称,使用技巧hive
  • Docker(1):Virtualbox Install Centos7 & Docker,dockervirtualbox

文章分类

  • windows
  • 服务器硬件
  • 服务器运维
  • 云计算
  • 虚拟化
  • IIS教程
  • Linux
  • Apache
  • Ftp
  • DNS
  • Nginx

最近更新的内容

    • 深入理解Scala 标识符,命名和域,深入理解scala
    • 【解决】hive动态增加partitions不能超过100的问题,hivepartitions
    • Cloud Foundry安装部署指南(下),cloudfoundry
    • request.getScheme()的使用方法,request.getscheme
    • hive优化-----控制hive任务的reduce数,hivereduce
    • 在 Mac OS X 系统里使用 Docker,osdocker
    • 基于 ssh + Xpra 构建 Docker 桌面系统,xpradocker
    • 详说大数据计算的可类化Classable,类化classable
    • HDFS小文件的合并优化
    • hadoop-2.6.0安装(new),hadoop-2.6.0new

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有