09 java访问metastore

2020-06-16  本文已影响0人  张力的程序园

本节将展示使用java代码访问hive的metastore服务。

1、前提约束

2、操作步骤

        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-metastore</artifactId>
            <version>2.3.4</version>
        </dependency>
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
import org.apache.hadoop.hive.metastore.api.Database;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.api.Table;

import java.util.List;

public class TestMetastore {
    public static void main(String[] args) throws Exception {
        System.setProperty("hadoop.home.dir", "C:/hadoop2.7.2");
        HiveConf hiveConf = new HiveConf();
        hiveConf.set("hive.metastore.uris","thrift://192.168.100.142:9083");
        HiveMetaStoreClient hiveMetaStoreClient = new HiveMetaStoreClient(hiveConf);
        //database默认为“default”
        Database database= hiveMetaStoreClient.getDatabase("default");
        //获取database下的所有table
        List<String> tablesList = hiveMetaStoreClient.getAllTables("default");

        for(String table:tablesList)
        {
            System.out.println(table);
        }
        hiveMetaStoreClient.close();
    }
}

以上就是java访问metastore的简单demo。

上一篇 下一篇

猜你喜欢

热点阅读