您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關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”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。