码迷,mamicode.com
首页 > 数据库 > 详细

Jdbc连接Hive

时间:2015-04-27 10:07:08      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:jdbc连接hive   jdbc   hive   

1.启动hive 服务

./hive --service hiveserver & 

2.创建hive程序

package hive;

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

public class TestHive02 {
	private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";

	/**
	 * @param args
	 * @throws SQLException
	 */
	public static void main(String[] args) throws SQLException {
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(1);
		}
		Connection con = DriverManager.getConnection(
				"jdbc:hive://192.168.213.5:10000/defalt", "", "");
		Statement stmt = con.createStatement();
		// del
		stmt.executeQuery("drop table test");
		// create
		stmt.executeQuery("create table if not exists test(amount DOUBLE, st_name string) "
				+ "ROW FORMAT DELIMITED "
				+ "FIELDS TERMINATED BY ' ' "
				+ "STORED AS TEXTFILE");
		// load
		stmt.executeQuery("load data local inpath '/home/test.txt' into table test");
		// start time
		long st = System.currentTimeMillis();
		// run
		ResultSet res = stmt
				.executeQuery("select st_name,sum(amount) c from test group by st_name  sort by c");
		// count
		int i = 0;
		// for
		while (res.next()) {
			i++;
			// data out print
		}
		// end time
		long en = System.currentTimeMillis();
		// start - end = runtime
		System.out.println("总耗时:" + (en - st) + ",记录总数:" + i);
	}
}


3.此连接需要导入的jar包

技术分享


Jdbc连接Hive

标签:jdbc连接hive   jdbc   hive   

原文地址:http://blog.csdn.net/pengweid/article/details/45289679

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!