Hive JDBC操作

2020-10-18 Java實用技術

一:啟動hadoop

1. core-site.xml 配置代理用戶屬性

特別注意:hadoop.proxyuser.<伺服器用戶名>.hosts 和 hadoop.proxyuser.<伺服器用戶名>.groups這兩個屬性,伺服器用戶名是hadoop所在的機器的登錄的名字,根據自己實際的登錄名來配置。這裡我的電腦用戶名為mengday。

<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="configuration.xsl"?><configuration> <property> <name>hadoop.tmp.dir</name> <value>file:/usr/local/Cellar/hadoop/3.2.1/libexec/tmp</value> </property> <property> <name>fs.defaultFS</name> <value>hdfs://localhost:8020</value> </property> <property> <name>hadoop.proxyuser.mengday.hosts</name> <value>*</value> </property> <property> <name>hadoop.proxyuser.mengday.groups</name> <value>*</value> </property> </configuration>

2. 啟動hadoop

> cd /usr/local/Cellar/hadoop/3.2.1/sbin> ./start-all.sh> jps

啟動成功後注意查看DataNode節點是否啟動起來, 經常遇到DataNode節點啟動不成功。

二:配置hive-site.xml

Java是通過beeline來連接Hive的。啟動beeline最重要的就是配置好hive-site.xml。

其中javax.jdo.option.ConnectionURL涉及到一個資料庫,最好重新刪掉原來的metastore資料庫然後重新創建一個並初始化一下。

mysql> create database metastore;

> cd /usr/local/Cellar/hive/3.1.2/libexec/bin> schematool -initSchema -dbType mysql

hive-site.xml

<configuration>  <property> <name>hive.metastore.local</name> <value>true</value> </property> <property> <name>hive.metastore.uris</name> <value>thrift://localhost:9083</value> <description>Thrift URI for the remote metastore. Used by metastore client to connect to remote metastore.</description> </property> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://localhost:3306/metastore?characterEncoding=UTF-8&createDatabaseIfNotExist=true</value> </property> <property> <name>javax.jdo.option.ConnectionDriverName</name> <value>com.mysql.cj.jdbc.Driver</value> </property>  <!--mysql用戶名--> <property> <name>javax.jdo.option.ConnectionUserName</name> <value>root</value> </property>  <!--mysql密碼-->  <property> <name>javax.jdo.option.ConnectionPassword</name> <value>root123</value> </property> <!-- hive用來存儲不同階段的map/reduce的執行計劃的目錄,同時也存儲中間輸出結果 ,默認是/tmp/<user.name>/hive,我們實際一般會按組區分,然後組內自建一個tmp目錄存>儲 --> <property> <name>hive.exec.local.scratchdir</name> <value>/tmp/hive</value> </property> <property> <name>hive.downloaded.resources.dir</name> <value>/tmp/hive</value> </property> <property> <name>hive.metastore.warehouse.dir</name> <value>/data/hive/warehouse</value> </property> <property> <name>hive.metastore.event.db.notification.api.auth</name> <value>false</value> </property> <property> <name>hive.server2.active.passive.ha.enable</name> <value>true</value> </property> <property> <name>hive.server2.transport.mode</name> <value>binary</value> <description> Expects one of [binary, http]. Transport mode of HiveServer2. </description> </property> <property> <name>hive.server2.logging.operation.log.location</name> <value>/tmp/hive</value> </property> <property> <name>hive.hwi.listen.host</name> <value>0.0.0.0</value> <description>This is the host address the Hive Web Interface will listen on</description> </property> <property> <name>hive.server2.webui.host</name> <value>0.0.0.0</value> <description>The host address the HiveServer2 WebUI will listen on</description> </property></configuration>

三:啟動metastore

在啟動beeline之前需要先啟動hiveserver2,而在啟動hiveserver2之前需要先啟動metastore。metastore默認的埠為9083。

> cd /usr/local/Cellar/hive/3.1.2/bin> hive --service metastore &

啟動過一定確認一下啟動是否成功。

四:啟動hiveserver2

> cd /usr/local/Cellar/hive/3.1.2/bin> hive --service hiveserver2 &

hiveserver2默認的埠為10000,啟動之後一定要查看10000埠是否存在,配置有問題基本上10000埠都啟動不成功。10000埠存在不存在是啟動beeline的關鍵。

五:啟動beeline

> cd /usr/local/Cellar/hive/3.1.2/bin> beeline -u jdbc:hive2://localhost:10000/default -n mengday -p

  • -u: 連接的url,jdbc:hive2://<主機名或IP>:<埠默認>/<資料庫名>,埠號默認10000 可通過 ```hiveserver2 --hiveconf hive.server2.thrift.port=14000 修改埠號,default是自帶的資料庫
  • -n: hive所在的那臺伺服器的登錄帳號名稱, 這裡是我Mac機器的登錄用戶名mengday, 這裡的名字要和core-site.xml中的hadoop.proxyuser.mengday.hosts和hadoop.proxyuser.mengday.groups中mengday保持一致。
  • -p: 密碼,用戶名對應的密碼

