hive的本地模式和远程模式有什么区别
hive本质上是将sql语法解析为mapreduce的过程,既然如此它就必须提交mapreduce任务到resoucemanager,那么它如何提交?就是通过hadoop提供的命令hadoop jar命令来提交。
本地模式:简单的理解,hive客户端仅供本地使用,直接使用hive命令,不需要指定IP 端口
远程模式:简单的理解,将hive发布成一个服务进程,通过hive –service hiveserver命令,那么其hive客户端就可以连接hive的服务进程其他客户端可以是jdbc方式、hive提供的beeline命令等,既然要连接远端的hive服务进程,那自然需要指定IP 端口,这里的IP指的是hive服务进程所在的IP,端口自然也是,也自然与hadoop无关。所以不要混淆
hive的三种连接方式
hive 命令行模式
hive –service cli
hive web界面的 (端口号9999) 启动方式
hive –service hwi &
hive 远程服务 (端口号10000) 启动方式
hive –service hiveserver &
hive –service hiveserver -p 10002 (指定端口)
hive中jdbc使用
- 创建表
hive> create table employee(eid int, name String, dept String, yoj String)
ROW FORMAT DELIMITED FIELDS TERMINATED BY “,”
LINES TERMINATED BY “\n” STORED AS TEXTFILE; - 创建一个maven项目添加maven仓库依赖
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-metastore -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-metastore</artifactId>
<version>1.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-metastore -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.2.1</version>
</dependency>
- Java测试代理案列
package com.hyg.hadoop.hive;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class HiveJdbcTest {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "", "");
Statement stmt = con.createStatement();
String tableName = "employee";
// describe table
String sql = "describe " + tableName;
System.out.println("Running: " + sql);
ResultSet res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1) + "\t" + res.getString(2));
}
//load data
sql = "load data local inpath '/root/employee.txt' overwrite into table "+tableName;
System.out.println("Running: " + sql);
//stmt.executeQuery(sql);
System.out.println("Load Data into "+tableName+" successful");
//show table data
sql = "select * from employee";
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1)+"--"+res.getString(2)+"--"+res.getString(3)+"--"+res.getString(4));
}
}
}
启动后台hiveserver遇见问题
Exception in thread "main" java.lang.ClassNotFoundException: org.apache.hadoop.hive.service.HiveServer
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:274)
at org.apache.hadoop.util.RunJar.main(RunJar.java:205)
^C
[1]+ Exit 1 hive --service hiveserver
解决办法: hive –service hiveserver2 使用hiveserver2
加载hive驱动报错
*驱动路径为:*org.apache.hive.jdbc.HiveDriver