HIVE简单API使用
2016-05-27 20:27
197 查看
近期在学习HIVE,发现网上的代码都是很早以前的版本,我是使用1.1.1版本的hive。根本没法用,也有很多冲突。不过查阅官网,分析包结构,最后还是弄完了。直接把代码帖上来吧。
package com.yc.hive; 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; public class HiveUtil { //网上写 org.apache.hadoop.hive.jdbc.HiveDriver ,新版本不能这样写 private static String driverName = "org.apache.hive.jdbc.HiveDriver"; private static Connection con; //这里是hive2,网上其他人都写hive,在高版本中会报错 private static String url = "jdbc:hive2://master:10000/default"; private static String user = "hive"; private static String password = "hive"; private static final Logger log = Logger.getLogger(HiveUtil.class); private static Connection getCon(){ Connection conn = null; try { Class.forName(driverName); conn = DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } public static ResultSet selectData(String sql,Statement stmt){ ResultSet res=null; try { res=stmt.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return res; } public static void loadData(Statement stmt, String sql) throws SQLException { //目录 ,我的是hive安装的机子的虚拟机的home目录下 System.out.println("Running:" + sql); stmt.execute(sql); } public static void createTable(Statement stmt,String sql) throws SQLException{ stmt.execute(sql); } private static ResultSet showTables(Statement stmt, String tableName) throws SQLException { String sql = "show tables '" + tableName + "'"; System.out.println("Running:" + sql); ResultSet res = stmt.executeQuery(sql); System.out.println("执行 show tables 运行结果:"); return res; } private static ResultSet describeTables(Statement stmt, String tableName) throws SQLException { String sql = "describe " + tableName; ResultSet res = stmt.executeQuery(sql); return res; } private static ResultSet selectAll(Statement stmt, String tableName) throws SQLException { String sql = "select * from " + tableName; ResultSet res = stmt.executeQuery(sql); return res; } private static void dropTable(Statement stmt,String tableName) throws SQLException { // 创建的表名 String sql = "drop table " + tableName; stmt.execute(sql); } public static void main(String[] args) { try { String sql="create table newtable (key int, value string) row format delimited fields terminated by '\t'"; con=getCon(); Statement stmt=con.createStatement(); if(!showTables(stmt,"newtable").next()){ createTable(stmt, sql); } String sql2="load data local inpath '1' into table newtable"; loadData(stmt, sql2); ResultSet res=selectAll(stmt,"newtable"); while(res.next()){ System.out.println(res.getString(1)+"\t"+res.getString(2)); } } catch (SQLException e) { e.printStackTrace(); } } }
相关文章推荐
- Android Manifest 用法
- 什么是 GraphQL?
- Spark RDD API详解(一) Map和Reduce
- Spring Boot 开发微服务
- 分享Hive的一份胶片资料
- lwn拾遗:[sn3218 led drivers]-api解释-1
- 页面元素查找之Selectors API
- Hadoop生态上几个技术的关系与区别:hive、pig、hbase 关系与区别
- 一个小型js框架myJSFrame附API使用帮助
- 详细分析交换机、路由器、集线器的区别和联系
- PowerShell打开或关闭光驱
- 批处理的api WMIC学习体会有感第1/2页
- 批处理 API实现文件下载的代码第1/2页
- Lua教程(十七):C API简介
- 强制删除工具 xdelbox xdelbox1.5正式版下载
- 揪出交换机端口背后“凶手”导致网速太慢
- 电脑重启后突然检测不到硬盘的原因分析与解决办法
- C#中设计、使用Fluent API
- Google官方支持的NodeJS访问API,提供后台登录授权
- PQ分区出错! 巧用Ghost急速补救的绝妙办法