Hive之 hive的三种使用方式(CLI、HWI、Thrift)
来源:互联网 发布:飞思卡尔k60单片机 编辑:程序博客网 时间:2024/05/17 02:22
Hive有三种使用方式——CLI命令行,HWI(hie web interface)浏览器 以及 Thrift客户端连接方式。
1、hive 命令行模式
直接输入/hive/bin/hive的执行程序,或者输入 hive –service cli
用于linux平台命令行查询,查询语句基本跟MySQL查询语句类似
2、hive web界面的启动方式
hive –service hwi 用于通过浏览器来访问hive
如果lib目录下没有hive-hwi-{version}.war包,我们要自己打包
官网下载源码包(比如1.10版本)
解压
$ tar zxvf apache-hive-1.1.0.src.tar.gz
再进入 hwi 目录,打包 war 文件(注意命令末尾有一个点.)
#cd apache-hive-1.1.0-src/hwi#jar cvfM0 hive-hwi-1.1.0.war -C web/ .
打包完成后,有了我们需要的 war 文件,再复制到 $HIVE_HOME/lib 目录下
#cp hive-hwi-1.1.0.war /usr/local/hive-1.1.0/lib
另外我们还需要拷贝一个 Java 的 tools.jar 到 $HIVE_HOME/lib 目录下
cp /usr/local/jdk1.7.0_67/lib/tools.jar /usr/local/hive-1.1.0/lib
否则会出现类似于下面的错误(因为 JAVA_HOME 指到$JAVA_HOME/jre 下了,而其 lib下的 tools.jar 跟$JAVA_HOME/lib/tools.jar 不一样,编译的时候需要用到后者)
最后,我们将 hive-site.xml 文件修改为
<property> <name>hive.hwi.listen.host</name> <value>0.0.0.0</value> <description>监听的地址</description> </property> <property> <name>hive.hwi.listen.port</name> <value>9999</value> <description>监听的端口号</description> </property> <property> <name>hive.hwi.war.file</name> <value>lib/hive-hwi-1.1.0.war</value> <description>war包所在的地址,注意这里不支持绝对路径,坑!</description> </property>
启动 hwi
在 $HIVE_HOME/bin 目录下,启动 hwi(由于我们之前已经修改了 Derby 为 MySQL 数据库,所以在启动 hwi 之前,请确保 MySQL 和 Hadoop 已经成功启动):
nohup bin/hive --service hwi > /dev/null 2> /dev/null &
web访问
我们可以在浏览器中打开网络接口的地址:localhost:9999/hwi, 启动成功
3、jdbc远程连接hiveserver2
在之前的学习和实践Hive中,使用的都是CLI或者hive –e的方式,该方式仅允许使用HiveQL执行查询、更新等操作,并且该方式比较笨拙单一。幸好Hive提供了轻客户端的实现,通过HiveServer或者HiveServer2,客户端可以在不启动CLI的情况下对Hive中的数据进行操作,两者都允许远程客户端使用多种编程语言如Java、Python向Hive提交请求,取回结果。HiveServer或者HiveServer2都是基于Thrift的,但HiveSever有时被称为Thrift
server,而HiveServer2却不会。既然已经存在HiveServer为什么还需要HiveServer2呢?这是因为HiveServer不能处理多于一个客户端的并发请求,这是由于HiveServer使用的Thrift接口所导致的限制,不能通过修改HiveServer的代码修正。因此在Hive-0.11.0版本中重写了HiveServer代码得到了HiveServer2,进而解决了该问题。HiveServer2支持多客户端的并发和认证,为开放API客户端如JDBC、ODBC提供了更好的支持。
配置
<property> <name>hive.metastore.warehouse.dir</name> <value>/usr/hive/warehouse</value> //(hive中的数据库和表在HDFS中存放的文件夹的位置) <description>location of default database for the warehouse</description></property><property> <name>hive.server2.thrift.port</name> <value>10000</value> //(HiveServer2远程连接的端口,默认为10000) <description>Port number of HiveServer2 Thrift interface. Can be overridden by setting $HIVE_SERVER2_THRIFT_PORT</description></property> <property> <name>hive.server2.thrift.bind.host</name> <value>**.**.**.**</value> //(hive所在集群的IP地址) <description>Bind host on which to run the HiveServer2 Thrift interface. Can be overridden by setting $HIVE_SERVER2_THRIFT_BIND_HOST</description></property><property> <name>hive.server2.long.polling.timeout</name> <value>5000</value> // (默认为5000L,此处修改为5000,不然程序会报错) <description>Time in milliseconds that HiveServer2 will wait, before responding to asynchronous calls that use long polling</description></property><property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true</value> //(Hive的元数据库,我采用的是本地Mysql作为元数据库) <description>JDBC connect string for a JDBC metastore</description></property> <property> <name>javax.jdo.option.ConnectionDriverName</name> //(连接元数据的驱动名) <value>com.mysql.jdbc.Driver</value> <description>Driver class name for a JDBC metastore</description></property><property> <name>javax.jdo.option.ConnectionUserName</name> //(连接元数据库用户名) <value>hive</value> <description>username to use against metastore database</description></property> <property> <name>javax.jdo.option.ConnectionPassword</name> // (连接元数据库密码) <value>hive</value> <description>password to use against metastore database</description></property>
先启动元数据库,在命令行中键入:hive --service metastore &
接下来开启hiveserver2服务:
在命令行中键入:hive --service hiveserver2 &
注意查看日志是否报错。
javaapi操作hive实例
package com.berg.hive.test1.api;import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.apache.log4j.Logger; /** * Hive的JavaApi * * 启动hive的远程服务接口命令行执行:hive --service hiveserver & * * @author 汤高 * */ public class HiveJdbcCli { //网上写 org.apache.hadoop.hive.jdbc.HiveDriver ,新版本不能这样写 private static String driverName = "org.apache.hive.jdbc.HiveDriver"; //这里是hive2,网上其他人都写hive,在高版本中会报错 private static String url = "jdbc:hive2://master:10000/default"; private static String user = "hive"; private static String password = "hive"; private static String sql = ""; private static ResultSet res; private static final Logger log = Logger.getLogger(HiveJdbcCli.class); public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { conn = getConn(); stmt = conn.createStatement(); // 第一步:存在就先删除 String tableName = dropTable(stmt); // 第二步:不存在就创建 createTable(stmt, tableName); // 第三步:查看创建的表 showTables(stmt, tableName); // 执行describe table操作 describeTables(stmt, tableName); // 执行load data into table操作 loadData(stmt, tableName); // 执行 select * query 操作 selectData(stmt, tableName); // 执行 regular hive query 统计操作 countData(stmt, tableName); } catch (ClassNotFoundException e) { e.printStackTrace(); log.error(driverName + " not found!", e); System.exit(1); } catch (SQLException e) { e.printStackTrace(); log.error("Connection error!", e); System.exit(1); } finally { try { if (conn != null) { conn.close(); conn = null; } if (stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e) { e.printStackTrace(); } } } private static void countData(Statement stmt, String tableName) throws SQLException { sql = "select count(1) from " + tableName; System.out.println("Running:" + sql); res = stmt.executeQuery(sql); System.out.println("执行“regular hive query”运行结果:"); while (res.next()) { System.out.println("count ------>" + res.getString(1)); } } private static void selectData(Statement stmt, String tableName) throws SQLException { sql = "select * from " + tableName; System.out.println("Running:" + sql); res = stmt.executeQuery(sql); System.out.println("执行 select * query 运行结果:"); while (res.next()) { System.out.println(res.getInt(1) + "\t" + res.getString(2)); } } private static void loadData(Statement stmt, String tableName) throws SQLException { //目录 ,我的是hive安装的机子的虚拟机的home目录下 String filepath = "user.txt"; sql = "load data local inpath '" + filepath + "' into table " + tableName; System.out.println("Running:" + sql); stmt.execute(sql); } private static void describeTables(Statement stmt, String tableName) throws SQLException { sql = "describe " + tableName; System.out.println("Running:" + sql); res = stmt.executeQuery(sql); System.out.println("执行 describe table 运行结果:"); while (res.next()) { System.out.println(res.getString(1) + "\t" + res.getString(2)); } } private static void showTables(Statement stmt, String tableName) throws SQLException { sql = "show tables '" + tableName + "'"; System.out.println("Running:" + sql); res = stmt.executeQuery(sql); System.out.println("执行 show tables 运行结果:"); if (res.next()) { System.out.println(res.getString(1)); } } private static void createTable(Statement stmt, String tableName) throws SQLException { sql = "create table " + tableName + " (key int, value string) row format delimited fields terminated by '\t'"; stmt.execute(sql); } private static String dropTable(Statement stmt) throws SQLException { // 创建的表名 String tableName = "testHive"; sql = "drop table " + tableName; stmt.execute(sql); return tableName; } private static Connection getConn() throws ClassNotFoundException, SQLException { Class.forName(driverName); Connection conn = DriverManager.getConnection(url, user, password); return conn; } }
- Hive之 hive的三种使用方式(CLI、HWI、Thrift)
- Hive的使用之hwi
- hive之hwi
- 使用HIVE的WEB界面:HWI
- 使用HIVE的WEB界面:HWI
- Hive HWI
- Hive用户接口(一)—Hive Web接口HWI的操作及使用
- Hive用户接口(一)—Hive Web接口HWI的操作及使用
- hive-hwi 浏览器界面使用
- hive管理之命令行方式CLI
- Hive学习之Hive CLI
- hive三种方式区别和搭建、HiveServer2环境搭建、HWI环境搭建和beeline环境搭建
- hive学习之hive的常用使用方式
- Hive 之CLI管理模式
- [一起学Hive]之十六-Hive的WEB页面接口-HWI
- [一起学Hive]之十六-Hive的WEB页面接口-HWI
- hive安装过程:metastore(元数据存储)的三种方式之本地derby方式
- hive安装过程:metastore(元数据存储)的三种方式之本地mysql方式
- PullToRefreshLayout
- java程序员的大数据之路(5):HDFS压缩与解压缩
- Oracle 存储过程
- HashSet的储存方式以及内存泄露的原因
- android的m、mm、mmm编译命令的使用
- Hive之 hive的三种使用方式(CLI、HWI、Thrift)
- Visualizing what ConvNets learn
- web.xml 配置中classpath: 与classpath*:的区别
- Struts2获得表单数据
- poj_2406_kmp_入门_精华操作_解释
- 数据结构上机2
- Bootstrap 栅格系统分析
- The prefix "tx" for element "tx:annotation-driven" is not bound. ssm配置事物切面报错
- 常用依赖