使用JAVA操作HIVE
2015-10-06 11:46
531 查看
一、 首先,在机器上打开hiveservice
打开50000端口,然后java就可以使用java连了,需要的jar包我发个图片
二、编写代码
三、JAVA操作HIVE表
hive --service hiveserver -p 50000 &
打开50000端口,然后java就可以使用java连了,需要的jar包我发个图片
二、编写代码
package hive; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class HiveJavaOperate { public static void main(String[] args) throws Exception{ Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver"); Connection con = DriverManager.getConnection( "jdbc:hive://crxy90:50000/default", "", ""); Statement stmt = con.createStatement(); String querySQL = "SELECT * FROM t1"; ResultSet res = stmt.executeQuery(querySQL); while (res.next()) { System.out.println(res.getInt(1) + "\t" + res.getString(2)); } } }
三、JAVA操作HIVE表
package 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; /** * Hive的JavaApi * * 启动hive的远程服务接口命令行执行:hive --service hiveserver >/dev/null 2>/dev/null & * * @author 吖大哥 * */ public class HiveJdbcCli { private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver"; private static String url = "jdbc:hive://192.168.215.90:50000/default"; private static String user = ""; private static String password = ""; private static String sql = ""; private static ResultSet res; private static final Logger log = Logger.getLogger(HiveJdbcCli.class); public static void main(String[] args) { Connection conn = null; Statement stmt = null; try { conn = getConn(); stmt = conn.createStatement(); // 第一步:存在就先删除 String tableName = dropTable(stmt); // 第二步:不存在就创建 createTable(stmt, tableName); // 第三步:查看创建的表 showTables(stmt, tableName); // 执行describe table操作 describeTables(stmt, tableName); // 执行load data into table操作 loadData(stmt, tableName); // 执行 select * query 操作 selectData(stmt, tableName); // 执行 regular hive query 统计操作 countData(stmt, tableName); } catch (ClassNotFoundException e) { e.printStackTrace(); log.error(driverName + " not found!", e); System.exit(1); } catch (SQLException e) { e.printStackTrace(); log.error("Connection error!", e); System.exit(1); } finally { try { if (conn != null) { conn.close(); conn = null; } if (stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e) { e.printStackTrace(); } } } private static void countData(Statement stmt, String tableName) throws SQLException { sql = "select count(1) from " + tableName; System.out.println("Running:" + sql); res = stmt.executeQuery(sql); System.out.println("执行“regular hive query”运行结果:"); while (res.next()) { System.out.println("count ------>" + res.getString(1)); } } private static void selectData(Statement stmt, String tableName) throws SQLException { 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)); } } private static void loadData(Statement stmt, String tableName) throws SQLException { String filepath = "/usr/local/hive/data/t1"; //磁盘上的数据 sql = "load data local inpath '" + filepath + "' into table " + tableName; System.out.println("Running:" + sql); res = stmt.executeQuery(sql); } private static void describeTables(Statement stmt, String tableName) throws SQLException { sql = "describe " + tableName; System.out.println("Running:" + sql); res = stmt.executeQuery(sql); System.out.println("执行 describe table 运行结果:"); while (res.next()) { System.out.println(res.getString(1) + "\t" + res.getString(2)); } } private static void showTables(Statement stmt, String tableName) throws SQLException { sql = "show tables '" + tableName + "'"; System.out.println("Running:" + sql); res = stmt.executeQuery(sql); System.out.println("执行 show tables 运行结果:"); if (res.next()) { System.out.println(res.getString(1)); } } private static void createTable(Statement stmt, String tableName) throws SQLException { sql = "create table " + tableName + " (key int, value string) row format delimited fields terminated by '\t'"; stmt.executeQuery(sql); } private static String dropTable(Statement stmt) throws SQLException { // 创建的表名 String tableName = "testHive"; sql = "drop table " + tableName; stmt.executeQuery(sql); return tableName; } private static Connection getConn() throws ClassNotFoundException, SQLException { Class.forName(driverName); Connection conn = DriverManager.getConnection(url, user, password); return conn; } }
相关文章推荐
- Java 读取大文件
- PhoneGap 上传图片HTML和服务器端端实现(JAVA)
- Java多线程18:线程池
- Eclipse web 项目转到myEclipse无法导出WAR包
- java自带线程池和队列详细讲解
- java拾遗之关键字static
- java中的构造器
- spring 源码如何导入到eclipse
- java中的堆、栈、常量池
- Spring整合CXF发布Web Services
- spring mvc 环境搭建
- JAVA容器类解析
- ubuntu12.04 安装配置jdk1.7
- 矩阵相关运算的java实现
- leetcode58题---java解法
- java中对集合或数组进行排序有两种方法:Comparable 与 Comparator
- 红黑树的插入删除完整版以及java版本
- eclipse出现Call requires API level11和layout cannot be resolved
- Spring事务传播
- Spring标签:context:component-scan