大資料Hive系列之Hive API(jdbc增刪改查 &)
Maven依賴配置
-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
<modelVersion>4.0.0</modelVersion>
-
<groupId>com.volitation.hive</groupId>
-
<artifactId>bigdata-data-management</artifactId>
-
<version>1.0.0-SNAPSHOT</version>
-
<packaging>jar</packaging>
-
<properties>
-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-
<java.version>1.8</java.version>
-
<!-- junit -->
-
<junit.version>4.11</junit.version>
-
<!-- hadoop -->
-
<hadoop.core.version>1.2.1</hadoop.core.version>
-
<!-- hive -->
-
<hive.version>2.1.1</hive.version>
-
</properties>
-
<repositories>
-
<repository>
-
<id>spring-milestones</id>
-
<url>http://repo.spring.io/libs-milestone/</url>
-
</repository>
-
</repositories>
-
<dependencies>
-
<dependency>
-
<groupId>junit</groupId>
-
<artifactId>junit</artifactId>
-
<version>${junit.version}</version>
-
</dependency>
-
<!-- hadoop -->
-
<dependency>
-
<groupId>org.apache.hadoop</groupId>
-
<artifactId>hadoop-core</artifactId>
-
<version>${hadoop.core.version}</version>
-
</dependency>
-
<!-- hive -->
-
<dependency>
-
<groupId>org.apache.hive</groupId>
-
<artifactId>hive-jdbc</artifactId>
-
<version>${hive.version}</version>
-
</dependency>
-
</dependencies>
-
</project>
Hive JDBC四大引數定義
-
/**
-
* 程式碼集定義
-
*
-
* @author volitation
-
*
-
*/
-
public class PlatformDictionary {
-
/*
-
* Hive
-
*/
-
public static final String DRIVER_NAME = "org.apache.hive.jdbc.HiveDriver";
-
public static final String URL = "jdbc:hive2://192.168.9.87:10000";
-
public static final String USER_NAME = "hadoop";
-
public static final String PASSWORD = "hadoop!QWE";
-
}
異常處理
-
import java.util.HashMap;
-
import java.util.Map;
-
/**
-
* 異常處理工具類
-
*
-
* @author volitation
-
*
-
*/
-
public class AbnormalUtils {
-
/**
-
* 獲取異常資訊
-
*
-
* @param e
-
* @return
-
*/
-
public static Object getAbnormal(Exception e) {
-
Object abnormalType = e.getCause().getClass().toString();
-
Object abnormalName = e.getCause().getMessage().toString();
-
Map<String, Object> map = new HashMap<>();
-
map.put("異常型別", abnormalType);
-
map.put("異常點資訊", abnormalName);
-
return map.toString();
-
}
-
}
Hive通過JDBC進行增刪查操作
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
import java.sql.Statement;
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.List;
-
import java.util.Map;
-
import com.volitation.hive.platform.PlatformDictionary;
-
import com.volitation.hive.platform.utils.AbnormalUtils;
-
/**
-
* Hive JDBC操作
-
*
-
* @author volitation
-
*
-
*/
-
public class HiveJDBC {
-
private static Connection conn = null;
-
private static Statement stmt = null;
-
private static ResultSet rs = null;
-
/**
-
* 載入驅動,建立連線
-
*
-
* @throws Exception
-
*/
-
private static void init() {
-
try {
-
Class.forName(PlatformDictionary.DRIVER_NAME);
-
conn = DriverManager.getConnection(PlatformDictionary.URL, PlatformDictionary.USER_NAME,
-
PlatformDictionary.PASSWORD);
-
stmt = conn.createStatement();
-
} catch (ClassNotFoundException | SQLException e) {
-
Object object = AbnormalUtils.getAbnormal(e);
-
System.err.println(object);
-
}
-
}
-
/**
-
* 建立資料庫
-
*
-
* @param databaseName
-
*/
-
public static void createDatabase(String databaseName) {
-
try {
-
init();
-
String sql = "create database " + databaseName;
-
System.out.println("Running: " + sql);
-
stmt.execute(sql);
-
} catch (SQLException e) {
-
Object object = AbnormalUtils.getAbnormal(e);
-
System.err.println(object);
-
} finally {
-
destory();
-
}
-
}
-
/**
-
* 查詢所有資料庫
-
*
-
* @return
-
*/
-
public static List<String> showDatabases() {
-
List<String> list = new ArrayList<>();
-
try {
-
init();
-
String sql = "show databases";
-
System.out.println("Running: " + sql);
-
rs = stmt.executeQuery(sql);
-
while (rs.next()) {
-
list.add(rs.getString(1));
-
}
-
} catch (SQLException e) {
-
Object object = AbnormalUtils.getAbnormal(e);
-
System.err.println(object);
-
} finally {
-
destory();
-
}
-
return list;
-
}
-
/**
-
* 刪除資料庫
-
*
-
* @param databaseName
-
*/
-
public static void dropDatabase(String databaseName) {
-
try {
-
init();
-
String sql = "drop database if exists " + databaseName;
-
System.out.println("Running: " + sql);
-
stmt.execute(sql);
-
} catch (SQLException e) {
-
Object object = AbnormalUtils.getAbnormal(e);
-
System.err.println(object);
-
} finally {
-
destory();
-
}
-
}
-
/**
-
* 建立表
-
*
-
* @param createTableSql
-
*/
-
public static void createTable(String createTableSql) {
-
try {
-
init();
-
System.out.println("Running: " + createTableSql);
-
stmt.execute(createTableSql);
-
} catch (SQLException e) {
-
Object object = AbnormalUtils.getAbnormal(e);
-
System.err.println(object);
-
} finally {
-
destory();
-
}
-
}
-
/**
-
* 查詢所有表
-
*
-
* @return
-
*/
-
public static List<String> showTables(String databaseName) {
-
List<String> list = new ArrayList<>();
-
try {
-
init();
-
String useSql = "use " + databaseName;
-
System.out.println("Running: " + useSql);
-
stmt.execute(useSql);
-
String sql = "show tables";
-
System.out.println("Running: " + sql);
-
rs = stmt.executeQuery(sql);
-
while (rs.next()) {
-
list.add(rs.getString(1));
-
}
-
} catch (SQLException e) {
-
Object object = AbnormalUtils.getAbnormal(e);
-
System.err.println(object);
-
} finally {
-
destory();
-
}
-
return list;
-
}
-
/**
-
* 查看錶結構
-
*
-
* @param databaseName
-
* @param tableName
-
* @return
-
*/
-
public static List<Map<String, Object>> descTable(String databaseName, String tableName) {
-
List<Map<String, Object>> list = new ArrayList<>();
-
Map<String, Object> map = null;
-
try {
-
init();
-
String sql = "desc " + databaseName + "." + tableName;
-
System.out.println("Running: " + sql);
-
rs = stmt.executeQuery(sql);
-
while (rs.next()) {
-
map = new HashMap<>();
-
map.put("colName", rs.getString(1));
-
map.put("dataType", rs.getString(2));
-
list.add(map);
-
}
-
} catch (SQLException e) {
-
Object object = AbnormalUtils.getAbnormal(e);
-
System.err.println(object);
-
} finally {
-
destory();
-
}
-
return list;
-
}
-
/**
-
* 載入資料
-
*
-
* @param hdfsPath
-
* @param tableName
-
*/
-
public static void loadData(String hdfsPath, String tableName) {
-
try {
-
init();
-
String sql = "load data inpath '" + hdfsPath + "' insert into table " + tableName;
-
System.out.println("Running: " + sql);
-
stmt.execute(sql);
-
} catch (SQLException e) {
-
Object object = AbnormalUtils.getAbnormal(e);
-
System.err.println(object);
-
} finally {
-
destory();
-
}
-
}
-
/**
-
* 查詢資料
-
*
-
* @param selectSql
-
* @return
-
*/
-
public static List<String> selectData(String selectSql) {
-
List<String> list = new ArrayList<>();
-
try {
-
init();
-
System.out.println("Running: " + selectSql);
-
rs = stmt.executeQuery(selectSql);
-
while (rs.next()) {
-
list.add(rs.getString(1));
-
}
-
} catch (SQLException e) {
-
Object object = AbnormalUtils.getAbnormal(e);
-
System.err.println(object);
-
} finally {
-
destory();
-
}
-
return list;
-
}
-
/**
-
* 刪除資料庫表
-
*
-
* @param databaseName
-
* @param tableName
-
*/
-
public static void deopTable(String databaseName, String tableName) {
-
try {
-
init();
-
String sql = "drop table if exists " + databaseName + "." + tableName;
-
System.out.println("Running: " + sql);
-
stmt.execute(sql);
-
} catch (SQLException e) {
-
Object object = AbnormalUtils.getAbnormal(e);
-
System.err.println(object);
-
} finally {
-
destory();
-
}
-
}
-
/**
-
* 釋放資源
-
*/
-
private static void destory() {
-
try {
-
if (rs != null) {
-
rs.close();
-
}
-
if (stmt != null) {
-
stmt.close();
-
}
-
if (conn != null) {
-
conn.close();
-
}
-
} catch (SQLException e) {
-
Object object = AbnormalUtils.getAbnormal(e);
-
System.err.println(object);
-
}
-
}
-
}
相關推薦
大資料Hive系列之Hive API(jdbc增刪改查 &)
Maven依賴配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schema
C# 資料操作系列 - 8. EF Core的增刪改查
# 0.前言 到目前為止,我們看了一下如何宣告EF Core的初步使用,也整體的看了下EF Core的對映關係配置以及導航屬性的配置。 這一篇,我帶大家分享一下,我在工作中需要的EF Core的用法。 # 1. 初始化 在實際開發中,一般都是先設計好資料表再進行開發,所以很少用到EF Core的資料遷
大資料Hive系列之Hive API
Maven依賴配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ma
大資料Hive系列之Hive MapReduce
1. JOIN 1.1 join操作 INSERT OVERWRITE TABLE pv_users SELECT pv.pageid, u.age FROM page_view pv JOIN user u ON (pv.userid = u.userid);
大資料Hive系列之Hive常用SQL
1. hive匯出資料到hdfs 語法:export table 表名 to '輸出路徑'; 例子:export table cloud.customer to '/tmp/hive/customer'; 2. beeline連線 $ beeline 語法:beeline> !
大資料Hive系列之Hive使用者許可權管理
1. 角色 * 建立角色 create role role_name; * 顯示角色 show roles; * 刪除角色 drop role role_name; 2. 使用者 * 使用者進入admin角色許可權 set hive.users.in.admin.role;
大資料Hive系列之Hive效能優化
一、介紹 首先,我們來看看Hadoop的計算框架特性,在此特性下會衍生哪些問題? 資料量大不是問題,資料傾斜是個問題。 jobs數比較多的作業執行效率相對比較低,比如即使有幾百行的表,如果多次關聯多次彙總,產生十幾個jobs,耗時很長。原因是map reduce作業初
大資料學習系列之七 ----- Hadoop+Spark+Zookeeper+HBase+Hive叢集搭建 圖文詳解
引言 在之前的大資料學習系列中,搭建了Hadoop+Spark+HBase+Hive 環境以及一些測試。其實要說的話,我開始學習大資料的時候,搭建的就是叢集,並不是單機模式和偽分散式。至於為什麼先寫單機的搭建,是因為作為個人學習的話,單機已足以,好吧,
大資料數倉之Hive入門《一》
一 、Hive 基本概念 1.1 什麼是 Hive Hive:由 Facebook 開源用於解決海量結構化日誌的資料統計。它是基於 Hadoop 的一個數據倉庫工具,可以將結構化的資料檔案對映為一張表,並提供類 SQL 查詢功能。 本質就是:將HQL 轉化成 MapReduce 程式 1
【大資料學習】之Hive部署
為了減少衝突,一般來說版本都用統一的比較好,所以選擇CDH的部署。 前面的hadoop是選擇hadoop-2.6.0-cdh5.7.0 ,所以hive安裝的版本跟hadooop的尾巴對準了。 2、ctrl+F 搜 hive-1.1.0-cdh5.7.0 , 右鍵選擇
大資料Zookeeper系列之Zookeeper服務開機自啟動配置
1. 編寫執行指令碼 $ sudo cd /etc/init.d $ sudo vi zookeeper #!/bin/bash #chkconfig:2345 20 90 #description:zookeeper #processname:zookeeper
大資料Hadoop系列之Hadoop服務開機自啟動配置
1. 編寫執行指令碼 $ sudo cd /etc/init.d $ sudo vi hadoop #!/bin/bash #chkconfig:35 95 1 #description:script to start/stop hadoop su - hadoop
大資料HBase系列之HBase分散式資料庫部署
一、部署準備 1. 依賴框架 大資料Hadoop系列之Hadoop分散式叢集部署:https://blog.csdn.net/volitationLong/article/details/80285123 大資料Zookeeper系列之Zookeeper叢集部署:https://
大資料Zookeeper系列之Zookeeper分散式協調服務部署
一、部署準備 1. 安裝介質 zookeeper-3.4.13:http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.13/zookeeper-3.4.13.tar.gz 2. 主機規劃 192.168.233.13
大資料HBase系列之初識HBase
1. HBase簡介 1.1 為什麼使用HBase 傳統的RDBMS關係型資料庫(MySQL/Oracle)儲存一定量資料時進行資料檢索沒有問題,可當資料量上升到非常巨大規模的資料(TB/PB)級別時,傳統的RDBMS已無法支撐,這時候就需要一種新型的資料庫系統更好更
大資料Flume系列之Flume叢集搭建
1. 概念 叢集的意思是多臺機器,最少有2臺機器,一臺機器從資料來源中獲取資料,將資料傳送到另一臺機器上,然後輸出。接下來就要實現Flume叢集搭建。叢集如下圖所示。 2. Flume搭建 2.1 部署準備 部署主機 192.168.9.139 host14
大資料專案實戰之八:8.JDBC元件開發
/** * JDBC輔助元件 * 在正式的專案的程式碼編寫過程中,是完全嚴格按照大公司的coding標準來的 * 也就是說,在程式碼中,是不能出現任何hard code(硬編碼)的字元 * 比如“張三”、“com.mysql.jdbc.Driver”
大資料專案實戰之七:7.JDBC原理
1.Java程式/J2EE程式,當需要操作資料庫,通常就是對資料庫中某一個,或者某些表,進行增刪改查 那麼就需要通過某些方式連線到資料庫,比如MySQL,執行各種SQL語句(insert、select、udpate) 2.JDBC,Java Database Connectivity,Jav
大資料學習系列之—HBASE
hadoop生態系統 zookeeper負責協調 hbase必須依賴zookeeper flume 日誌工具 sqoop 負責 hdfs dbms 資料轉換 資料到關係型資料庫轉換 大資料學習群119599574 hbase簡介 hadoop database 是一個
大資料Hadoop系列之Hadoop Web控制檯新增身份驗證
1. 背景介紹 本文件介紹如何配置Hadoop HTTP Web控制檯以要求使用者身份驗證。 預設情況下,Hadoop HTTP Web控制檯(ResourceManager,NameNode,NodeManagers和DataNodes)允許訪問而無需任何形式的身份驗證