1. 程式人生 > >大資料Hive系列之Hive API(jdbc增刪改查 &)

大資料Hive系列之Hive API(jdbc增刪改查 &)

Maven依賴配置

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  3. <modelVersion>4.0.0</modelVersion>

  4. <groupId>com.volitation.hive</groupId>

  5. <artifactId>bigdata-data-management</artifactId>

  6. <version>1.0.0-SNAPSHOT</version>

  7. <packaging>jar</packaging>

  8. <properties>

  9. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  10. <java.version>1.8</java.version>

  11. <!-- junit -->

  12. <junit.version>4.11</junit.version>

  13. <!-- hadoop -->

  14. <hadoop.core.version>1.2.1</hadoop.core.version>

  15. <!-- hive -->

  16. <hive.version>2.1.1</hive.version>

  17. </properties>

  18. <repositories>

  19. <repository>

  20. <id>spring-milestones</id>

  21. <url>http://repo.spring.io/libs-milestone/</url>

  22. </repository>

  23. </repositories>

  24. <dependencies>

  25. <dependency>

  26. <groupId>junit</groupId>

  27. <artifactId>junit</artifactId>

  28. <version>${junit.version}</version>

  29. </dependency>

  30. <!-- hadoop -->

  31. <dependency>

  32. <groupId>org.apache.hadoop</groupId>

  33. <artifactId>hadoop-core</artifactId>

  34. <version>${hadoop.core.version}</version>

  35. </dependency>

  36. <!-- hive -->

  37. <dependency>

  38. <groupId>org.apache.hive</groupId>

  39. <artifactId>hive-jdbc</artifactId>

  40. <version>${hive.version}</version>

  41. </dependency>

  42. </dependencies>

  43. </project>

 Hive JDBC四大引數定義

  1. /**

  2. * 程式碼集定義

  3. *

  4. * @author volitation

  5. *

  6. */

  7. public class PlatformDictionary {

  8. /*

  9. * Hive

  10. */

  11. public static final String DRIVER_NAME = "org.apache.hive.jdbc.HiveDriver";

  12. public static final String URL = "jdbc:hive2://192.168.9.87:10000";

  13. public static final String USER_NAME = "hadoop";

  14. public static final String PASSWORD = "hadoop!QWE";

  15. }

 異常處理

  1. import java.util.HashMap;

  2. import java.util.Map;

  3. /**

  4. * 異常處理工具類

  5. *

  6. * @author volitation

  7. *

  8. */

  9. public class AbnormalUtils {

  10. /**

  11. * 獲取異常資訊

  12. *

  13. * @param e

  14. * @return

  15. */

  16. public static Object getAbnormal(Exception e) {

  17. Object abnormalType = e.getCause().getClass().toString();

  18. Object abnormalName = e.getCause().getMessage().toString();

  19. Map<String, Object> map = new HashMap<>();

  20. map.put("異常型別", abnormalType);

  21. map.put("異常點資訊", abnormalName);

  22. return map.toString();

  23. }

  24. }

