(1)hive 三种启动方式及用途,本文主要关注通过hiveserver(可jdbc连接)的方式启动
1, hive 命令行模式,直接输入/hive/bin/hive的执行程序,或者输入 hive --service cli
用于linux平台命令行查询,查询语句基本跟mysql查询语句类似
2, hive web界面的启动方式,hive --service hwi
用于通过浏览器来访问hive,提供基本的基于web的hive查询服务,可以看作是hive数据平台的demo,
具体用法可见:http://www.cnblogs.com/gpcuster/archive/2010/02/25/1673480.html 使用HIVE的WEB界面:HWI
3, hive 远程服务 (端口号10000) 启动方式,nohup ./hive --service hiveserver >/dev/null 2>/dev/null &
用java等程序实现通过jdbc等驱动的方式访问hive就用这种起动方式了,这个是程序员最需要的方式了。
开源工具phphiveadmin就采用的这种方式,这种方式其实启动了一个 Hive Thrift Server ,允许你使用任意语言
与hive server通信,所以如果你不会java,语言将不会成为问题。
(2)给出一个基于hiveserver的demo,这个demo可以扩展成一个基于web操作hive的离线分析工具,类似phphiveadmin。
1、demo部署路径如下:
注:开发环境:myeclipse 8.5, tomcat 6.0
2、code:
2.1 HiveTestCase.java
01 | import java.sql.Connection; |
02 | import java.sql.DriverManager; |
03 | import java.sql.ResultSet; |
04 | import java.sql.SQLException; |
05 | import java.sql.Statement; |
06 |
07 | public class HiveTestCase { |
08 | public static void main(String[] args) throws Exception { |
09 | String querySQL = "SELECT a.name, a.id, a.sex FROM com58 a where a.id > 100 and a.id < 110 and sex='male'" ; |
10 | hive2Txt(querySQL); |
11 | } |
12 |
13 | private static void hive2Txt(String querySQL) |
14 | throws ClassNotFoundException, SQLException { |
15 | Class.forName( "org.apache.hadoop.hive.jdbc.HiveDriver" ); |
16 |
17 | // String dropSQL = "drop table com58"; |
18 | // 1 male 29 3d649ecc 3d649ecc@qq.com 20110304 20110402 |
19 | // 1.id 2.sex 3.age 4.name 5.mail 6.sDate 7.eDate |
20 | // String createSQL = |
21 | // "create table com58 (id int, sex string, age int, name string, mail string, sDate bigint, eDate bigint) row format delimited fields terminated by ' '"; |
22 | // hive插入数据支持两种方式一种:load文件,令一种是 CTAS(create table as select... |
23 | // 从另一个表中查询进行插入) |
24 | // hive是不支持insert into...values(....)这种操作的 |
25 | // String insterSQL = |
26 | // "LOAD DATA LOCAL INPATH '/home/june/hadoop/hadoop-0.20.203.0/tmp/1.txt' OVERWRITE INTO TABLE com58"; |
27 |
28 | Connection con = DriverManager.getConnection( |
29 | "jdbc:hive://localhost:10000/default" , "" , "" ); |
30 | Statement stmt = con.createStatement(); |
31 | // stmt.executeQuery(dropSQL); // 执行删除语句 |
32 | // stmt.executeQuery(createSQL); // 执行建表语句 |
33 | // stmt.executeQuery(insterSQL); // 执行插入语句 |
34 | ResultSet res = stmt.executeQuery(querySQL); // 执行查询语句 |
35 |
36 | while (res.next()) { |
37 | System.out.println( "name:\t" + res.getString( 1 ) + "\tid:\t" |
38 | + res.getString( 2 ) + "\tsex:\t" + res.getString( 3 )); |
39 | } |
40 | } |
41 | } |
2.2 hiveSelect.jsp
01 | <%@ page language= "java" contentType= "text/html; charset=UTF-8" |
02 | pageEncoding= "UTF-8" %> |
03 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd" > |
04 | <% @page import = "java.util.*" %> |
05 | <% @page |
06 | import ="java.io.File, |
07 | java.io.FileWriter, |
08 | java.io.IOException, |
09 | java.sql.Connection, |
10 | java.sql.DriverManager, |
11 | java.sql.ResultSet, |
12 | java.sql.SQLException, |
13 | java.sql.Statement"%> |
14 |
15 |
16 | <% |
17 | Class.forName( "org.apache.hadoop.hive.jdbc.HiveDriver" ); |
18 | String table = request.getParameter( "table" ); |
19 | String querySQL = "select * from " + table + " limit 10" ; |
20 | Connection con = DriverManager.getConnection( |
21 | "jdbc:hive://localhost:10000/default" , "" , "" ); |
22 | Statement stmt = con.createStatement(); |
23 | // stmt.executeQuery(dropSQL); // 执行删除语句 |
24 | // stmt.executeQuery(createSQL); // 执行建表语句 |
25 | // stmt.executeQuery(insterSQL); // 执行插入语句 |
26 | ResultSet res = stmt.executeQuery(querySQL); // 执行查询语句 |
27 |
28 | FileWriter fw = new FileWriter( "/home/june/a.txt" ); |
29 | while (res.next()) { |
30 | System.out.println( "name:\t" + res.getString( 1 ) + "\tid:\t" |
31 | + res.getString( 2 ) + "\tsex:\t" + res.getString( 3 )); |
32 | fw.write( "name:\t" + res.getString( 1 ) + "\tid:\t" |
33 | + res.getString( 2 ) + "\tsex:\t" + res.getString( 3 ) |
34 | + "\n" ); |
35 | } |
36 | fw.flush(); |
37 | fw.close(); |
38 | %> |
39 |
40 | <html> |
41 | <head> |
42 | <meta http-equiv= "Content-Type" content= "text/html; charset=UTF-8" > |
43 | <title>Insert title here</title> |
44 | </head> |
45 | <body> |
46 |
47 | </body> |
48 | </html> |
01 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
02 | < html > |
03 | < head > |
04 | < meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" > |
05 | < title >Insert title here</ title > |
06 | </ head > |
07 | < html > |
08 | < head > |
09 | < title >Hive Web Interface-Create a Hive Session</ title > |
10 | </ head > |
11 | < body > |
12 | < table > |
13 | < tr > |
14 |
15 | < td valign = "top" > |
16 | < h2 > |
17 | select hive table to file. |
18 | </ h2 > |
19 | < form action = "hiveSelect.jsp" > |
20 | < table border = "1" > |
21 | < tr > |
22 | < td > |
23 | Session Name |
24 | </ td > |
25 | < td > |
26 | < input type = "text" name = "table" value = "table" > |
27 | </ td > |
28 | </ tr > |
29 | < tr > |
30 | < td colSpan = "2" > |
31 | < input type = "submit" > |
32 | </ td > |
33 | </ tr > |
34 | </ table > |
35 | </ form > |
36 | </ td > |
37 | </ tr > |
38 | </ table > |
39 | </ body > |
40 | </ html > |
41 |
42 | </ html > |
REF:
http://blog.youkuaiyun.com/a221133/article/details/6734762
http://blog.youkuaiyun.com/a221133/article/details/6734746
官方站点:
HiveClient