hiveapi通过Java程序调用

基于hadoop的Hive数据仓库JavaAPI简单调用的实例,关于Hive的简介在此不赘述。hive提供了三种用户接口:CLI,JDBC/ODBC和 WebUI

    CLI,即Shell命令行JDBC/ODBC 是 Hive 的Java,与使用传统数据库JDBC的方式类似WebGUI是通过浏览器访问 Hive

本文主要介绍的就是第二种用户接口,直接进入正题。

1、Hive 安装:

2)测试数据data文件’\t’分隔:

1 zhangsan

2 lisi

3 wangwu

3)将测试数据data上传到linux目录下,我放置在:/home/hadoop01/data

2、在使用 JDBC 开发 Hive 程序时, 必须首先开启 Hive 的远程服务接口。使用下面命令进行开启:

Java代码

    hive–servicehiveserver>/dev/null2>/dev/null&

3、测试代码:

Java代码

    importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.Statement;importorg.apache.log4j.Logger;/***Hive的JavaApi**启动hive的远程服务接口命令行执行:hive–servicehiveserver>/dev/null2>/dev/null&**@author吖大哥**/publicclassHiveJdbcCli{privatestaticStringdriverName="org.apache.hadoop.hive.jdbc.HiveDriver";privatestaticStringurl="jdbc:hive://hadoop3:10000/default";privatestaticStringuser="hive";privatestaticStringpassword="mysql";privatestaticStringsql="";privatestaticResultSetres;privatestaticfinalLoggerlog=Logger.getLogger(HiveJdbcCli.class);publicstaticvoidmain(String[]args){Connectionconn=null;Statementstmt=null;try{conn=getConn();stmt=conn.createStatement();//第一步:存在就先删除StringtableName=dropTable(stmt);//第二步:不存在就创建createTable(stmt,tableName);//第三步:查看创建的表showTables(stmt,tableName);//执行describetable操作describeTables(stmt,tableName);//执行loaddataintotable操作loadData(stmt,tableName);//执行select*query操作selectData(stmt,tableName);//执行regularhivequery统计操作countData(stmt,tableName);}catch(ClassNotFoundExceptione){e.printStackTrace();log.error(driverName+"notfound!",e);System.exit(1);}catch(SQLExceptione){e.printStackTrace();log.error("Connectionerror!",e);System.exit(1);}finally{try{if(conn!=null){conn.close();conn=null;}if(stmt!=null){stmt.close();stmt=null;}}catch(SQLExceptione){e.printStackTrace();}}}privatestaticvoidcountData(Statementstmt,StringtableName)throwsSQLException{sql="selectcount(1)from"+tableName;System.out.println("Running:"+sql);res=stmt.executeQuery(sql);System.out.println("执行“regularhivequery”运行结果:");while(res.next()){System.out.println("count——>"+res.getString(1));}}privatestaticvoidselectData(Statementstmt,StringtableName)throwsSQLException{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));}}privatestaticvoidloadData(Statementstmt,StringtableName)throwsSQLException{Stringfilepath="/home/hadoop01/data";sql="loaddatalocalinpath’"+filepath+"’intotable"+tableName;System.out.println("Running:"+sql);res=stmt.executeQuery(sql);}privatestaticvoiddescribeTables(Statementstmt,StringtableName)throwsSQLException{sql="describe"+tableName;System.out.println("Running:"+sql);res=stmt.executeQuery(sql);System.out.println("执行describetable运行结果:");while(res.next()){System.out.println(res.getString(1)+"\t"+res.getString(2));}}privatestaticvoidshowTables(Statementstmt,StringtableName)throwsSQLException{sql="showtables’"+tableName+"’";System.out.println("Running:"+sql);res=stmt.executeQuery(sql);System.out.println("执行showtables运行结果:");if(res.next()){System.out.println(res.getString(1));}}privatestaticvoidcreateTable(Statementstmt,StringtableName)throwsSQLException{sql="createtable"+tableName+"(keyint,valuestring)rowformatdelimitedfieldsterminatedby’\t’";stmt.executeQuery(sql);}privatestaticStringdropTable(Statementstmt)throwsSQLException{//创建的表名StringtableName="testHive";sql="droptable"+tableName;stmt.executeQuery(sql);returntableName;}privatestaticConnectiongetConn()throwsClassNotFoundException,SQLException{Class.forName(driverName);Connectionconn=DriverManager.getConnection(url,user,password);returnconn;}}

4、测试结果

5、终端查询结果:

hive> select * from testHive;

OK

1 zhangsan

2 lisi

3 wangwu

Time taken: 11.232 seconds

这几年大多是昆明空运来的,

hiveapi通过Java程序调用

相关文章:

你感兴趣的文章:

标签云: