hive学习001 hive的使用,
概念
hive是一个数据仓库(数据库与数据仓库概念我也不是很清楚),不过他可以建很多数据库,很多数据库下又会有很多的表。提供了一种HIVE QL的查询语言。
结构
(图选自hive 用户指导1.0,版本不同会有差异http://wenku.baidu.com/link?url=Oe4fznW4mkt5yS418-oPSFwGFEJYrco9NkGH-2LBMsdSztQ_jloUg6xKcGD3_pXewnNTMUA6X-UWAuIg8HioNu8kSwjyAnQRapUoCkz5vki点击打开链接)
使用
HIVE提供了dbc、webgui、cli这样的接口:
1.CLI的使用
使用的是Bitvise SSH Client的Linux远程桌面程序:步骤如图示
WEB-GUI使用
JDBC方式使用
java程序中绑定hive数据库(类似于mysql、oracle数据库一类),wiki地址:https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients#HiveServer2Clients-JDBC点击打开链接
importjava.sql.SQLException;importjava.sql.Connection;importjava.sql.ResultSet;importjava.sql.Statement;importjava.sql.DriverManager;
publicclassHiveJdbcClient {privatestaticString driverName ="org.apache.hive.jdbc.HiveDriver";/*** @param args* @throws SQLException*/publicstaticvoidmain(String[] args)throwsSQLException {try{Class.forName(driverName);}catch(ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();System.exit(1);}//replace "hive" here with the name of the user the queries should run asConnection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default","hive","");//获取数据源Statement stmt = con.createStatement();String tableName ="testHiveDriverTable";stmt.execute("drop table if exists "+ tableName);stmt.execute("create table "+ tableName +" (key int, value string)");// show tablesString sql ="show tables '"+ tableName +"'";System.out.println("Running: "+ sql);ResultSet res = stmt.executeQuery(sql);if(res.next()) {System.out.println(res.getString(1));}// describe tablesql ="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 into table// NOTE: filepath has to be local to the hive server// NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per lineString filepath ="/tmp/a.txt";sql ="load data local inpath '"+ filepath +"' into table "+ tableName;System.out.println("Running: "+ sql);stmt.execute(sql);// select * querysql ="select * from "+ tableName;System.out.println("Running: "+ sql);res = stmt.executeQuery(sql);while(res.next()) {System.out.println(String.valueOf(res.getInt(1)) +"\t"+ res.getString(2));}// regular hive querysql ="select count(1) from "+ tableName;System.out.println("Running: "+ sql);res = stmt.executeQuery(sql);while(res.next()) {System.out.println(res.getString(1));}}}
接下来一篇就是SQL方言各类特性
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。