资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java如何操作hive

这篇文章将为大家详细讲解有关java如何操作hive,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

成都创新互联专业为企业提供榆次网站建设、榆次做网站、榆次网站设计、榆次网站制作等企业网站建设、网页设计与制作、榆次企业网站模板建站服务,10多年榆次做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

存储类:

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 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 getData(String sql)  throws SQLException{

List list=new ArrayList();

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 columns=new ArrayList();

        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 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”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


当前文章:java如何操作hive
文章起源:http://cdkjz.cn/article/jejopd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220