使用hive客户端java api读写hive集群上的信息
时间:2022-05-02
本文章向大家介绍使用hive客户端java api读写hive集群上的信息,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
上文介绍了hdfs集群信息的读取方式,本文说hive
1、先解决依赖
<properties>
<hive.version>1.2.1</hive.version>
</properties>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>${hive.version}</version>
<scope>provided</scope>
</dependency>
2、配置文件
这里我们给出一种简单的配置方法,就是直接将hive-site.xml通过添加文件的方式加载到配置
例如,hive-site.xml中的配置如下
<configuration>
<property>
<name>hive.metastore.uris</name>
<value>thrift://10.91.64.23:9083,thrift://10.91.64.23:9083,thrift://10.91.64.23:9083</value>
</property>
</configuration>
3、hive client api
说明:
1、hiveConf.addResource("hive-site.xml") 可以直接把配置文件加载到配置
2、hive的api很丰富,下面只介绍了其中一部分,如果用到其他再进行封装即可
package com.xiaoju.dqa.prometheus.client.hive;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.IMetaStoreClient;
import org.apache.hadoop.hive.metastore.RetryingMetaStoreClient;
import org.apache.hadoop.hive.metastore.api.Database;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.api.MetaException;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import java.util.List;
public class HiveClient {
protected final Logger logger = org.slf4j.LoggerFactory.getLogger(this.getClass());
IMetaStoreClient client;
public HiveClient() {
try {
HiveConf hiveConf = new HiveConf();
hiveConf.addResource("hive-site.xml");
client = RetryingMetaStoreClient.getProxy(hiveConf);
} catch (MetaException ex) {
logger.error(ex.getMessage());
}
}
public List<String> getAllDatabases() {
List<String> databases = null;
try {
databases = client.getAllDatabases();
} catch (TException ex) {
logger.error(ex.getMessage());
}
return databases;
}
public Database getDatabase(String db) {
Database database = null;
try {
database = client.getDatabase(db);
} catch (TException ex) {
logger.error(ex.getMessage());
}
return database;
}
public List<FieldSchema> getSchema(String db, String table) {
List<FieldSchema> schema = null;
try {
schema = client.getSchema(db, table);
} catch (TException ex) {
logger.error(ex.getMessage());
}
return schema;
}
public List<String> getAllTables(String db) {
List<String> tables = null;
try {
tables = client.getAllTables(db);
} catch (TException ex) {
logger.error(ex.getMessage());
}
return tables;
}
public String getLocation(String db, String table) {
String location = null;
try {
location = client.getTable(db, table).getSd().getLocation();
}catch (TException ex) {
logger.error(ex.getMessage());
}
return location;
}
}
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- Docker私有镜像仓库是什么?
- React Native布局详细指南
- 走进Golang之Context的使用
- 「Workshop」第十一期:降维
- 开始在 GitHub 上写博客
- 微信公众号菜单点击发送天气预报
- SAP CRM Fiori应用如何启用Sales Office和Sales Group两个字段
- 通过注册表调整 Windows 8 窗口边框宽度
- 在 Mac OS X 中创建和使用内存盘
- Mono for Android 下的 ListActivity
- 使用JavaScript Function.prototype进行代码重构的一些例子
- Activity 生命周期及其栈管理方式
- 如何操作SAP UI5应用Footer区域工具栏按钮的背景颜色
- 我的第一个 Mono for Android 应用
- 【DB笔试面试851】在Oracle中,造成“ORA-28040: No matching ...”错误的原因是什么?