ubuntu上java连接hive

在ubuntu上的eclipse使用java连接并操作hive

终端操作

  1. 启动hadoop,
    /usr/local/hadoop/sbin$ ./start-all.sh
  2. 启动tomcat(看项目需求来确定是否需要),
    /usr/local/hadoop/share/hadoop/kms/tomcat/bin$ sudo ./startup.sh
  3. 启动hive-metastore,
    /usr/local/hive/bin$ ./hive --service metastore
  4. 启动hiveserver2,
    /usr/local/hive/bin$ ./hive --service hiveserver2

eclipse操作
使用了maven,添加依赖

     <!-- hive模块 -->
    <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-jdbc</artifactId>
		<version>2.1.0.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework.data</groupId>
		<artifactId>spring-data-hadoop</artifactId>
		<version>2.5.0.RELEASE</version>
	</dependency>
	<dependency>
    	<groupId>org.apache.hadoop</groupId>
    	<artifactId>hadoop-common</artifactId><!--该依赖必需-->
    	<version>2.3.0</version>
	</dependency>
	<dependency>
		<groupId>org.apache.hive</groupId>
		<artifactId>hive-jdbc</artifactId><!--该依赖必需-->
		<!--此处版本要与自己到hive版本相同-->
		<version>2.1.0</version>
		<exclusions>
			<exclusion>
				<groupId>org.eclipse.jetty.aggregate</groupId>
				<artifactId>*</artifactId>
			</exclusion>
		</exclusions>
	</dependency>
	<dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-service</artifactId>
        <version>2.1.0</version>
    </dependency>
	<dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-exec</artifactId>
        <version>2.1.0</version>
    </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-metastore -->
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-metastore</artifactId>
        <version>2.3.5</version>
    </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-mapreduce-client-core -->
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-mapreduce-client-core</artifactId>
        <version>2.6.0</version>
    </dependency>

连接操作

public void hiveConnect() { 
	ResultSet rs;
	try {
    	Class.forName("org.apache.hive.jdbc.HiveDriver");     
    	Connection con = DriverManager.getConnection("jdbc:hive2://ip:10000/default","hive","hive");   //可以添加具体数据库,也可以不添加
    	stmt = conn.createStatement();  
    	rs = stmt.executeQuery("show databases");  //有返回结果,用executeQuery,无返回结果,用execute
    	databaseList.clear();
    	while(rs.next()){
        	String databaseName = rs.getString(1);
        	databaseList.add(databaseName);
        }
	}catch(Exception e) {
		System.out.println(e.toString());
	}
    System.out.println(databaseList);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值