11.5.2 程序实例
在使用JDBC链接Hive之前,首先需要开启Hive监听用户的链接。开启Hive服务的方法如下所示:
hive—service hiveservice
Service hiveservice not found
Available Services:cli help hiveserver hwi jar lineage metastore rcfilecat
hadoop@master:~/hadoop-1.0.1/hive-0.8.1/bin/ext$hive—service hiveserver
Starting Hive Thrift Server
Hive history file=/tmp/hadoop/hive_job_log_hadoop_201205150632_559026727.txt
下面是一个使用Java编写的JDBC客户端访问的代码样例:
package cn.edu.rnc.cloudcomputing.book.chapter11;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveJdbcClient{
/**
*@param args
*@throws SQLException
*/
public static void main(String[]args)throws SQLException{
//注册JDBC驱动
try{
Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");
}catch(ClassNotFoundException e){
//TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
//创建连接
Connection con=DriverManager.getConnection("jdbc:hive://
master:10000/default","","");
//statement用来执行SQL语句
Statement stmt=con.createStatement();
//下面为Hive测试语句
String tableName="u1_data";
stmt.executeQuery("drop table"+tableName);
ResultSet res=stmt.executeQuery("create table"+tableName+"
(userid int,"+
"movieid int,"+
"rating int,"+
"city string,"+
"viewTime string)"+
"row format delimited"+
"fields terminated by'\t'"+
"stored as textfile");//创建表
//show tables语句
String sql="show tables";
System.out.println("Running:"+sql+":");
res=stmt.executeQuery(sql);
if(res.next()){
System.out.println(res.getString(1));
}
//describe table语句
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));
}
//load data语句
String filepath="/home/hadoop/Downloads/u.data.new";
sql="load data local inpath'"+filepath+"'overwrite into table
"+tableName;
System.out.println("Running:"+sql);
res=stmt.executeQuery(sql);
//select query:选取前5条记录
sql="select*from"+tableName+"limit 5";
System.out.println("Running:"+sql);
res=stmt.executeQuery(sql);
while(res.next()){
System.out.println(String.valueOf(res.getString(3)+"\t"+
res.getString(4)));
}
//hive query:统计记录个数
sql="select count(*)from"+tableName;
System.out.println("Running:"+sql);
res=stmt.executeQuery(sql);
while(res.next()){
System.out.println(res.getString(1));
}
}
}
从上述代码可以看出,在进行查询操作之前需要做如下工作:
1)通过Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");语句注册Hive驱动;
2)通过Connection con=DriverManager.getConnection("jdbc:hive://master:10000/default","","");语句建立与Hive数据库的连接。
在上述操作完成之后便可以正常进行操作了。上述操作结果为:
Running:show tables:
page_view
testhivedrivertable
u1_data
u2_data
u3_data
Running:describe u1_data
userid int
movieid int
rating int
city string
viewtime string
Running:load data local inpath'/home/hadoop/Downloads/u.data.new'overwrite into
table u1_data
Running:select*from u1_data limit 10
3北京
3北京
1石家庄
2石家庄
1苏州
Running:select count(*)from u1_data
100000
当前的JDBC接口只支持查询的执行及结果的获取,并且支持部分元数据的读取。Hive支持的接口除了JDBC外,还有Python、PHP、ODBC等。读者可以访问http://wiki.apache.org/hadoop/Hive/HiveClient#JDBC查看相关信息。