条件:安装好eclipse和hive
创建项目引入jar包
package hivetest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class HiveTest {
public static void main(String[] args) throws Exception {
Class.forName("org.apache.hive.jdbc.HiveDriver");
Connection conn=DriverManager.getConnection("jdbc:hive2://localhost:10000/default","","");
Statement stmt=conn.createStatement();
String query_sql="select * from people";
ResultSet rs=stmt.executeQuery(query_sql);
String str="";
try {
ResultSetMetaData rsmd = rs.getMetaData();
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
str+=rsmd.getColumnName(i)+",";
}
System.out.println(str);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
遇到的问题:
SunandLi is not allowed to impersonate anonymous
修改core-site.xml中文件内容,在原文件内容中添加:
<property>
<name>hadoop.proxyuser.SunandLi.hosts</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.SunandLi.groups</name>
<value>*</value>
</property>
然后重启一下hadoop,等一下运行,因为hadoop刚启动时出去安全模式,运行会报
Name node is in safe mode.