Hive-JDBC操作

二:配置hive-site.xml

Java是通过beeline来连接Hive的。启动beeline最重要的就是配置好hive-site.xml。

其中javax.jdo.option.ConnectionURL涉及到一个数据库,最好重新删掉原来的metastore数据库然后重新创建一个并初始化一下。

mysql> create database metastore;

cd /usr/local/Cellar/hive/3.1.2/libexec/bin
schematool -initSchema -dbType mysql

hive-site.xml

hive.metastore.local true hive.metastore.uris thrift://localhost:9083 Thrift URI for the remote metastore. Used by metastore client to connect to remote metastore. javax.jdo.option.ConnectionURL jdbc:mysql://localhost:3306/metastore?characterEncoding=UTF-8&createDatabaseIfNotExist=true javax.jdo.option.ConnectionDriverName com.mysql.cj.jdbc.Driver javax.jdo.option.ConnectionUserName root javax.jdo.option.ConnectionPassword root123 hive.exec.local.scratchdir /tmp/hive hive.downloaded.resources.dir /tmp/hive hive.metastore.warehouse.dir /data/hive/warehouse hive.metastore.event.db.notification.api.auth false hive.server2.active.passive.ha.enable true hive.server2.transport.mode binary Expects one of [binary, http]. Transport mode of HiveServer2. hive.server2.logging.operation.log.location /tmp/hive hive.hwi.listen.host 0.0.0.0 This is the host address the Hive Web Interface will listen on hive.server2.webui.host 0.0.0.0 The host address the HiveServer2 WebUI will listen on

三:启动metastore

在启动beeline之前需要先启动hiveserver2,而在启动hiveserver2之前需要先启动metastore。metastore默认的端口为9083。

cd /usr/local/Cellar/hive/3.1.2/bin
hive --service metastore &

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

启动过一定确认一下启动是否成功。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

四:启动hiveserver2

cd /usr/local/Cellar/hive/3.1.2/bin
hive --service hiveserver2 &

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

hiveserver2默认的端口为10000,启动之后一定要查看10000端口是否存在,配置有问题基本上10000端口都启动不成功。10000端口存在不存在是启动beeline的关键。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

五:启动beeline

cd /usr/local/Cellar/hive/3.1.2/bin
beeline -u jdbc:hive2://localhost:10000/default -n mengday -p

  • -n: hive所在的那台服务器的登录账号名称, 这里是我Mac机器的登录用户名mengday, 这里的名字要和core-site.xml中的hadoop.proxyuser.mengday.hosts和hadoop.proxyuser.mengday.groups中mengday保持一致。
  • -p: 密码,用户名对应的密码

看到0: jdbc:hive2://localhost:10000/default>就表示启动成功了。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

六:Hive JDBC

1. 引入依赖
org.apache.hive hive-jdbc 3.1.2
2. 准备数据

/data/employee.txt

1,zhangsan,28,60.66,2020-02-01 10:00:00,true,eat#drink,k1:v1#k2:20,s1#c1#s1#1
2,lisi,29,60.66,2020-02-01 11:00:00,false,play#drink,k3:v3#k4:30,s2#c2#s1#2

3. Java

import java.sql.*;

