java如何操作hive
这篇文章将为大家详细讲解有关java如何操作hive,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
存储类:
package com.rxzx.hive;
public class Beacon {
private String stime;
private String uid;
private String sid;
private String pid;
public String getStime() {
return stime;
}
public void setStime(String stime) {
this.stime = stime;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return stime+" "+uid+" "+sid+" "+pid;
}
}
创建连接和关闭连接类:
package com.rxzx.hive;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class HiveJDBC {
private static Connection conn;
//初始化数据仓库连接对象
public static Connection getConnection() {
if(conn==null) {
try {
conn=DriverManager.getConnection("jdbc:hive2://192.168.2.100:10000/hive_db", "Teacher", "123456");
} catch (SQLException e) {
System.out.println("数据仓库连接失败");
e.printStackTrace();
}
}
return conn;
}
//关闭连接对象
public static void close() {
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
功能类:创建表,删除表,加载数据,数据展现到java窗口
package com.rxzx.hive;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiConsumer;
public class BeaconDao {
//删除表
public void dropTable(String tableName) throws SQLException{
Connection conn=HiveJDBC.getConnection();
Statement stat=conn.createStatement();
stat.execute("drop table if exists "+tableName);
}
//创建表
public void createTable(String tableName,List<String> columns,String partition,String rowFormat) throws SQLException {
Connection conn=HiveJDBC.getConnection();
Statement stat=conn.createStatement();
String sql="create table if not exists "+tableName+"(";
if(columns!=null && columns.size()>0) {
String columnsql="";
for(String c:columns) {
columnsql+=c+",";
}
if(columnsql.endsWith(",")) {
columnsql=columnsql.substring(0, columnsql.length()-1);
}
sql+=columnsql+") ";
if(partition!=null && !partition.equals("")) {
sql+=partition+" ";
}
if(rowFormat!=null && !rowFormat.equals("")) {
sql+=rowFormat+" ";
}
}else {
throw new SQLException("字段不能为空");
}
stat.execute(sql);
}
public static void load(boolean local,String path,boolean overwrite,String tablename,String partition) throws SQLException{
Connection conn=HiveJDBC.getConnection();
Statement stat=conn.createStatement();
String sql="load data "+(local?"local":"")+" inpath '"+path+"'"+(overwrite?"overwrite":"")+" into table "+tablename+" "+partition;
System.out.println(sql);
stat.execute(sql);
}
public static List<Beacon> getData(String sql) throws SQLException{
List<Beacon> list=new ArrayList<Beacon>();
Connection conn=HiveJDBC.getConnection();
Statement stat=conn.createStatement();
ResultSet rs=stat.executeQuery(sql);
while(rs.next()) {
Beacon b=new Beacon();
b.setStime(rs.getString(1));
b.setUid(rs.getString(2));
b.setSid(rs.getString(3));
b.setPid(rs.getString(4));
list.add(b);
}
return list;
}
}
测试类:
package com.rxzx.hive;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class TestDemo {
public static void main(String[] args) {
BeaconDao bd=new BeaconDao();
List<String> columns=new ArrayList<String>();
columns.add("stime string");
columns.add("uid string");
columns.add("sid string");
columns.add("pid string");
try {
//bd.dropTable("beacon");
bd.createTable("beacon", columns, null, " row format delimited fields terminated by '\t' ");
//bd.load(true, "/home/Teacher/datafile/vistdata.log", false, "beacon", "");
//List<Beacon> list=bd.getData("select * from beacon where sid='h_1009'");
//for(Beacon b:list) { System.out.println(b.toString()); }
System.out.println("执行完成");
} catch (SQLException e) {
System.out.println("执行失败");
e.printStackTrace();
}finally {
HiveJDBC.close();
}
}
}
关于“java如何操作hive”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341