使用hive用戶端java api讀寫hive叢集上的資訊

來源:互聯網
上載者:User

標籤:group   讀取   location   資訊   ret   his   用戶端   hive   code   

上文介紹了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;    }}

 

使用hive用戶端java api讀寫hive叢集上的資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.