public class HiveJdbcClient {
private static String url = “jdbc:hive2://localhost:10000/default”;
private static String driverName = “org.apache.hive.jdbc.HiveDriver”;
private static String user = “mengday”;
private static String password = “user对应的密码”;

private static Connection conn = null;
private static Statement stmt = null;
private static ResultSet rs = null;

static {
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void init() throws Exception {
stmt.execute(“drop database if exists hive_test”);
stmt.execute(“create database hive_test”);
rs = stmt.executeQuery(“show databases”);
while (rs.next()) {
System.out.println(rs.getString(1));
}

stmt.execute(“drop table if exists employee”);
String sql = “create table if not exists employee(” +
" id bigint, " +
" username string, " +
" age tinyint, " +
" weight decimal(10, 2), " +
" create_time timestamp, " +
" is_test boolean, " +
" tags array, " +
" ext map<string, string>, " +
" address struct<street:String, city:string, state:string, zip:int> " +
" ) " +
" row format delimited " +
" fields terminated by ‘,’ " +
" collection items terminated by ‘#’ " +
" map keys terminated by ‘:’ " +
" lines terminated by ‘\n’";
stmt.execute(sql);

rs = stmt.executeQuery(“show tables”);
while (rs.next()) {
System.out.println(rs.getString(1));
}

rs = stmt.executeQuery(“desc employee”);
while (rs.next()) {
System.out.println(rs.getString(1) + “\t” + rs.getString(2));
}
}

private static void load() throws Exception {
// 加载数据
String filePath = “/data/employee.txt”;
stmt.execute(“load data local inpath '” + filePath + “’ overwrite into table employee”);

// 查询数据
rs = stmt.executeQuery(“select * from employee”);
while (rs.next()) {
System.out.println(rs.getLong(“id”) + “\t”

  • rs.getString(“username”) + “\t”
  • rs.getObject(“tags”) + “\t”
  • rs.getObject(“ext”) + “\t”
  • rs.getObject(“address”)
    );
    }
    }

private static void close() throws Exception {
if ( rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
.getObject(“tags”) + “\t”

  • rs.getObject(“ext”) + “\t”
  • rs.getObject(“address”)
    );
    }
    }

private static void close() throws Exception {
if ( rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();

### 使用 Hive JDBC 配置连接及示例代码 #### 1. 安装和配置 Cloudera Hive JDBC 驱动 为了实现 HiveJDBC 的集成,首先需要完成 Cloudera Hive JDBC 驱动的安装与配置。这通常涉及以下几个方面: - **下载驱动程序** 可以通过官方提供的链接下载 Hive JDBC Uber 驱动文件 `hive-jdbc-uber-2.6.5.0-292.jar`[^3]。 - **设置环境变量** 将下载好的 JAR 文件放置到项目的类路径下或者指定的库目录中。如果是在 Java 应用中运行,则可以通过 `-cp` 参数加载该 JAR 文件。 - **验证驱动可用性** 确认驱动已成功加载并能够正常工作。此过程可能需要测试简单的数据库查询操作来确认连通性[^1]。 #### 2. 构建连接字符串 构建用于连接 Hive 数据库的 URL 是使用 JDBC 接口的关键部分之一。标准的 Hive JDBC 连接字符串格式如下所示[^4]: ```plaintext jdbc:hive2://<host>:<port>/dbName;sess_var_list?hive_conf_list#hive_var_list ``` 其中 `<host>` 表示 Hive Server 所在主机名或 IP 地址;`<port>` 默认为 10000 或者由具体部署决定;而 `/dbName` 则指定了要访问的具体数据库名称。此外还可以附加一些额外属性以便进一步定制会话行为[^2]。 #### 3. 编写 Java 示例代码 下面给出一段完整的基于上述理论编写的 Java 示例代码片段,展示如何利用 JDBC 来执行基本 SQL 查询命令: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class HiveJdbcClient { private static String driverName = "org.apache.hive.jdbc.HiveDriver"; public static void main(String[] args) throws Exception { try { Class.forName(driverName); // 替换以下参数为你自己的Hive服务信息 Connection con = DriverManager.getConnection( "jdbc:hive2://localhost:10000/default", "", ""); Statement stmt = con.createStatement(); String tableName = "test_hive_jdbc"; stmt.executeQuery("drop table if exists " + tableName); // 清理旧表 System.out.println("Creating table..."); stmt.executeUpdate("create table " + tableName+"(key int, value string)"); System.out.println("Inserting data into the table..."); stmt.execute("insert into "+tableName+" values(1,'value1')"); stmt.execute("insert into "+tableName+" values(2,'value2')"); ResultSet res = stmt.executeQuery("select * from " + tableName); while (res.next()) { // 输出结果集中的每一行数据 System.out.println(res.getInt(1)+"\t"+res.getString(2)); } } catch(Exception e){ e.printStackTrace(); throw new RuntimeException(e.getMessage()); } } } ``` 以上代码展示了创建表格、插入记录以及读取数据的过程。注意替换 `"jdbc:hive2://localhost:10000/default"` 中的相关字段以匹配实际使用的服务器地址和其他必要选项。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值