2.x的版本hive的api调用增加,删除,添加数据
2016-05-26 21:56
423 查看
hivesql语句其实sql差不多,在api调用时也简单用execute()或executeQuery()即可,不过在创建表时,注意指定数据的读入格式,否则就不能插入数据。
代码展示:
}
代码展示:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class HiveTestApi001 { private static String driverName = "org.apache.hive.jdbc.HiveDriver"; private static String url = "jdbc:hive2://master:10000/default"; private static String user = "hive"; private static String password = "hive"; public static void main(String[] args) { Connection conn = null; try { conn = getConn(); System.out.println( conn ); Statement stmt = conn.createStatement(); createtable();//创建hellohive表 selectalltable();//显示表 desctable(); //查看表结构 loadData(stmt,"hellohive");//向表中查入数据 selecttable(stmt,"hellohive");//查询表中的数据 } catch (Exception e) { e.printStackTrace(); } } //建立连接 private static Connection getConn() throws ClassNotFoundException,SQLException { Class.forName(driverName); Connection conn = DriverManager.getConnection(url, user, password); return conn; } //创建表以及删除表 private static void createtable(){ try { Connection conn =getConn(); Statement stmt = conn.createStatement(); String tableName = "hellohive"; stmt.execute("drop table if exists " + tableName); //stmt.execute("drop table if exists"+tableName);//如果表存在则将其删除 stmt.execute("create table "+tableName+" (key int, value string) row format delimited fields terminated by '\t'"); System.out.println("Create table success!"); } catch (Exception e) { e.printStackTrace(); } } //显示存在的表 private static void selectalltable(){ try { Connection conn =getConn(); Statement stmt = conn.createStatement(); String tableName = "hellohive"; String sql="show tables '"+tableName+"'"; ResultSet res=stmt.executeQuery(sql); if(res.next()){ System.out.println(res.getString(1)); } } catch (Exception e) { e.printStackTrace(); } } //查看表结构 private static void desctable(){ try { Connection conn =getConn(); Statement stmt = conn.createStatement(); String tableName = "hellohive"; String sql = "describe " + tableName; System.out.println("Running: " + sql); ResultSet res = stmt.executeQuery(sql); while (res.next()) { System.out.println(res.getString(1) + "\t" + res.getString(2)); } } catch (ClassNotFoundException | SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void loadData(Statement stmt, String tableName)throws SQLException { String filepath = "/home/hadoop/user.txt"; String sql = "load data local inpath '" + filepath + "' into table " + tableName; System.out.println("Running:" + sql); stmt.execute(sql); } private static void selecttable(Statement stmt, String tableName) throws SQLException{ String sql = "select * from " + tableName; ResultSet res = stmt.executeQuery(sql); while (res.next()) { System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2)); } }
}
相关文章推荐
- Android Manifest 用法
- 什么是 GraphQL?
- Spark RDD API详解(一) Map和Reduce
- Spring Boot 开发微服务
- 我是运营,我没有假期
- 分享Hive的一份胶片资料
- lwn拾遗:[sn3218 led drivers]-api解释-1
- 页面元素查找之Selectors API
- Hadoop生态上几个技术的关系与区别:hive、pig、hbase 关系与区别
- DB2数据库的安装
- 一个小型js框架myJSFrame附API使用帮助
- C#实现把指定数据写入串口
- “传奇”图象数据存储方式
- 详细分析交换机、路由器、集线器的区别和联系
- PowerShell打开或关闭光驱
- 修复mysql数据库
- 批处理的api WMIC学习体会有感第1/2页
- 批处理 API实现文件下载的代码第1/2页
- Lua教程(十七):C API简介
- 强制删除工具 xdelbox xdelbox1.5正式版下载