Java是通过beeline来连接Hive的。启动beeline最重要的就是配置好hive-site.xml。
其中javax.jdo.option.ConnectionURL涉及到一个数据库,最好重新删掉原来的metastore数据库然后重新创建一个并初始化一下。
mysql> create database metastore;
cd /usr/local/Cellar/hive/3.1.2/libexec/bin
schematool -initSchema -dbType mysql
hive-site.xml
hive.metastore.local true hive.metastore.uris thrift://localhost:9083 Thrift URI for the remote metastore. Used by metastore client to connect to remote metastore. javax.jdo.option.ConnectionURL jdbc:mysql://localhost:3306/metastore?characterEncoding=UTF-8&createDatabaseIfNotExist=true javax.jdo.option.ConnectionDriverName com.mysql.cj.jdbc.Driver javax.jdo.option.ConnectionUserName root javax.jdo.option.ConnectionPassword root123 hive.exec.local.scratchdir /tmp/hive hive.downloaded.resources.dir /tmp/hive hive.metastore.warehouse.dir /data/hive/warehouse hive.metastore.event.db.notification.api.auth false hive.server2.active.passive.ha.enable true hive.server2.transport.mode binary Expects one of [binary, http]. Transport mode of HiveServer2. hive.server2.logging.operation.log.location /tmp/hive hive.hwi.listen.host 0.0.0.0 This is the host address the Hive Web Interface will listen on hive.server2.webui.host 0.0.0.0 The host address the HiveServer2 WebUI will listen on在启动beeline之前需要先启动hiveserver2,而在启动hiveserver2之前需要先启动metastore。metastore默认的端口为9083。
cd /usr/local/Cellar/hive/3.1.2/bin
hive --service metastore &
启动过一定确认一下启动是否成功。
cd /usr/local/Cellar/hive/3.1.2/bin
hive --service hiveserver2 &
hiveserver2默认的端口为10000,启动之后一定要查看10000端口是否存在,配置有问题基本上10000端口都启动不成功。10000端口存在不存在是启动beeline的关键。
cd /usr/local/Cellar/hive/3.1.2/bin
beeline -u jdbc:hive2://localhost:10000/default -n mengday -p
看到0: jdbc:hive2://localhost:10000/default>就表示启动成功了。
/data/employee.txt
1,zhangsan,28,60.66,2020-02-01 10:00:00,true,eat#drink,k1:v1#k2:20,s1#c1#s1#1
2,lisi,29,60.66,2020-02-01 11:00:00,false,play#drink,k3:v3#k4:30,s2#c2#s1#2
import java.sql.*;
public class HiveJdbcClient {
private static String url = “jdbc:hive2://localhost:10000/default”;
private static String driverName = “org.apache.hive.jdbc.HiveDriver”;
private static String user = “mengday”;
private static String password = “user对应的密码”;
private static Connection conn = null;
private static Statement stmt = null;
private static ResultSet rs = null;
static {
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void init() throws Exception {
stmt.execute(“drop database if exists hive_test”);
stmt.execute(“create database hive_test”);
rs = stmt.executeQuery(“show databases”);
while (rs.next()) {
System.out.println(rs.getString(1));
}
stmt.execute(“drop table if exists employee”);
String sql = “create table if not exists employee(” +
" id bigint, " +
" username string, " +
" age tinyint, " +
" weight decimal(10, 2), " +
" create_time timestamp, " +
" is_test boolean, " +
" tags array, " +
" ext map
" address struct
" ) " +
" row format delimited " +
" fields terminated by ‘,’ " +
" collection items terminated by ‘#’ " +
" map keys terminated by ‘:’ " +
" lines terminated by ‘\n’";
stmt.execute(sql);
rs = stmt.executeQuery(“show tables”);
while (rs.next()) {
System.out.println(rs.getString(1));
keys terminated by ‘:’ " +
" lines terminated by ‘\n’";
stmt.execute(sql);
rs = stmt.executeQuery(“show tables”);
while (rs.next()) {
System.out.println(rs.getString(1));
[外链图片转存中…(img-4WGrmupQ-1635265367109)]