Hive安裝配置使用及java api調用

mysql -u root -p root;

建立hive用戶,hive數據庫,並授權。

create database hive;

grant all on hive.* to hive@'%'  identified by 'hive';

grant all on hive.* to hive@'localhost'  identified by 'hive';

flush privileges; 

 

退出mysql 

exit

 

驗證hive用戶

mysql -u hive -p hive

show databases;

看到如下反饋信息,則說明創建成功

mysql> show databases;

+--------------------+

| Database          |

+--------------------+

| information_schema |

| hive              |

| test              |

+--------------------+

3 rows in set (0.00 sec)

 

退出mysql

exit

 

三,安裝hive

1,解壓安裝包

cd  ~

tar -zxvf apache-hive-1.0.1-bin.tar.gz

2,建立軟連接

ln -s apache-hive-1.0.1-bin hive

3,添加環境變量

vi  .bash_profile

導入下面的環境變量

export HIVE_HOME=/home/Hadoop/software/hive

export PATH=$PATH:$HIVE_HOME/bin

 

使其有效

source .bash_profile

 

4

cphive/conf/hive-default.xml.template hive/conf/hive-site.xml

編輯hive-site.xml修改以下參數:

<property> 

   <name>javax.jdo.option.ConnectionURL </name> 

   <value>jdbc:mysql://localhost:3306/hive </value> 

</property> 

 

<property> 

   <name>javax.jdo.option.ConnectionDriverName </name> 

   <value>com.mysql.jdbc.Driver </value> 

</property>



<property> 

   <name>javax.jdo.option.ConnectionPassword </name> 

   <value>hive </value> 

</property> 

 

<property> 

   <name>hive.hwi.listen.port </name> 

   <value>9999 </value> 

   <description>This is the port the Hive Web Interface will listen on </descript ion> 

</property> 



<property> 

   <name>datanucleus.autoCreateSchema </name> 

   <value>true</value> 

</property> 

 

<property> 

   <name>datanucleus.fixedDatastore </name> 

   <value>false</value> 

</property> 

</property> 



  <property>

    <name>javax.jdo.option.ConnectionUserName</name>

    <value>hive</value>

    <description>Username to use against metastoredatabase</description>

  </property>



  <property>

    <name>hive.exec.local.scratchdir</name>

    <value>/home/hdpsrc/hive/iotmp</value>

    <description>Local scratch space for Hivejobs</description>

  </property>

  <property>

    <name>hive.downloaded.resources.dir</name>

    <value>/home/hdpsrc/hive/iotmp</value>

    <description>Temporary local directory for addedresources in the remote file system.</description>

  </property>

  <property>

    <name>hive.querylog.location</name>

    <value>/home/hdpsrc/hive/iotmp</value>

    <description>Location of Hive run time structured logfile</description>

  </property>

 

 

5,拷貝mysql-connector-java-5.1.6-bin.jar 到hive 的lib下面

mv/home/hdpsrc/Desktop/mysql-connector-java-5.1.6-bin.jar /home/hdpsrc/hive/lib/

 

 

 

 

hive常用命令     

 

#創建新表

 

hive> CREATE TABLE t_hive (a int, b int, c int) ROW FORMAT DELIMITEDFIELDS TERMINATED BY ',';

 

#導入數據t_hive.txtt_hive

 

hive> LOAD DATA LOCAL INPATH '/home/hadoop/software/test/t_hive.txt'OVERWRITE INTO TABLE t_hive;

 

#正則匹配表名

 

hive>show tables '*t*';

 

#增加一個字段

 

hive> ALTER TABLE t_hive ADD COLUMNS (new_col String);

 

#重命令表名

 

hive> ALTER TABLE t_hive RENAME TO t_hadoop;

 

#HDFS加載數據

 

hive> LOAD DATA INPATH '/user/hive/warehouse/t_hive/t_hive.txt'OVERWRITE INTO TABLE t_hive2;

 

#從其他表導入數據

 

hive> INSERT OVERWRITE TABLE t_hive2 SELECT * FROM t_hive ;

 

#創建表並從其他表導入數據

 

hive> CREATE TABLE t_hive AS SELECT * FROM t_hive2 ;

 

#僅複製表結構不導數據

 

hive> CREATE TABLE t_hive3 LIKE t_hive;

 

#通過Hive導出到本地文件系統

 

hive> INSERT OVERWRITE LOCAL DIRECTORY '/tmp/t_hive' SELECT * FROMt_hive;

 

#Hive查詢HiveQL

 

from ( select b,c as c2 from t_hive) t select t.b, t.c2 limit 2;

 

select b,c from t_hive limit 2;

 

#創建視圖

 

hive> CREATE VIEW v_hive AS SELECT a,b FROM t_hive;

 

#刪表

 

drop table if exists t_hft;

 

#創建分區表

 

DROP TABLE IF EXISTS t_hft;

CREATE TABLE t_hft(

SecurityID STRING,

tradeTime STRING,

PreClosePx DOUBLE

) PARTITIONED BY (tradeDate INT)

ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

 

#導入分區數據

 

hive> load data local inpath '/home/BlueBreeze/data/t_hft_1.csv'overwrite into table t_hft partition(tradeDate=20130627);

 

#查看分區表

 

hive> SHOW PARTITIONS t_hft;

 

 

api調用:

 

nohup hive --service hiveserver2 &

