hive java API查表 查数据库 查表结构 查分区

idea中创建的maven项目,首先添加一些基础依赖。
	<properties>
        <hadoop.version>2.7.7</hadoop.version>
        <hive-metastore>1.2.1</hive-metastore>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-mapreduce-client-core</artifactId>
            <version>${hadoop.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-exec</artifactId>
            <version>${hive-metastore}</version>
        </dependency>
    </dependencies>

版本 根据自己的实际场景修改

添加了这些依赖后hive的DDL操作基本上都能实现。
但是!我最开始研究时以为分区表的所有分区应该也是有对应函数能直接获取的,直到搜了好久 ~ 反思:具体的分区已经算是表的数据了,需要连接hive库,以SQL形式获取。
需要添加hive-jdbc依赖
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-jdbc</artifactId>
            <version>1.1.0</version>
        </dependency>
遇到的问题:引入hive-jdbc时版本填错了会报错!

问题及解决:https://blog.csdn.net/u010379814/article/details/60755846

相当于是以客户端的形式连接hiveserver2服务,所以确保hiveserver2服务启动。url等信息模板:
    private static String driverName = "org.apache.hive.jdbc.HiveDriver";
    private static String url = "jdbc:hive2://master:10000/default";
    private static String user = "root";
    private static String password = "";

这时我只需要写 String sql = "show partitions "+dbName+"."+tbName;
就可实现获取指定表的所有分区。

记录一下我的完整代码:
package com.dragon.util;

import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.api.MetaException;
import org.apache.hadoop.hive.metastore.api.Table;
import org.apache.thrift.TException;
import org.junit.After;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class HiveMetaStoreOption {
    private static String driverName = "org.apache.hive.jdbc.HiveDriver";
    private static String url = "jdbc:hive2://master:10000/default";
    private static String user = "root";
    private static String password = "";

    protected static HiveMetaStoreClient client;
    protected static HiveConf hiveConf;
    protected static Connection conn = null;
    protected static Statement stat = null;
    protected static ResultSet rs = null;

    static {

        try {
            hiveConf = new HiveConf();
            hiveConf.addResource("hive-site.xml");
            client = new HiveMetaStoreClient(hiveConf);
            Class.forName(driverName);
            conn = DriverManager.getConnection(url,user,password);
            stat = conn.createStatement();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    // 获取数据库
    public static List<String> getAllDatabases(){
        List<String> allDatabases = null;
        try {
            allDatabases = client.getAllDatabases();
        } catch (TException e) {
            e.printStackTrace();
        }
        return allDatabases;
    }

    // 获取指定数据库所有表
    public static List<String> getTables(String dbName){
        List<String> tables = null;
        try {
            tables = client.getAllTables(dbName);
        } catch (MetaException e) {
            e.printStackTrace();
        }
        return tables;
    }

    // 获取指定表所有字段信息
    public static List<FieldSchema> showSchemas(String dbName,String tbName){
        List<FieldSchema> schemas = null;
        try {
            Table table = client.getTable(dbName, tbName);
            schemas = table.getSd().getCols();
            if(table.isSetPartitionKeys()){
                List<FieldSchema> partitionKeys = table.getPartitionKeys();
                for (FieldSchema key : partitionKeys) {
                    key.setComment("partition key");
                }
                schemas.addAll(partitionKeys);
            }
        } catch (TException e) {
            e.printStackTrace();
        }
        return schemas;
    }

    public static List<String> showPartitions(String dbName,String tbName){
        List<String> pars = new ArrayList<>();
        try {
            String sql = "show partitions "+dbName+"."+tbName;
            System.out.println("Running: " + sql);
            rs = stat.executeQuery(sql);
            while(rs.next()){
                String p1 = rs.getString(1);
                pars.add(p1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pars;
    }

    // 释放资源
    @After
    public void destory() throws SQLException {
        if (rs != null){
            rs.close();
        }
        if (stat != null){
            stat.close();
        }
        if (conn != null){
            conn.close();
        }
    }

}

对了,最后面有个@After,所以这个依赖也得加上:

		<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

参考:
hive 1.2.2 api 文档:http://hive.apache.org/javadocs/r1.2.2/api/index.html
一开始依赖参考:https://www.cnblogs.com/kangoroo/p/7221548.html
jdbc引入:https://www.cnblogs.com/takemybreathaway/articles/9750175.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

总要冲动一次

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值