您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)java如何操作hive,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
存儲(chǔ)類(lèi):
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;
}
}
創(chuàng)建連接和關(guān)閉連接類(lèi):
package com.rxzx.hive;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class HiveJDBC {
private static Connection conn;
//初始化數(shù)據(jù)倉(cāng)庫(kù)連接對(duì)象
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("數(shù)據(jù)倉(cāng)庫(kù)連接失敗");
e.printStackTrace();
}
}
return conn;
}
//關(guān)閉連接對(duì)象
public static void close() {
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
功能類(lèi):創(chuàng)建表,刪除表,加載數(shù)據(jù),數(shù)據(jù)展現(xiàn)到j(luò)ava窗口
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);
}
//創(chuàng)建表
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;
}
}
測(cè)試類(lèi):
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("執(zhí)行完成");
} catch (SQLException e) {
System.out.println("執(zhí)行失敗");
e.printStackTrace();
}finally {
HiveJDBC.close();
}
}
}
關(guān)于“java如何操作hive”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。