声明:我不生成代码,只是代码的搬运工,然后整合优化下,毕竟轮子都是圆的
maven依赖
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.1.0</version>
</dependency>
常见报错:
错误1:java.lang.NoClassDefFoundError....
解决方法:添加hadoop-common依赖
错误2:Required field 'client_protocol' is unset!
解决方法:查看服务器hive版本与hive-jdbc版本保持一致
查看hive版本:
常规Jdbc
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JDBCHive {
private static String Driver = "org.apache.hive.jdbc.HiveDriver";
private static String url = "jdbc:hive2://192.168.1.10:10000";
private static String name = "worker";
private static String password = "xxxxxx";
public static void main(String[] args) {
try {
Class.forName(Driver);
Connection conn = DriverManager.getConnection(url, name, password);
Statement stat = conn.createStatement();
String sql = "show databases";
ResultSet rs = stat.executeQuery(sql);
List<Map<String, Object>> maps = ResultSetToList(rs);
System.out.println(maps);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static List<Map<String, Object>> ResultSetToList(ResultSet rs) throws SQLException {
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
ResultSetMetaData rsmd = rs.getMetaData();
int colCount = rsmd.getColumnCount();
List<String> colNameList = new ArrayList<String>();
for (int i = 0; i < colCount; i++) {
colNameList.add(rsmd.getColumnName(i + 1));
}
while (rs.next()) {
for (int i = 0; i < colCount; i++) {
Map map = new HashMap<String, Object>();
String key = colNameList.get(i);
Object value = rs.getString(colNameList.get(i));
map.put(key, value);
results.add(map);
}
}
return results;
}
}