Hive学习笔记:Hive JDBC+Java API

本文介绍了在CentOS7环境下,使用Hive-1.1.0-cdh5.14.0和hadoop-2.6.0-cdh5.14.0,如何配置Hive JDBC以及通过Java API进行操作。主要内容包括Hive JDBC的配置,如修改hive-site.xml设置HiveServer2的端口和主机,以及启动metastore和hiveserver2服务。同时,文章提到了如何创建Maven项目并配置相关依赖,以便使用Java API连接和操作Hive,并推荐了一篇关于PreparedStatement的详细文章。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境:CentOS7
           hive-1.1.0-cdh5.14.0
           hadoop-2.6.0-cdh5.14.0

Hive JDBC配置与实现

修改$HIVE_HOIME/conf下的hive-site.xml,添加以下内容

<property>
        <name>hive.server2.thrift.port</name>
        <value>10000</value>
</property>
<property>
        <name>hive.server2.thrift.bind.host</name>
        <value>0.0.0.0</value>
</property>

设置Hiveserver2 Thrift的port和host(host设置成0.0.0.0,来接收未知来源的ip)

nohup启动metastore和hiveserver2(当然是要先起hdfs和yarn)
nohup hive --service metastore > metastore.log 2>&1 &
nohup hive --service hiveserver2 > hiveserver2.log 2>&1 &
(可以写在一个shell文件里,然后在测试环境要起hive的时候就可以偷懒了233)
jps看一下有两个runjar那就说明没有问题啦,或者看一下生成的日志信息保证没有报错

然后使用beeline连接hive

[root@centos bin]# beeline
Beeline version 1.1.0-cdh5.14.0 by Apache Hive
beeline> !connect jdbc:hive2://centos:10000
scan complete in 1ms
Connecting to jdbc:hive2://centos:10000
Enter username for jdbc:hive2://centos:10000: root
Enter password for jdbc:hive2://centos:10000: ******
Connected to: Apache Hive (version 1.1.0-cdh5.14.0)
Driver: Hive JDBC (version 1.1.0-cdh5.14.0)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://centos:10000> 

Java API 操作 

新建一个Maven Project,这是用到的pom.xml,只需要将<build>到</project>间的复制到自己的project,update一下maven project就可以了

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>06HiveJDBCJava</groupId>
  <artifactId>06HiveJDBCJava</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <build> 
    <pluginManagement>
    	<plugins>
    		<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
    		<plugin>
    			<groupId>org.eclipse.m2e</groupId>
    			<artifactId>lifecycle-mapping</artifactId>
    			<version>1.0.0</version>
    			<configuration>
    				<lifecycleMappingMetadata>
    					<pluginExecutions>
    						<pluginExecution>
    							<pluginExecutionFilter>
    								<groupId>
    									org.apache.maven.plugins
    								</groupId>
    								<artifactId>
    									maven-compiler-plugin
    								</artifactId>
    								<versionRange>[3.1,)</versionRange>
    								<goals>
    									<goal>compile</goal>
    								</goals>
    							</pluginExecutionFilter>
    							<action>
    								<ignore></ignore>
    							</action>
    						</pluginExecution>
    					</pluginExecutions>
    				</lifecycleMappingMetadata>
    			</configuration>
    		</plugin>
    	</plugins>
    </pluginManagement>
  </build>
  
  <repositories>
    <repository>
      <id>cloudera</id>
      <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
    </repository>
    <repository>
      <id>maven</id>
      <url>http://central.maven.org/maven2/</url>
    </repository>
    <repository>
    <id>alimaven</id>  
  			<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    </repository>  
     </repositories>

