Jdbc连接Hive,jdbchive


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包




相关内容