HiveServer2&beeline的使用
概述
网址:
https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients
Hive仅仅是一个客户端而已,在生产上是不需要部署集群的
Hive客户端有几大类:
- hive
- WebUI: 通过HUE/Zeppelin来对Hive表进行操作
- jdbc
HiveServer2的使用
HiveServer的概念:
在hive的机器上启动一个server:客户端可以通过ip + port的方式对其进行访问
之后,就可以有很多客户端连到这个server上面去干活
可以采用jdbc、odbc、beeline的方式进行连接
启动HiveServer2:
$>cd $HIVE_HOME/bin
$>./hiveserver2
beeline的使用
启动beeline:
##-n 指定机器登陆的名字,当前机器的登陆用户名
##-u 指定一个连接串
##每成功运行一个命令,hiveserver2启动的那个窗口,只要在启动beeline的窗口中执行成功一条命令,另外个窗口随即打印一个OK
##如果命令错误,hiveserver2那个窗口就会抛出异常
$>./beeline -u jdbc:hive2://hadoop003:10000/default -n hadoop
运行效果:
自己设置端口,使用HiveServer2与beeline
设置hiveserver的端口:
$>./hiveserver2 --hiveconf hive.server2.thrift.port=4444
启动beeline客户端:
$>./beeline -u jdbc:hive2://hadoop003:4444/default -n hadoop
Hive JDBC编程
注意点:
JDBC是客户端的访问方式,因此需要启动一个server即HiveServer2
使用maven构建项目,pom.xml文件如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhaotao.bigdata</groupId>
<artifactId>hive-train</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>hive-train</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hadoop.version>2.6.0-cdh5.7.0</hadoop.version>
<hive.version>1.1.0-cdh5.7.0</hive.version>
</properties>
<repositories>
<repository>
<id>cloudera</id>
<url>http://repository.cloudera.com/artifactory/cloudera-repos</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>${hive.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>${hive.version}</version>
</dependency>
</dependencies>
</project>
程序:
package com.zhaotao.bigdata.hive;
import java.sql.*;
/**
* Hive JDBC JAVA API 访问
* CRUD 操作
* Created by 陶 on 2017/10/7.
*/
public class HiveJDBC {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
/**
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
//replace "hive" here with the name of the user the queries should run as
Connection con = DriverManager.getConnection("jdbc:hive2://192.168.26.133:4444/default", "root", "");
Statement stmt = con.createStatement();
// 用于测试创建新表
String tableName = "testHiveDriverTable";
// 用于查询已经存在的emp的数据
// String tableName = "emp";
// 若在windows操作系统下,执行下列操作的时候,会报权限问题
// 问题的根源所在是因为HDFS中文件的权限不足以windows的用户进行操作
// 解决方法:修改权限
// 删除表
stmt.execute("drop table if exists " + tableName);
// 创建表
stmt.execute("create table " + tableName + " (key int, value string)");
// 展示表名
// String sql = "show tables '" + tableName + "'";
// System.out.println("Running: " + sql);
// ResultSet res = stmt.executeQuery(sql);
// if (res.next()) {
// System.out.println(res.getString(1));
// }
// 表描述符
// sql = "describe " + tableName;
// System.out.println("Running: " + sql);
// res = stmt.executeQuery(sql);
// while (res.next()) {
// System.out.println(res.getString(1) + "\t" + res.getString(2));
// }
// 加载数据到Hive表中
// String filepath = "/opt/data/emp.txt";
// sql = "load data local inpath '" + filepath + "' into table " + tableName;
// System.out.println("Running: " + sql);
// stmt.execute(sql);
// 查询Hive表中数据
// sql = "select * from " + tableName;
// System.out.println("Running: " + sql);
// res = stmt.executeQuery(sql);
// while (res.next()) {
// System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
// }
// 统计表中的行数
// 由于该Hive ql会执行MapReduce,因此需要注意的问题就是权限问题
// 因为在运行的时候,会在HDFS中/tmp的目录下存放临时文件
// 具体错为:Permission denied: user=root, access=EXECUTE, inode="/tmp/hadoop-yarn":hadoop:supergroup:drwx------
// 解决方法:修改/tmp的执行权限 或是 在用户名一致的情况下去运行该程序
// sql = "select count(1) from " + tableName;
// System.out.println("Running: " + sql);
// res = stmt.executeQuery(sql);
// while (res.next()) {
// System.out.println(res.getString(1));
// }
}
}