或者bin/hive --service hiveserver -p 10002

 

代表你已經成功的在端口爲10002(默認的端口是10000)啓動了hiveserver服務。這時候,你就可以通過Java代碼來連接hiveserver

問題1:無法啓動,因爲用的是hive2,應該使用命令 hive –service hiveserver2

問題2Nosuitable driver found for jdbc:hive://192.168.184.169:10000/default ,原因,因爲用的版本是hive-1.0.1,url應該改成jdbc:hive2://192.168.184.169:10000/default即可

 

代碼如下:

import java.sql.Connection; 

import java.sql.DriverManager; 

import java.sql.ResultSet; 

import java.sql.SQLException; 

import java.sql.Statement; 

 

import org.apache.log4j.Logger; 

 

 /**

  * Hive版本:1.0.1

  *

 * HiveJavaApi

 

 * 啓動hive的遠程服務接口命令行執行:bin/hive --service hiveserver2 &

 

 * @author ycblus

*/ 

 public class HiveJdbcClient { 

   static Logger log =Logger.getLogger("HiveJdbcClient");

  

   private static String driverName= "org.apache.hive.jdbc.HiveDriver"; 

   private static String url ="jdbc:hive2://192.168.184.158:10000/default"; 

   private static String user ="hadoop";  //主機的用戶名

   private static String password ="hadoop";  //遠程主機密碼

   private static String sql =""; 

   private static ResultSetres; 

 

   public static void main(String[]args) { 

      

       Connection conn = null; 

       Statement stmt = null; 

       try { 

           conn = getConn(); 

           stmt =conn.createStatement(); 

 

           String tableName ="t_hive";

           // 第一步:存在就先刪除 

           dropTable(stmt); 

 

           // 第二步:不存在就創建 

           createTable(stmt,tableName); 

 

           // 第三步:查看創建的表 

           showTables(stmt,tableName); 

 

           // 執行describe table操作 

           describeTables(stmt,tableName); 

 

           // 執行load data into table操作 

           loadData(stmt,tableName); 

 

           // 執行 select * query 操作 

           //selectData(stmt,tableName); 

 

           // 執行 regular hive query 統計操作 

           countData(stmt,tableName);

 

       } catch(ClassNotFoundException e) { 

           e.printStackTrace(); 

           log.error(driverName +" not found!", e); 

           System.exit(1); 

       } catch (SQLException e){ 

           e.printStackTrace(); 

           log.error("Connectionerror!", e); 

           System.exit(1); 

       } finally { 

           try {

               if(res != null){

                   res.close();

               }

              

               if (stmt != null){ 

                   stmt.close(); 

               }

              

               if (conn != null){ 

                  conn.close();  

               }

           } catch (SQLException e){ 

               e.printStackTrace();

          

      

   }

   private static voidcountData(Statement stmt, String tableName) 

           throws SQLException{ 

       sql = "select count(1)from " + tableName; 

       log.info("Running:"+ sql); 

       res =stmt.executeQuery(sql); 

       log.info("執行“regular hive query”運行結果:"); 

       while (res.next()) { 

           log.info("count------>" + res.getString(1)); 

      

  

 

   private static voidselectData(Statement stmt, String tableName) 

           throws SQLException{ 

       sql = "select * from" + tableName; 

       log.info("Running:"+ sql); 

       res =stmt.executeQuery(sql); 

       log.info("執行 select * query 運行結果:"); 

       while (res.next()) { 

           log.info(res.getString(1)+ "\t" + res.getString(2)); 

      

  

 

    private static void loadData(Statement stmt,String tableName) 

            throws SQLException{ 

        String filepath ="/home/hadoop/software/test/t_hive.txt"; 

        sql = "load data localinpath '" + filepath + "' into table " 

                + tableName; 

       log.info("Running:" + sql); 

        stmt.execute(sql); 

   

 

    private static voiddescribeTables(Statement stmt, String tableName) 

            throws SQLException{ 

        sql = "describe "+ tableName; 

       log.info("Running:" + sql); 

        res =stmt.executeQuery(sql); 

        log.info("執行 describe table 運行結果:"); 

        while (res.next()) { 

           log.info(res.getString(1) + "\t" + res.getString(2)); 

       

   

 

    private static voidshowTables(Statement stmt, String tableName) 

            throws SQLException{ 

        sql = "show tables'" + tableName + "'"; 

       log.info("Running:" + sql); 

        res =stmt.executeQuery(sql); 

        log.info("執行 show tables 運行結果:"); 

        if (res.next()) { 

           log.info(res.getString(1)); 

       

   

 

    private static voidcreateTable(Statement stmt, String tableName) 

            throws SQLException{ 

        log.info("執行 create tables:"+tableName); 

        sql = "create table" 

                + tableName 

                + " (a string,b string,c string)  row format delimitedfields terminated by '+'"; 

        stmt.execute(sql); 

   

 

    private static StringdropTable(Statement stmt) throws SQLException { 

        // 創建的表名 

        String tableName ="t_hive"; 

        sql = "drop table" + tableName; 

        stmt.execute(sql); 

        return tableName; 

   

 

    private static ConnectiongetConn() throws ClassNotFoundException, 

            SQLException { 

       Class.forName(driverName); 

        Connection conn =DriverManager.getConnection(url, user, password); 

        return conn; 

   

 

 

用到的jar包如下:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章