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