vim /opt/hadoop/etc/hadoop/core-site.xml
# 快捷键是G然后k然后o,从而在配置项中添加以下两项:
<property>
<name>hadoop.proxyuser.root.hosts</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.root.groups</name>
<value>*</value>
</property>
必要的话重新启动HDFS:
stop-dfs.sh
jps
start-dfs.sh
启动hiveserver2服务:
# hive --service metastore &
hive --service hiveserver2 &
查看hiveserver2是否已经开启:
# 可以打开新的终端运行命令,和上面进程的输出信息隔开:
netstat -nl | grep 10000
启动beeline:
beeline
在beeline中连接 hive serve2:
!connect jdbc:hive2://localhost:10000/default
输入用户名 root,密码留空(直接按回车)
执行sql命令:
-- use default;
create table testHiveDriverTable(
sid int, sname string, sage int, sdept string
) row format delimited fields terminated by ',';
-- 查询表:
show tables;
关闭连接:
!close
退出beeline:
!quit
远程下载工程代码:
git clone https://git.code.tencent.com/lacus_w/hive-labs.git
cd hive-labs/HiveJDBCConnect
vi src/main/java/com/z3/examples/HiveJDBCConnect.java
package com.z3.examples;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class HiveJDBCConnect {
public static void main(String[] args) {
Connection con = null;
try {
String conStr = "jdbc:hive2://localhost:10000/default";
Class.forName("org.apache.hive.jdbc.HiveDriver");
con = DriverManager.getConnection(conStr, "root", "");
Statement stmt = con.createStatement();
// show tables:
String sql = "show tables";
System.out.println("正在执行的命令是: " + sql);
ResultSet res = stmt.executeQuery(sql);
if (res.next()) {
System.out.println(res.getString(1));
}
// create table:
for (int i = 0; i < 100; i++) {
sql = "create table if not exists tb" + Integer.toString(i) + " (key int, value string)";
stmt.execute(sql);
System.out.printf("表%d已创建,\n", i);
}
System.out.println("命令已全部执行。");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
if (con != null)
con.close();
} catch (Exception ex) {
}
}
}
}
注意在pom.xml中添加该程序的依赖:
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>3.1.3</version>
<scope>provided</scope>
</dependency>
编译打包jar程序:
mvn compile package
查看编译得到的jar程序:
ll target/*.jar
# target/HiveJDBCConnect-1.0-jar-with-dependencies.jar
下载依赖:
mvn dependency:copy-dependencies
设置classpath变量:
export CLASSPATH=target/HiveJDBCConnect-1.0-jar-with-dependencies.jar
for i in target/dependency/*.jar ; do
CLASSPATH=$CLASSPATH:$i
done
echo $CLASSPATH
运行主函数:
java -cp $CLASSPATH com.z3.examples.HiveJDBCConnect
参考效果:
参考:
https://cwiki.apache.org/confluence/display/Hive/HiveJDBCInterface
https://cwiki.apache.org/confluence/display/Hive/HiveClient#HiveClient-JDBC
https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。