基于hadoop的Hive数据仓库JavaAPI简单调用的实例,关于Hive的简介在此不赘述。hive提供了三种用户接口:CLI,JDBC/ODBC和 WebUI
- CLI,即Shell命令行
- JDBC/ODBC 是 Hive 的Java,与使用传统数据库JDBC的方式类似
- WebGUI是通过浏览器访问 Hive
本文主要介绍的就是第二种用户接口,直接进入正题。
1、Hive 安装:
1)hive的安装请参考网上的相关文章,测试时只在hadoop一个节点上安装hive即可。
2)测试数据data文件'\t'分隔:
1 zhangsan
2 lisi
3 wangwu
3)将测试数据data上传到linux目录下,我放置在:/home/hadoop01/data
2、在使用 JDBC 开发 Hive 程序时, 必须首先开启 Hive 的远程服务接口。使用下面命令进行开启:
hive --service hiveserver >/dev/null 2>/dev/null &
3、测试代码:
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 >/dev/null 2>/dev/null & * * @author 吖大哥 * */ public class HiveJdbcCli { private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver"; private static String url = "jdbc:hive://hadoop3:10000/default"; private static String user = "hive"; private static String password = "mysql"; 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 { String filepath = "/home/hadoop01/data"; sql = "load data local inpath '" + filepath + "' into table " + tableName; System.out.println("Running:" + sql); res = stmt.executeQuery(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.executeQuery(sql); } private static String dropTable(Statement stmt) throws SQLException { // 创建的表名 String tableName = "testHive"; sql = "drop table " + tableName; stmt.executeQuery(sql); return tableName; } private static Connection getConn() throws ClassNotFoundException, SQLException { Class.forName(driverName); Connection conn = DriverManager.getConnection(url, user, password); return conn; } }
4、测试结果
5、终端查询结果:
hive> select * from testHive;
OK
1 zhangsan
2 lisi
3 wangwu
Time taken: 11.232 seconds
hive>
相关推荐
同样hive metastore也提供了java接口。 使用 import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.metastore.HiveMetaStoreClient; import org.apache.hadoop.hive.metastore.api.Field...
hive java开发驱动包列表hive-common-2.3.4.jarhive-exec-2.3.4.jarhive-jdbc-2.3.4.jarhive-llap-client-2.3.4.jarhive-llap-common-2.3.4.jarhive-llap-server-2.3.4.jarhive-llap-tez-2.3.4.jarhive-metastore-...
HiveAPI 用Java编写的Hive Rest API
基于hadoop的Hive数据仓库JavaAPI简单调用的实例,关于Hive的简介在此不赘述。hive提供了三种用户接口:CLI,JDBC/ODBC和 WebUI CLI,即Shell命令行 JDBC/ODBC 是 Hive 的Java,与使用传统数据库JDBC的方式类似 Web...
内网开发的,不使用maven做项目管理的,直接拿来用,包括了所有需要的jar包(如基础包httpclient),hive是1.2版本的
2019-8____2019-10工作期间的java版hiveApi,hbaseApi和spark版本的hiveApi,hbaseApi
基于hadoop的,java实现hive快速导数据到Hbase解决方案。
jdbc连接hive代码所需要jar包,测试有效
java运行依赖jar包
压缩包中包含Java通过JDBC连接Hive所需的jar包。包含有,antlr-runtime-3.0.1.jar;commons-logging-1.0.4.jar;hadoop-core-1.2.1.jar;hive-exec-0.10.0.jar;hive-jdbc-0.13.1.jar;hive-metastore-0.9.0.jar;hive-...
antlr-runtime-3.0.1.jar hive-exec-0.7.1.jar hive-jdbc-0.7.1.jar hive-metastore-0.9.0.jar hive-service-0.9.0.jar jdo2-api-2.3-ec.jar libfb303.jar
在hive java api的时候 : 需要导入hive运行所需的jar包,导入jar包有两种方式可实现 1.使用maven依赖直接修改配置文件,以下为配置内容:(不推荐,会下载额外很多无用包,而且时间很长在1小时左右) 2.挑选必须...
标签:apache、flink、connector、hive、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...
###这是一个学习hive的库 PS:Hive版本为0.10.0-cdh4.3.2 ###Tips 1.使用jdbc方式连接hiveserver,只能使用默认的数据库即default。官方文档说明如下: "jdbc:hive://localhost:10000/default". Currently, the only...
标签:flink、11、apache、connector、hive_2、jar包、java、API文档、中文版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,...
标签:apache、flink、connector、hive、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明...
hive常用内置函数使用文档,类似于java的api doc文档
标签:apache、flink、connector、hive、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...
Hadoop——HDFS的Java API操作 4. Hadoop——分布式计算框架MapReduce 5. Hadoop——MapReduce案例 6. Hadoop——资源调度器YARN 7. Hadoop——Hadoop数据压缩 二、Zookeeper 1.Zookeeper——...
2019-08----2019-10工作期间所用到的hive和HBASE的jar包