2020年寒假假期总结0202
Java连接hive简单操作
首先在使用java连接前我们需要在虚拟机上开启hiveserver2(hiveserver2需要安装),hiveserve2的作用是实现hive可以进行并发操作,否则没有办法实现java对hive的操作。
使用命令:hiveserver2即可打开hiveserver2服务,需要注意的是,开启服务后这个命令窗口就会被占用,即出现一下情况:

此时已经代表服务已经开启,我们就不需要动这个终端窗口了,可以再开一个,因为下面这个窗口会显示你的hive操作是否成功,成功此时ok,错误会显示错误原因。即下图

注意点:如果在启动hive出现:cannot access /usr/local/spark/lib/spark-assembly-*.jar: No such file or directory,是因为这个jar包在新版本的spark中的位置已经改变!我们要做的只是将hive中的启动文件中的sparkAssemblyPath这一行更改为你安装的spark的jar包路径即可。我们找到hive文件夹下的bin目录,打开hive文件,找到这句话
# add Spark assembly jar to the classpath
if [[ -n "$SPARK_HOME" ]]
then
sparkAssemblyPath=`ls ${SPARK_HOME}/lib/spark-assembly-*.jar` CLASSPATH="${CLASSPATH}:${sparkAssemblyPath}"
fi然后改成下面内容即可:
# add Spark assembly jar to the classpath
if [[ -n "$SPARK_HOME" ]]
then
sparkAssemblyPath=`ls ${SPARK_HOME}/jars/*.jar`
CLASSPATH="${CLASSPATH}:${sparkAssemblyPath}"
fi下面就可以进行编程操作了。
1.添加依赖:
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.1.2</version>
</dependency>注意点:不少次添加依赖之后,project就会报错Failed to read artifact descriptor**jar之类的错误,基本上就是加入依赖只有jar没有下载成功或者不完全,也试过不少中方法,总结一下:
1.需要有一个良好的网络环境,有的时候即使是显示下完了也是有这样的显示,那就是没有下载好,这个良好的网络环境其实很难说,因为我用移动的网络很可能一下午都没有下好,电信可能一会就结束了,很费解。
2.使用国内的maven仓库,建议使用阿里云或者是华为云,阿里云在我看来下载速度可能更好一点。
<mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror>
3.点击页面右边的mavenProject重新下载资源:

4.在文件中右击pom.xml选择maven,点击以下选项尝试重新下载。


hive操作基本的api:
public class HiveApi {
// 驱动,固定的
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
// 默认就是10000端口,ip地址使用hive服务器的
private static String url = "jdbc:hive2://192.168.133.130:10000/default";
// hive连接的用户名和密码,默认就算是下面这两个
private static String user = "账号";
private static String password = "密码";
// 公共使用的变量
private static Connection conn = null;
private static Statement stmt = null;
private static ResultSet rs = null;
// 加载驱动、创建连接
public static void init() throws Exception {
Class.forName(driverName);
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
}
// 释放资源
public static void destory() throws Exception {
if ( rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
// 测试代码(每次都需要现在加载,执行万后释放)
public static void main(String[] args) throws Exception {
init();
// 创建表功能通过
// createTable();
// 显示表名称
// showTables();
// 显示表描述
// descTable();
// 本地数据导入
// loadData();
// 查询数据
selectData();
// 运行mapreduce作业
//countData();
// 执行删除
// dropTable();
destory();
}
// 创建表
public static void createTable() throws Exception {
String sql = "create table capital_info (id int, question string) row format delimited fields terminated by ‘,‘";
stmt.execute(sql);
}
// 查询所有表
public static void showTables() throws Exception {
String sql = "show tables";
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString(1));
}
}
// 查看表结构
public static void descTable() throws Exception {
String sql = "desc capital_info";
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString(1) + "\t" + rs.getString(2));
}
}
// 加载数据
public static void loadData() throws Exception {
String filePath = "/usr/local/data/capital_info.txt";
String sql = "load data local inpath ‘" + filePath + "‘ overwrite into table capital_info";
stmt.execute(sql);
}
// 查询数据
public static void selectData() throws Exception {
String sql = "select * from capital_info limit 20";
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString("id") + "\t\t" + rs.getString("question"));
}
}
// 统计查询(会运行mapreduce作业)
public static void countData() throws Exception {
String sql = "select count(1) from capital_info";
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getInt(1) );
}
}
// 删除数据库表
public static void dropTable() throws Exception {
String sql = "drop table if exists capital_info";
stmt.execute(sql);
}
}注意点:hive的账号和密码可以在hive目录下conf目录的hive-site.xml中可以查看到:

相关推荐
archive 2020-07-30
成长之路 2020-07-28
eternityzzy 2020-07-19
taisenki 2020-07-05
tugangkai 2020-07-05
SignalDu 2020-07-05
zlsdmx 2020-07-05
tomson 2020-07-05
tugangkai 2020-07-04
tomson 2020-07-05
Zhangdragonfly 2020-06-28
genshengxiao 2020-06-26
成长之路 2020-06-26
tomson 2020-06-26
蜗牛之窝 2020-06-26
成长之路 2020-06-25