前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用hive客户端java api读写hive集群上的信息

使用hive客户端java api读写hive集群上的信息

作者头像
用户1225216
发布2018-03-05 14:28:51
3.7K0
发布2018-03-05 14:28:51
举报
文章被收录于专栏:扎心了老铁

上文介绍了hdfs集群信息的读取方式,本文说hive

1、先解决依赖

代码语言:javascript
复制
<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中的配置如下

代码语言:javascript
复制
<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很丰富,下面只介绍了其中一部分,如果用到其他再进行封装即可

代码语言:javascript
复制
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;
    }

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-07-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档