Read and write information on a hive cluster using the Hive client Java API

Source: Internet
Author: User

This article describes how the HDFs cluster information is read, and this paper says that hive

1. Resolve Dependencies First

<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. Configuration files

Here we give a simple configuration method, which is to directly load the hive-site.xml by adding files to the configuration

For example, the configuration in Hive-site.xml is as follows

<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

Description

1, Hiveconf.addresource ("Hive-site.xml") can directly load the configuration file into the configuration

2, Hive API is very rich, the following is only part of the introduction, if the use of other packaging can be

 Packagecom.xiaoju.dqa.prometheus.client.hive;Importorg.apache.hadoop.hive.conf.HiveConf;Importorg.apache.hadoop.hive.metastore.IMetaStoreClient;Importorg.apache.hadoop.hive.metastore.RetryingMetaStoreClient;Importorg.apache.hadoop.hive.metastore.api.Database;ImportOrg.apache.hadoop.hive.metastore.api.FieldSchema;Importorg.apache.hadoop.hive.metastore.api.MetaException;Importorg.apache.thrift.TException;ImportOrg.slf4j.Logger;Importjava.util.List; Public classhiveclient {protected FinalLogger Logger = Org.slf4j.LoggerFactory.getLogger ( This. GetClass ());    Imetastoreclient client;  Publichiveclient () {Try{hiveconf hiveconf=Newhiveconf (); Hiveconf.addresource ("Hive-site.xml"); Client=Retryingmetastoreclient.getproxy (hiveconf); } Catch(Metaexception ex) {Logger.error (Ex.getmessage ()); }    }     PublicList<string>getalldatabases () {List<String> databases =NULL; Try{Databases=client.getalldatabases (); } Catch(Texception ex) {Logger.error (Ex.getmessage ()); }        returndatabases; }     Publicdatabase Getdatabase (String db) {database Database=NULL; Try{Database=client.getdatabase (DB); } Catch(Texception ex) {Logger.error (Ex.getmessage ()); }        returndatabase; }     PublicList<fieldschema>GetSchema (string db, String table) {List<FieldSchema> schema =NULL; Try{Schema=Client.getschema (db, table); } Catch(Texception ex) {Logger.error (Ex.getmessage ()); }        returnschema; }     PublicList<string>getalltables (String db) {List<String> tables =NULL; Try{Tables=client.getalltables (DB); } Catch(Texception ex) {Logger.error (Ex.getmessage ()); }        returntables; }     Publicstring GetLocation (string db, String table) {String location=NULL; Try{ Location=client.gettable (db, table). GETSD (). GetLocation (); }Catch(Texception ex) {Logger.error (Ex.getmessage ()); }        returnLocation ; }}

Read and write information on a hive cluster using the Hive client Java API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.