Java访问Hive获取表信息

1、pom依赖

    <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-jdbc</artifactId>
            <version>${hive.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-exec</artifactId>
            <version>${hive.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-metastore</artifactId>
            <version>${hive.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-cli</artifactId>
            <version>${hive.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-common</artifactId>
            <version>${hive.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-service</artifactId>
            <version>${hive.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-shims</artifactId>
            <version>${hive.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hive.hcatalog</groupId>
            <artifactId>hive-hcatalog-core</artifactId>
            <version>${hive.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.thrift</groupId>
            <artifactId>libfb303</artifactId>
            <version>0.9.3</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

2、代码编写

 public static void main(String[] args) throws Exception {
        Class.forName("org.apache.hive.jdbc.HiveDriver");
        Connection con = DriverManager.getConnection("jdbc:hive2://IP:10000/default", "hive", "hive");
        Statement st = con.createStatement();
        //查询hive表数据
        ResultSet rs = st.executeQuery("SELECT * from ods.dept");
        while (rs.next()){
            System.out.println(rs.getString(1) + "," + rs.getString(2));
        }

		//查询hive表结构---为了保证字段顺序
        Map<String, String> allColumnsAndType = new LinkedHashMap<>();
         rs = con.createStatement().executeQuery("describe " + "src_config_cs.src_paramter");
        if (rs != null) {
            while (rs.next()) {
                String col_name = rs.getString("col_name");
                if (!StringUtils.isBlank(col_name)) {
                    allColumnsAndType.put(rs.getObject("col_name").toString(), rs.getObject("data_type").toString());
                } else break;
                System.out.println(rs.getString("col_name") + "\t" + rs.getString("data_type"));
            }
        }
        //查询hive表结构
        HiveConf hiveConf = new HiveConf();
        hiveConf.set("hive.metastore.uris","thrift://IP:9083");
        HiveMetaStoreClient hiveMetaStoreClient = new HiveMetaStoreClient(hiveConf);
        //database默认为“default”
        Database database= hiveMetaStoreClient.getDatabase("src");
        //获取database下的所有table
        List<String> tablesList = hiveMetaStoreClient.getAllTables("ods");

        for(String table:tablesList)
        {
            System.out.println(table);
        }
		// 获取 Hive 表的分区字段
        Table table = hiveMetaStoreClient.getTable("ods", "ods_merchant");
        List<FieldSchema> schemas = table.getSd().getCols();

        if(table.isSetPartitionKeys()){
            List<FieldSchema> partitionKeys = table.getPartitionKeys();
            for (FieldSchema key : partitionKeys) {
                key.setComment("partition key");
            }
            schemas.addAll(partitionKeys);
            for (FieldSchema key : partitionKeys) {
                System.out.println(key);
            }
        }

        hiveMetaStoreClient.close();

        rs.close();
        st.close();
        con.close();

    }
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值