Hive通過JDBC進行增刪查操作

  1. import java.sql.Connection;

  2. import java.sql.DriverManager;

  3. import java.sql.ResultSet;

  4. import java.sql.SQLException;

  5. import java.sql.Statement;

  6. import java.util.ArrayList;

  7. import java.util.HashMap;

  8. import java.util.List;

  9. import java.util.Map;

  10. import com.volitation.hive.platform.PlatformDictionary;

  11. import com.volitation.hive.platform.utils.AbnormalUtils;

  12. /**

  13. * Hive JDBC操作

  14. *

  15. * @author volitation

  16. *

  17. */

  18. public class HiveJDBC {

  19. private static Connection conn = null;

  20. private static Statement stmt = null;

  21. private static ResultSet rs = null;

  22. /**

  23. * 載入驅動,建立連線

  24. *

  25. * @throws Exception

  26. */

  27. private static void init() {

  28. try {

  29. Class.forName(PlatformDictionary.DRIVER_NAME);

  30. conn = DriverManager.getConnection(PlatformDictionary.URL, PlatformDictionary.USER_NAME,

  31. PlatformDictionary.PASSWORD);

  32. stmt = conn.createStatement();

  33. } catch (ClassNotFoundException | SQLException e) {

  34. Object object = AbnormalUtils.getAbnormal(e);

  35. System.err.println(object);

  36. }

  37. }

  38. /**

  39. * 建立資料庫

  40. *

  41. * @param databaseName

  42. */

  43. public static void createDatabase(String databaseName) {

  44. try {

  45. init();

  46. String sql = "create database " + databaseName;

  47. System.out.println("Running: " + sql);

  48. stmt.execute(sql);

  49. } catch (SQLException e) {

  50. Object object = AbnormalUtils.getAbnormal(e);

  51. System.err.println(object);

  52. } finally {

  53. destory();

  54. }

  55. }

  56. /**

  57. * 查詢所有資料庫

  58. *

  59. * @return

  60. */

  61. public static List<String> showDatabases() {

  62. List<String> list = new ArrayList<>();

  63. try {

  64. init();

  65. String sql = "show databases";

  66. System.out.println("Running: " + sql);

  67. rs = stmt.executeQuery(sql);

  68. while (rs.next()) {

  69. list.add(rs.getString(1));

  70. }

  71. } catch (SQLException e) {

  72. Object object = AbnormalUtils.getAbnormal(e);

  73. System.err.println(object);

  74. } finally {

  75. destory();

  76. }

  77. return list;

  78. }

  79. /**

  80. * 刪除資料庫

  81. *

  82. * @param databaseName

  83. */

  84. public static void dropDatabase(String databaseName) {

  85. try {

  86. init();

  87. String sql = "drop database if exists " + databaseName;

  88. System.out.println("Running: " + sql);

  89. stmt.execute(sql);

  90. } catch (SQLException e) {

  91. Object object = AbnormalUtils.getAbnormal(e);

  92. System.err.println(object);

  93. } finally {

  94. destory();

  95. }

  96. }

  97. /**

  98. * 建立表

  99. *

  100. * @param createTableSql

  101. */

  102. public static void createTable(String createTableSql) {

  103. try {

  104. init();

  105. System.out.println("Running: " + createTableSql);

  106. stmt.execute(createTableSql);

  107. } catch (SQLException e) {

  108. Object object = AbnormalUtils.getAbnormal(e);

  109. System.err.println(object);

  110. } finally {

  111. destory();

  112. }

  113. }

  114. /**

  115. * 查詢所有表

  116. *

  117. * @return

  118. */

  119. public static List<String> showTables(String databaseName) {

  120. List<String> list = new ArrayList<>();

  121. try {

  122. init();

  123. String useSql = "use " + databaseName;

  124. System.out.println("Running: " + useSql);

  125. stmt.execute(useSql);

  126. String sql = "show tables";

  127. System.out.println("Running: " + sql);

  128. rs = stmt.executeQuery(sql);

  129. while (rs.next()) {

  130. list.add(rs.getString(1));

  131. }

  132. } catch (SQLException e) {

  133. Object object = AbnormalUtils.getAbnormal(e);

  134. System.err.println(object);

  135. } finally {

  136. destory();

  137. }

  138. return list;

  139. }

  140. /**

  141. * 查看錶結構

  142. *

  143. * @param databaseName

  144. * @param tableName

  145. * @return

  146. */

  147. public static List<Map<String, Object>> descTable(String databaseName, String tableName) {

  148. List<Map<String, Object>> list = new ArrayList<>();

  149. Map<String, Object> map = null;

  150. try {

  151. init();

  152. String sql = "desc " + databaseName + "." + tableName;

  153. System.out.println("Running: " + sql);

  154. rs = stmt.executeQuery(sql);

  155. while (rs.next()) {

  156. map = new HashMap<>();

  157. map.put("colName", rs.getString(1));

  158. map.put("dataType", rs.getString(2));

  159. list.add(map);

  160. }

  161. } catch (SQLException e) {

  162. Object object = AbnormalUtils.getAbnormal(e);

  163. System.err.println(object);

  164. } finally {

  165. destory();

  166. }

  167. return list;

  168. }

  169. /**

  170. * 載入資料

  171. *

  172. * @param hdfsPath

  173. * @param tableName

  174. */

  175. public static void loadData(String hdfsPath, String tableName) {

  176. try {

  177. init();

  178. String sql = "load data inpath '" + hdfsPath + "' insert into table " + tableName;

  179. System.out.println("Running: " + sql);

  180. stmt.execute(sql);

  181. } catch (SQLException e) {

  182. Object object = AbnormalUtils.getAbnormal(e);

  183. System.err.println(object);

  184. } finally {

  185. destory();

  186. }

  187. }

  188. /**

  189. * 查詢資料

  190. *

  191. * @param selectSql

  192. * @return

  193. */

  194. public static List<String> selectData(String selectSql) {

  195. List<String> list = new ArrayList<>();

  196. try {

  197. init();

  198. System.out.println("Running: " + selectSql);

  199. rs = stmt.executeQuery(selectSql);

  200. while (rs.next()) {

  201. list.add(rs.getString(1));

  202. }

  203. } catch (SQLException e) {

  204. Object object = AbnormalUtils.getAbnormal(e);

  205. System.err.println(object);

  206. } finally {

  207. destory();

  208. }

  209. return list;

  210. }

  211. /**

  212. * 刪除資料庫表

  213. *

  214. * @param databaseName

  215. * @param tableName

  216. */

  217. public static void deopTable(String databaseName, String tableName) {

  218. try {

  219. init();

  220. String sql = "drop table if exists " + databaseName + "." + tableName;

  221. System.out.println("Running: " + sql);

  222. stmt.execute(sql);

  223. } catch (SQLException e) {

  224. Object object = AbnormalUtils.getAbnormal(e);

  225. System.err.println(object);

  226. } finally {

  227. destory();

  228. }

  229. }

  230. /**

  231. * 釋放資源

  232. */

  233. private static void destory() {

  234. try {

  235. if (rs != null) {

  236. rs.close();

  237. }

  238. if (stmt != null) {

  239. stmt.close();

  240. }

  241. if (conn != null) {

  242. conn.close();

  243. }

  244. } catch (SQLException e) {

  245. Object object = AbnormalUtils.getAbnormal(e);

  246. System.err.println(object);

  247. }

  248. }

  249. }

相關推薦

資料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)允許訪問而無需任何形式的身份驗證