赞
踩
在启动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<string, string>, " +
" address struct<street:String, city:string, state:string, zip:int> " +
" ) " +
" 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));
}
rs = stmt.executeQuery(“desc employee”);
while (rs.next()) {
System.out.println(rs.getString(1) + “\t” + rs.getString(2));
}
}
private static void load() throws Exception {
// 加载数据
String filePath = “/data/employee.txt”;
stmt.execute(“load data local inpath '” + filePath + “’ overwrite into table employee”);
// 查询数据
rs = stmt.executeQuery(“select * from employee”);
while (rs.next()) {
System.out.println(rs.getLong(“id”) + “\t”
private static void close() throws Exception {
if ( rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
public static void ma
必看视频!获取2024年最新Java开发全套学习资料 备注Java
in(String[] args) throws Exception {
init();
load();
close();
}
}
按照上面的过程,4个月的时间刚刚好。当然Java的体系是很庞大的,还有很多更高级的技能需要掌握,但不要着急,这些完全可以放到以后工作中边用别学。
学习编程就是一个由混沌到有序的过程,所以你在学习过程中,如果一时碰到理解不了的知识点,大可不必沮丧,更不要气馁,这都是正常的不能再正常的事情了,不过是“人同此心,心同此理”的暂时而已。
“道路是曲折的,前途是光明的!”
掌握,但不要着急,这些完全可以放到以后工作中边用别学。
学习编程就是一个由混沌到有序的过程,所以你在学习过程中,如果一时碰到理解不了的知识点,大可不必沮丧,更不要气馁,这都是正常的不能再正常的事情了,不过是“人同此心,心同此理”的暂时而已。
“道路是曲折的,前途是光明的!”
[外链图片转存中…(img-ZsKs6dUx-1716442589040)]
[外链图片转存中…(img-jDs6bHyO-1716442589041)]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。