首页 > 数据库技术 > 详细

Jdbc连接Hive

时间:2015-04-27 10:03:57      阅读:146      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!