hive metastore java api使用使用
hive metastore
hive metastore是hive的元数据管理服务,实际应用中很多第三方框架需要访问metastore服务,如spark,impala等。同样hive metastore也提供
了java接口。
使用使用
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.Table;
import org.apache.thrift.TException;
import java.util.List;
public class MetaStoreDemo {
public static void main(String[] args) throws TException {
HiveConf hiveConf = new HiveConf();
hiveConf.addResource("hive-site.xml");
HiveMetaStoreClient client = new HiveMetaStoreClient(hiveConf);
//获取数据库信息
List tablesList = client.getAllTables("test");
System.out.print("test数据所有的表: ");
for (String s : tablesList) {
System.out.print(s + " ");
}
System.out.println();
//获取表信息
System.out.println("test.t3 表信息: ");
Table table= client.getTable("test","t3");
List fieldSchemaList= table.getSd().getCols();
for (FieldSchema schema: fieldSchemaList) {
System.out.println("字段: " + schema.getName() + ", 类型: " + schema.getType());
}
client.close();
}
}
pom文件中添加如下依赖
org.apache.hive
hive-metastore
1.2.2
同时需要将hive配置文件hive-site.xml拷贝到工程资源目录resources下
运行结果:
作者:苍老流年
评论0