hive jdbc使用

  • 2018-02-09
  • 浏览 (1290)

hive数据仓库建构在hadoop集群之上,数据存在hdfs文件系统中,hive中执行的操作会装换成mapreduce作业进行执行,hive支持类似SQL的语言HQL,hive采用元数据对表进行管理,元数据有三种存放模式:嵌入模式,远程模式,本地模式;hive提供了强大的编程接口,hive jdbc可以让你如使用普通的jdbc一般来操作hive表以及数据。


<dependency>
		    <groupId>org.apache.hive</groupId>
		    <artifactId>hive-jdbc</artifactId>
		    <exclusions>  
		        <exclusion>
	                <groupId>org.eclipse.jetty.aggregate</groupId>
	                <artifactId>*</artifactId>
	            </exclusion>
		        <exclusion>  
		            <groupId>org.apache.hive</groupId>  
		            <artifactId>hive-shims</artifactId>  
		        </exclusion>  
		    </exclusions>  
		    <version>1.1.0</version>
		</dependency>
		<dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <exclusions>
           	  	<exclusion>
		            <groupId>tomcat</groupId>
		            <artifactId>*</artifactId>
	            </exclusion>
	            <exclusion>
	              	<groupId>javax.servlet</groupId>
	              	<artifactId>servlet-api</artifactId>
	            </exclusion>
	            <exclusion>
	              	<groupId>javax.servlet.jsp</groupId>
	              	<artifactId>*</artifactId>
	            </exclusion>
            </exclusions>
            <version>2.6.4</version>
        </dependency>
		<dependency>  
		    <groupId>jdk.tools</groupId>  
		    <artifactId>jdk.tools</artifactId>  
		    <version>1.8</version>  
		    <scope>system</scope>  
		    <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>  
		</dependency>



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

/**
 * hive使用样例
**/
public class HiveJdbcClient {

	private static String driverName = "org.apache.hive.jdbc.HiveDriver";

	public static void main(String[] args) throws SQLException {

		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			System.exit(1);
		}

		Connection con = DriverManager.getConnection("jdbc:hive2://192.168.2.110:10000/coin", "root", "");
		Statement stmt = con.createStatement();
		ResultSet res = stmt.executeQuery("select * from test");

		int count = 0;
		while (res.next()) {
			System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
			count++;
			if(count > 100){
				return;
			}
		}
		
		res.close();
		stmt.close();
		con.close();

	}

}


0  赞