<dependencies>
		<dependency>  
		    <groupId>org.apache.maven.plugins</groupId>  
		    <artifactId>maven-resources-plugin</artifactId>  
		    <version>2.5</version>  
		</dependency>  
	  <dependency>
		<groupId>org.apache.hadoop</groupId>
		<artifactId>hadoop-common</artifactId>
		<version>2.6.0-cdh5.14.0</version>
		</dependency>
		<dependency>
		<groupId>org.apache.hadoop</groupId>
		<artifactId>hadoop-hdfs</artifactId>
		<version>2.6.0-cdh5.14.0</version>
		</dependency>
		<dependency>
		<groupId>org.apache.hadoop</groupId>
		<artifactId>hadoop-maven-plugins</artifactId>
		<version>2.6.0-cdh5.14.0</version>
		</dependency>
		 <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.6.0-cdh5.14.0</version>
        </dependency>
		 <dependency>
		    <groupId>org.apache.hadoop</groupId>
		    <artifactId>hadoop-yarn-server-resourcemanager</artifactId>
		    <version>2.6.0-cdh5.14.0</version>
		 </dependency>
		
		 <dependency>
		    <groupId>org.apache.hadoop</groupId>
		    <artifactId>hadoop-yarn-server-nodemanager</artifactId>
		    <version>2.6.0-cdh5.14.0</version>
		 </dependency>
		        
		 <dependency>
		    		<groupId>org.apache.hadoop</groupId>
		    		<artifactId>hadoop-yarn-common</artifactId>
		   		<version>2.6.0-cdh5.14.0</version>
		 </dependency>
		 <dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-mapreduce-client-app</artifactId>
    <version>2.6.0-cdh5.14.0</version>
		</dependency>
		 <dependency>
		    <groupId>org.apache.hadoop</groupId>
		    <artifactId>hadoop-mapreduce-client-common</artifactId>
		    <version>2.6.0-cdh5.14.0</version>
		</dependency>
		<dependency>
		    <groupId>org.apache.hadoop</groupId>
		    <artifactId>hadoop-mapreduce-client-core</artifactId>
		    <version>2.6.0-cdh5.14.0</version>
		</dependency>
		 <dependency>
		    <groupId>org.apache.hadoop</groupId>
		    <artifactId>hadoop-mapreduce-client-jobclient</artifactId>
		    <version>2.6.0-cdh5.14.0</version>
		</dependency>
		<dependency>
		     <groupId>org.apache.hadoop</groupId>
		     <artifactId>hadoop-yarn-api</artifactId>
		    <version>2.6.0-cdh5.14.0</version>
		 </dependency>
		<dependency>
		    <groupId>org.apache.hadoop</groupId>
		    <artifactId>hadoop-yarn-client</artifactId>
		    <version>2.6.0-cdh5.14.0</version>
		</dependency>
	<dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-exec</artifactId>
        <version>1.1.0-cdh5.14.0</version>
     </dependency>
      <dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-common</artifactId>
    <version>1.1.0-cdh5.14.0</version>
</dependency>
 <dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-jdbc</artifactId>
    <version>1.1.0-cdh5.14.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-metastore -->
<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-metastore</artifactId>
    <version>1.1.0-cdh5.14.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-exec</artifactId>
    <version>1.1.0-cdh5.14.0</version>
</dependency>
	  <dependency>
			<groupId>org.apache.mrunit</groupId>
			<artifactId>mrunit</artifactId>
			<version>1.1.0</version>
			<classifier>hadoop2</classifier>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.mockito</groupId>
			<artifactId>mockito-all</artifactId>
			<version>1.9.5</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.10</version>
			<scope>test</scope>
		</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>  
 </dependencies>
</project>

具体实现

package com.jdbc.test;

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

public class HiveJDBCTest 
{
	//驱动,URL,用户名,密码,数据库
	public static final String HIVE_DRIVER="org.apache.hive.jdbc.HiveDriver";
	public static final String HIVE_URL="jdbc:hive2://centos:10000";
	public static final String USER_NAME="root";
	public static final String PASSWORD="123456";
	
	public static void main(String[] args) throws SQLException 
	{
		Connection conn=null;
		PreparedStatement pstmt=null,pstmt1=null;
		ResultSet rs=null;
		
		try {
			//加载驱动,创建连接
			Class.forName(HIVE_DRIVER);
			conn = DriverManager.getConnection(HIVE_URL, USER_NAME, PASSWORD);
			
			//使用PreparedStatement,这里注意,查询的时候使用的是executeQuery,而且会有结果集返回其他的加载数据,创表创库等操作用的是execute方法
			pstmt1=conn.prepareStatement("load data local inpath"+"/student.txt"+"overwrite into table student");
			pstmt1.execute();
			
			pstmt = conn.prepareStatement("select * from student");
			rs=pstmt.executeQuery();
			
			//遍历输出每一行查询结果,注意get方法对应的字段的类型
			while(rs.next()!=false)
			{
				System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getInt(4));
			}
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		//释放资源
		rs.close();
		pstmt.close();
		pstmt1.close();
		conn.close();
		
	}
}

这里值得一提的是,这里使用了PreparedStatement,关于这个有一篇文章写的很详细,这里转个链接
http://www.cnblogs.com/zhizhuwang/p/3513372.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值