看到0: jdbc:hive2://localhost:10000/default>就表示啟動成功了。

六:Hive JDBC

1. 引入依賴

<dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-jdbc</artifactId> <version>3.1.2</version></dependency>

2. 準備數據

/data/employee.txt

1,zhangsan,28,60.66,2020-02-01 10:00:00,true,eat#drink,k1:v1#k2:20,s1#c1#s1#12,lisi,29,60.66,2020-02-01 11:00:00,false,play#drink,k3:v3#k4:30,s2#c2#s1#2

3. Java

import java.sql.*;public class HiveJdbcClient { private static String url = "jdbc:hive2://localhost:10000/default"; private static String driverName = "org.apache.hive.jdbc.HiveDriver"; private static String user = "mengday"; private static String password = "user對應的密碼"; private static Connection conn = null; private static Statement stmt = null; private static ResultSet rs = null; static { try { Class.forName(driverName); conn = DriverManager.getConnection(url, user, password); stmt = conn.createStatement(); } catch (Exception e) { e.printStackTrace(); } } public static void init() throws Exception { stmt.execute("drop database if exists hive_test"); stmt.execute("create database hive_test"); rs = stmt.executeQuery("show databases"); while (rs.next()) { System.out.println(rs.getString(1)); } stmt.execute("drop table if exists employee"); String sql = "create table if not exists employee(" + " id bigint, " + " username string, " + " age tinyint, " + " weight decimal(10, 2), " + " create_time timestamp, " + " is_test boolean, " + " tags array<string>, " + " ext map<string, string>, " + " address struct<street:String, city:string, state:string, zip:int> " + " ) " + " row format delimited " + " fields terminated by ',' " + " collection items terminated by '#' " + " map keys terminated by ':' " + " lines terminated by '\n'"; stmt.execute(sql); rs = stmt.executeQuery("show tables"); while (rs.next()) { System.out.println(rs.getString(1)); } rs = stmt.executeQuery("desc employee"); while (rs.next()) { System.out.println(rs.getString(1) + "\t" + rs.getString(2)); } } private static void load() throws Exception { // 加載數據 String filePath = "/data/employee.txt"; stmt.execute("load data local inpath '" + filePath + "' overwrite into table employee"); // 查詢數據 rs = stmt.executeQuery("select * from employee"); while (rs.next()) { System.out.println(rs.getLong("id") + "\t" + rs.getString("username") + "\t" + rs.getObject("tags") + "\t" + rs.getObject("ext") + "\t" + rs.getObject("address") ); } } private static void close() throws Exception { if ( rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } public static void main(String[] args) throws Exception { init(); load(); close(); }}

相關焦點

  • Hive學習筆記,看懂 Hive
    處理的數據存儲在HDFS2)Hive分析數據底層的實現是MapReduce3)執行程序運行在Yarn上Hive的優缺點優點1) 操作接口採用類bin/hadoop fs -chmod g+w /tmpbin/hadoop fs -chmod g+w /user/hive/warehouse3.Hive基本操作(1)啟動hivebin/hive(2)查看資料庫
  • 數據倉庫組件:Hive環境搭建和基礎用法
    Hive收到給客戶端發送的交互請求,接收到操作指令(SQL),並將指令翻譯成MapReduce,提交到Hadoop中執行,最後將執行結果輸出到客戶端。二、Hive環境安裝1、準備安裝包hive-1.2,依賴Hadoop集群環境,位置放在hop01服務上。
  • sqoop的使用之導入到hive和mysql
    承接上一篇文檔《sqoop的使用之import導入到HDFS》mysql導入數據到hive中該如何做呢?先登錄hive在hive中操作,創建資料庫:create database frommysql;創建表創建表create table importhive_info(num int,name string)row format delimited fields terminated
  • HIVE的安裝與配置
    2、 連接不上hiveserver2,在bin/hiveserver2啟動時等待了很長時間,一直沒響應,所以百度了一下,原因是後臺已經在執行路徑運行了,只需要用Xshell複製一個窗口hadoop111執行bin/beeline就可以連接到beeline了。3、 拒絕訪問hive的jdbc:即beeline> !
  • 使用sqoop在MySQL、hadoop、hive間同步數據
    /usr/local/service/sqoop/bin/sqoop-import \--connect jdbc:mysql://$mysqlIP/test --username root -P --table sqoop_test \--hive-import --hive-database db_sqoop_test --hive-table sqoop_test
  • Hive 如何快速拉取大批量數據
    用hive來做數倉類操作,或者大數據的運算,是沒有疑問的,至少在你沒有更多選擇之前。當我們要hive來做類似於大批量數據的select時,也許問題就會發生了變化。 1.
  • CentOS+Hadoop+MySQL安裝Hive
    /test/hive-3.1.2/conf目錄,下創建hive-site.xml 文件,複製hive-default.xml.template並更名為hive-site.xmlcp hive-default.xml.template hive-site.xml 4、創建HDFS文件夾提前把hadoop安裝配置好後啟動,具體安裝配置過程可以參考文章:《大數據01_centos7
  • 大數據兵器譜之hive數據倉庫
    項,將其修改為 :property.hive.log.dir = /opt/hive/logs/6、啟動(這裡我們先來簡單模式,直接使用自帶的微型資料庫啟動):先初始化資料庫:schematool -dbType derby -initSchema 然後輸入:hive直接啟動,稍等片刻你就可以看到一個以"hive>"開頭shell窗口,在這裡就可以對數據倉庫進行操作;這裡的語法和
  • MySQL數據導入Hive-Java
    "; String url = "jdbc:mysql://IP:3306/xcxvt?file.getPath()), new Path("/mysql")); //刪除臨時文件 file.deleteOnExit(); 導入Hive String driverName = "org.apache.hive.jdbc.HiveDriver
  • hive學習筆記之八:Sqoop
    導入MySQL(export)執行以下命令,將hive的數據導入到MySQL:./sqoop export \--connect jdbc:mysql://192.168.50.43:3306/sqoop \--table address \--username root \--password 123456 \--export-dir '/user/hive/warehouse/address' \--fields-terminated-by ','
  • HBase基礎環境搭建之Hive和Sqoop安裝
    目錄中配置文件目錄先重命名hive-default.xml.templatemv hive-default.xml.template hive-site.xml再重命名hive-env.sh.templatemv hive-env.sh.template hive-env.sh再重命名hive-log4j.properties.template
  • Win10系統下Hadoop和Hive開發環境搭建填坑指南
    在$HIVE_HOME/bin目錄下執行下面的命令可以啟動HiveServer2:hive.cmd --service hiveserver2客戶端需要引入hadoop-common和hive-jdbc依賴,依賴的版本儘量和對接的Hadoop和Hive版本對應。
  • 快速搭建CDH-Hadoop-Hive-Zoopkeeper-Sqoop學習環境
    目錄中配置文件目錄先重命名hive-default.xml.templatemv hive-default.xml.template hive-site.xml再重命名hive-env.sh.templatemv hive-env.sh.template hive-env.sh再重命名hive-log4j.properties.template
  • Apache Hive 0.7.1 發布,數據倉庫平臺
    通過hive,我們可以方便地進行ETL的工作。hive定義了一個類似於SQL的查詢語言:HQL,能 夠將用戶編寫的QL轉化為相應的Mapreduce程序基於Hadoop執行。 Hive 0.7.1 下載:http://hive.apache.org/releases.html#Download 版本更新日誌:** Bug    * [HIVE-2054] - Exception on windows when using the jdbc driver.
  • 0791-5.13.1-Hive視圖執行show create table被截斷異常分析
    2.通過Beeline0: jdbc:hive2://localhost:10000/default> SHOW CREATE TABLE test_view;+------------------------------------+--+|           createtab_stmt
  • 編寫Hive的UDF(查詢平臺數據同時向mysql添加數據)
    根據給定的參數值,構建多個不同維度的平臺維度對象完成參數驗證然後構建平臺信息添加write和readFields方法創建compareTo方法添加get/set、hashCode、toString、equals方法,自動生成就可以創建信息業務接口和實現類完成接口添加實現類創建一個jdbc
  • 大數據之Hive安裝配置
    hive默認將元數據存儲到本地內嵌的Derby資料庫中,但是Derby不支持多會話連結,因此我們使用mysql資料庫來存儲hive的元數據。配置完成hiveSQL的元資料庫之後再開始安裝、配置hive。
  • SpringBoot+Sharding-JDBC操作分庫分表
    .url=jdbc:mysql://localhost:3306/goods_db?.url=jdbc:mysql://localhost:3306/goods_db?上面講述了使用Sharding-JDBC如何對水平分表的操作,接下來玩玩水平分庫分表操作。
  • 數倉工程師的利器-HIVE詳解
    >hive>set hive.cli.print.current.db=true;2.顯示查詢結果時顯示欄位名稱:hive>set hive.cli.print.header=true;connect jdbc:hive2://mini1:10000(hadoop01是hiveserver2所啟動的那臺主機名,埠默認是10000)
  • 大數據分析:數據倉庫hive詳解
    大家先對上述hive先有個總體上大致的認識,通過實戰的操作再學習細節。;jdbc:mysql://192.168.1.66:3306/demo?>(5)其它類型Hive支持的其它類型有BOOLEAN和BINARY2、hive資料庫和表的基本操作在hive提示符也可以操作linux指令,格式為:!linux指令,例如:hive> !