hive2 java连接_用Java代码通过JDBC连接Hiveserver2

本文详细介绍了如何使用Java通过JDBC连接到Hiveserver2,包括启动hiveserver2,使用beeline连接hive,配置maven依赖,并提供了详细的Java API测试代码,涉及创建数据库、查询、删除、加载数据等操作。
摘要由CSDN通过智能技术生成

1.在终端启动hiveserver2

#hiveserver2

2.使用beeline连接hive

另外打开一个终端,输入如下命令(xavierdb必须是已经存在的数据库)

#beeline -u jdbc:hive2://localhost:10000/xavierdb -n hive -p hive

3.添加maven依赖

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

org.apache.hive

hive-jdbc

1.1.0

junit

junit

4.9

org.apache.hadoop

hadoop-common

2.6.0

org.apache.hadoop

hadoop-client

2.6.0

org.apache.hive

hive-metastore

1.1.0

org.apache.hive

hive-exec

1.1.0

maven依赖

出现过的错误: Error: Could not open client transport with JDBC Uri: jdbc:hive2://localhost:10000/default

解决办法:检查发现运行beeline时Driver版本Driver: Hive JDBC (version 1.1.0-cdh5.16.1)比maven依赖中的Driver版本低,将maven版本调至1.1.0问题解决

Java API测试:

注意:这里的url必须是beeline值中使用的url

package TestOption;

import org.junit.Test;

import org.junit.After;

import org.junit.Before;

import java.sql.*;

/**

* @Author:Xavier

* @Data:2019-02-18 11:43

**/

public class HiveOption {

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

private static String url = "jdbc:hive2://172.19.224.213:10000/xavierdb";

private static Connection con = null;

private static Statement state = null;

private static ResultSet res = null;

//加载驱动,创建连接

@Before

public void init() throws ClassNotFoundException, SQLException {

Class.forName(driverName);

con = DriverManager.getConnection(url, "hive", "hive");

state = con.createStatement();

}

//创建数据库

@Test

public void CreateDb() throws SQLException {

state.execute("create database xavierdb1");

}

// 查询所有数据库

@Test

public void selectDb() throws SQLException {

res = state.executeQuery("show databases");

while (res.next()) {

System.out.println(res.getString(1));

}

}

// 删除数据库

@Test

public void dropDb() throws SQLException {

state.execute("drop database if exists xavierdb1");

}

// 创建表

@Test

public void createTab() throws SQLException {

state.execute("create table if not exists student ( " +

"name string , " +

"age int , " +

"agent string ," +

"adress struct) " +

"row format delimited " +

"fields terminated by ',' " +//字段与字段之间的分隔符

"collection items terminated by ':'"+//一个字段各个item的分隔符

"lines terminated by '\n' ");//行分隔符

}

// 查询所有表

@Test

public void selectTab() throws SQLException {

res=state.executeQuery("show tables");

while(res.next()){

System.out.println(res.getString(1));

}

}

// 查看表结构

@Test

public void descTab() throws SQLException {

res=state.executeQuery("desc student");

while(res.next()){

System.out.println(res.getString(1)+"\t"+res.getString(2));

}

}

// 加载数据(本地加载)

@Test

public void loadData() throws SQLException {

String infile=" '/root/studentData' ";

state.execute("load data local inpath "+infile+"overwrite into table student");

}

// 查询数据

@Test

public void selectTab() throws SQLException {

res=state.executeQuery("select * from student");

while(res.next()){

System.out.println(

res.getString(1)+"-"+

res.getString(2)+"-"+

res.getString(3)+"-"+

res.getString(4));

}

}

// 统计查询(会运行mapreduce作业,资源开销较大)

@Test

public void countData() throws SQLException {

res=state.executeQuery("select count(1) from student");

while(res.next()){

System.out.println(res.getInt(1));

}

}

// 删除表

@Test

public void dropTab() throws SQLException {

state.execute("drop table student1");

}

@After

public void destory() throws SQLException {

if (res != null) state.close();

if (state != null) state.close();

if (con != null) con.close();

}

}

标签:JDBC,Java,res,void,hive,throws,Hiveserver2,state,public

来源: https://www.cnblogs.com/xavier-xd/p/10399581.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值