日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Hive3.1.2安装指南

發布時間:2023/12/20 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hive3.1.2安装指南 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Hive3.1.2安裝指南

1、安裝Hive3.1.2

首先需要下載Hive安裝包文件, Hive官網下載地址

#解壓安裝包 hadoop@hadoop-master:~$ sudo tar xf apache-hive-3.1.2-bin.tar.gz -C /usr/local/ hadoop@hadoop-master:~$ cd /usr/local/ hadoop@hadoop-master:/usr/local$ sudo mv apache-hive-3.1.2-bin hive#把hbase目錄權限賦予給hadoop用戶: hadoop@hadoop-master:/usr/local$ sudo chown -R hadoop:hadoop hive

為了方便使用,我們把hive命令加入到環境變量中去,命令如下:

#配置環境變量 hadoop@hadoop-master:/usr/local$ vim ~/.bashrc hadoop@hadoop-master:/usr/local$ tail -3 ~/.bashrc export HIVE_HOME=/usr/local/hive export PATH=$PATH:/usr/local/hadoop/bin:/usr/local/hadoop/sbin:/usr/local/hbase/bin:$HIVE_HOME/bin export HADOOP_HOME=/usr/local/hadoop#使環境變量立即生效 hadoop@hadoop-master:/usr/local$ source ~/.bashrc

將hive-default.xml.template重命名為hive-default.xml

hadoop@hadoop-master:/usr/local$ cd /usr/local/hive/conf/ hadoop@hadoop-master:/usr/local/hive/conf$ cp -a hive-default.xml.template hive-default.xml

然后新建一個配置文件hive-site.xml,添加如下配置信息:

hadoop@hadoop-master:/usr/local/hive/conf$ nano hive-site.xml hadoop@hadoop-master:/usr/local/hive/conf$ cat hive-site.xml <?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> <configuration><property><name>javax.jdo.option.ConnectionURL</name><value>jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true</value><description>JDBC connect string for a JDBC metastore</description></property><property><name>javax.jdo.option.ConnectionDriverName</name><value>com.mysql.jdbc.Driver</value><description>Driver class name for a JDBC metastore</description></property><property><name>javax.jdo.option.ConnectionUserName</name><value>hive</value><description>username to use against metastore database</description></property><property><name>javax.jdo.option.ConnectionPassword</name><value>hive</value><description>password to use against metastore database</description></property> </configuration>

2、安裝并配置mysql

這里我們采用MySQL數據庫保存Hive的元數據,而不是采用Hive自帶的derby來存儲元數據。

首先需要安裝MySQL

#更新軟件源 hadoop@hadoop-master:~$ sudo apt-get update#安裝mysql hadoop@hadoop-master:~$ sudo apt-get -y install mysql-server#修改MySQL的編碼 hadoop@hadoop-master:~$ egrep -v "^#|^$" /etc/mysql/mysql.conf.d/mysqld.cnf ...... [mysqld] user = mysql pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 basedir = /usr datadir = /var/lib/mysql tmpdir = /tmp character_set_server=utf8 #添加此行 lc-messages-dir = /usr/share/mysql ......

將下載后的mysql jdbc包解壓

#解壓安裝包 hadoop@hadoop-master:~$ tar xf mysql-connector-java-5.1.40.tar.gz#將mysql-connector-java-5.1.40-bin.jar拷貝到/usr/local/hive/lib目錄下 hadoop@hadoop-master:~$ cp -a mysql-connector-java-5.1.40/mysql-connector-java-5.1.40-bin.jar /usr/local/hive/lib/

新建hive數據庫

#這個hive數據庫與hive-site.xml中localhost:3306/hive的hive對應,用來保存hive元數據 mysql> create database hive; #配置mysql允許hive接入: #將所有數據庫的所有表的所有權限賦給hive用戶,后面的hive是配置hive-site.xml中配置的連接密碼 mysql> grant all on *.* to hive@localhost identified by 'hive'; #刷新mysql系統權限關系表 mysql> flush privileges;

替換guava.jar包

#查看hadoop安裝目錄下share/hadoop/common/lib內guava.jar版本 hadoop@hadoop-master:~$ ll -d /usr/local/hadoop/share/hadoop/common/lib/guava-27.0-jre.jar -rw-r--r-- 1 hadoop hadoop 2747878 9月 12 2019 /usr/local/hadoop/share/hadoop/common/lib/guava-27.0-jre.jar#查看hive安裝目錄下lib內guava.jar的版本 hadoop@hadoop-master:~$ ll -d /usr/local/hive/lib/guava-19.0.jar -rw-r--r-- 1 hadoop hadoop 2308517 9月 27 2018 /usr/local/hive/lib/guava-19.0.jar#如果兩者不一致,刪除版本低的,并拷貝高版本的 hadoop@hadoop-master:~$ rm -rf /usr/local/hive/lib/guava-19.0.jar hadoop@hadoop-master:~$ cp -a /usr/local/hadoop/share/hadoop/common/lib/guava-27.0-jre.jar /usr/local/hive/lib/#重啟數據庫 hadoop@hadoop-master:~$ sudo systemctl restart mysql

使用schematool工具

#Hive現在包含一個用于 Hive Metastore 架構操控的脫機工具,名為 schematool.此工具可用于初始化當前 Hive 版本的 Metastore 架構。此外,其還可處理從較舊版本到新版本的架構升級。hadoop@hadoop-master:~$ cd /usr/local/hive hadoop@hadoop-master:/usr/local/hive$ ./bin/schematool -dbType mysql -initSchema

啟動hive之前,請確保hadoop集群已啟動

#啟動hive hadoop@hadoop-master:/usr/local/hive$ hive

使用mysql作為元數據庫時登陸使用mysql作為元數據庫時登陸啟動Hive過程中,可能出現的錯誤和解決方案如下:

【錯誤1】

【錯誤】 java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument【原因】 com.google.common.base.Preconditions.checkArgument 這是因為hive內依賴的guava.jar和hadoop內的版本不一致造成的。【解決方法】 1.查看hadoop安裝目錄下share/hadoop/common/lib內guava.jar版本 2.查看hive安裝目錄下lib內guava.jar的版本 如果兩者不一致,刪除版本低的,并拷貝高版本的 問題解決!

【錯誤2】

【錯誤】 org.datanucleus.store.rdbms.exceptions.MissingTableException: Required table missing : “VERSION” in Catalog “” Schema “”. DataNucleus requires this table to perform its persistence operations.【解決方案】 進入hive安裝目錄(比如/usr/local/hive),執行如下命令:./bin/schematool -dbType mysql -initSchema

【錯誤3】

【錯誤】 在啟動Hive時,有可能會出現Hive metastore database is not initialized的錯誤,這里給出解決方案。【原因】 以前曾經安裝了Hive或MySQL,重新安裝Hive和MySQL以后,導致版本、配置不一致。【解決方案】 使用schematool工具。Hive現在包含一個用于 Hive Metastore 架構操控的脫機工具,名為 schematool.此工具可用于初始化當前 Hive 版本的 Metastore 架構。此外,其還可處理從較舊版本到新版本的架構升級。所以,解決上述錯誤,你可以在終端執行如下命令:cd /usr/local/hive ./bin/schematool -dbType mysql -initSchema

執行后,再啟動Hive,應該就正常了。

啟動進入Hive的交互式執行環境以后,會出現如下命令提示符:

hive>

可以在里面輸入SQL語句,如果要退出Hive交互式執行環境,可以輸入如下命令:

hive>exit;

3、Hive的常用HiveQL操作

3.1 Hive基本數據類型

首先,我們簡單敘述一下HiveQL的基本數據類型。

Hive支持基本數據類型和復雜類型, 基本數據類型主要有數值類型(INT、FLOAT、DOUBLE ) 、布爾型和字符串, 復雜類型有三種:ARRAY、MAP 和 STRUCT。

  • 基本數據類型
    • TINYINT: 1個字節
    • SMALLINT: 2個字節
    • INT: 4個字節
    • BIGINT: 8個字節
    • BOOLEAN: TRUE/FALSE
    • FLOAT: 4個字節,單精度浮點型
    • DOUBLE: 8個字節,雙精度浮點型STRING 字符串
  • 復雜數據類型
    • ARRAY: 有序字段
    • MAP: 無序字段
    • STRUCT: 一組命名的字段

3.2 常用的HiveQL操作命令

Hive常用的HiveQL操作命令主要包括:數據定義、數據操作。接下來詳細介紹一下這些命令即用法。

數據定義:主要用于創建修改和刪除數據庫、表、視圖、函數和索引。

創建、修改和刪除數據庫

create database if not exists hive; #創建數據庫 show databases; #查看Hive中包含數據庫 show databases like 'h.*'; #查看Hive中以h開頭數據庫 describe databases; #查看hive數據庫位置等信息 alter database hive set dbproperties; #為hive設置鍵值對屬性 use hive; #切換到hive數據庫下 drop database if exists hive; #刪除不含表的數據庫 drop database if exists hive cascade; #刪除數據庫和它中的表

注意,除 dbproperties屬性外,數據庫的元數據信息都是不可更改的,包括數據庫名和數據庫所在的目錄位置,沒有辦法刪除或重置數據庫屬性。

創建、修改和刪除表

#創建內部表(管理表) create table if not exists hive.usr(name string comment 'username',pwd string comment 'password',address struct<street:string,city:string,state:string,zip:int>,comment 'home address',identify map<int,tinyint> comment 'number,sex') comment 'description of the table' tblproperties('creator'='me','time'='2016.1.1'); #創建外部表 create external table if not exists usr2(name string,pwd string,address struct<street:string,city:string,state:string,zip:int>,identify map<int,tinyint>) row format delimited fields terminated by ','location '/usr/local/hive/warehouse/hive.db/usr'; #創建分區表 create table if not exists usr3(name string,pwd string,address struct<street:string,city:string,state:string,zip:int>,identify map<int,tinyint>) partitioned by(city string,state string); #復制usr表的表模式 create table if not exists hive.usr1 like hive.usr;show tables in hive; show tables 'u.*'; #查看hive中以u開頭的表 describe hive.usr; #查看usr表相關信息 alter table usr rename to custom; #重命名表#為表增加一個分區 alter table usr2 add if not exists partition(city=”beijing”,state=”China”) location '/usr/local/hive/warehouse/usr2/China/beijing'; #修改分區路徑 alter table usr2 partition(city=”beijing”,state=”China”)set location '/usr/local/hive/warehouse/usr2/CH/beijing'; #刪除分區 alter table usr2 drop if exists partition(city=”beijing”,state=”China”) #修改列信息 alter table usr change column pwd password string after address;alter table usr add columns(hobby string); #增加列 alter table usr replace columns(uname string); #刪除替換列 alter table usr set tblproperties('creator'='liming'); #修改表屬性 alter table usr2 partition(city=”beijing”,state=”China”) #修改存儲屬性 set fileformat sequencefile; use hive; #切換到hive數據庫下 drop table if exists usr1; #刪除表 drop database if exists hive cascade; #刪除數據庫和它中的表

視圖和索引的創建、修改和刪除

主要語法如下,用戶可自行實現。

create view view_name as....; #創建視圖 alter view view_name set tblproperties(…); #修改視圖

因為視圖是只讀的,所以 對于視圖只允許改變元數據中的 tblproperties屬性。

#刪除視圖 drop view if exists view_name;#創建索引 create index index_name on table table_name(partition_name/column_name) as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler' with deferred rebuild....;

這里org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler是一個索引處理器,即一個實現了索引接口的Java類,另外Hive還有其他的索引實現。

alter index index_name on table table_name partition(...) rebulid; #重建索引

如果使用 deferred rebuild,那么新索引成空白狀態,任何時候可以進行第一次索引創建或重建。

show formatted index on table_name; #顯示索引 drop index if exists index_name on table table_name; #刪除索引

用戶自定義函數

在新建用戶自定義函數(UDF)方法前,先了解一下Hive自帶的那些函數。show functions; 命令會顯示Hive中所有的函數名稱:

hive> show functions; OK ! != $sum0 % & * ......

若想要查看具體函數使用方法可使用describe function函數名:

hive> describe function abs; OK abs(x) - returns the absolute value of x Time taken: 0.027 seconds, Fetched: 1 row(s)

首先編寫自己的UDF前需要繼承UDF類并實現evaluate()函數,或是繼承GenericUDF類實現initialize()函數、evaluate()函數和getDisplayString()函數,還有其他的實現方法,感興趣的用戶可以自行學習。

另外,如果用戶想在Hive中使用該UDF需要將我們編寫的Java代碼進行編譯,然后將編譯后的UDF二進制類文件(.class文件)打包成一個JAR文件,然后在Hive會話中將這個JAR文件加入到類路徑下,在通過create function語句定義好使用這個Java類的函數。

add jar <jar文件的絕對路徑>; #創建函數 create temporary function function_name; drop temporary function if exists function_name; #刪除函數

3.3 數據操作

主要實現的是將數據裝載到表中(或是從表中導出),并進行相應查詢操作,對熟悉SQL語言的用戶應該不會陌生。

向表中裝載數據

這里我們以只有兩個屬性的簡單表為例來介紹。首先創建表stu和course,stu有兩個屬性id與name,course有兩個屬性cid與sid。

#創建hive庫 hive> create database if not exists hive; OK Time taken: 0.186 seconds#創建表stu hive> create table stu(id int,name string) row format delimited fields terminated by ' '; OK Time taken: 0.082 seconds#創建表course hive> create table course(cid int,sid int) row format delimited fields terminated by ' '; OK Time taken: 0.044 seconds

向表中裝載數據有兩種方法:從文件中導入和通過查詢語句插入。

1、從文件中導入
假如這個表中的記錄存儲于文件stu.txt中,內容如下。

hadoop@hadoop-master:~$ mkdir -p /usr/local/hadoop/examples hadoop@hadoop-master:~$ vim /usr/local/hadoop/examples/stu.txt hadoop@hadoop-master:~$ cat /usr/local/hadoop/examples/stu.txt 1 xiapi 2 xiaoxue 3 qingqing

下面我們把這個文件中的數據裝載到表stu中,操作如下:

hive> use hive; OK Time taken: 0.027 secondshive> load data local inpath '/usr/local/hadoop/examples/stu.txt' overwrite into table stu; Loading data to table hive.stu OK Time taken: 0.173 secondshive> select * from stu; OK 1 xiapi 2 xiaoxue 3 qingqing Time taken: 0.096 seconds, Fetched: 3 row(s)

如果stu.txt文件存儲在HDFS 上,則不需要 local 關鍵字。

2、通過查詢語句插入

使用如下命令,創建stu1表,它和stu表屬性相同,我們要把從stu表中查詢得到的數據插入到stu1中:

hive> create table stu1 as select id,name from stu;

上面是創建表,并直接向新表插入數據;若表已經存在,向表中插入數據需執行以下命令:

insert overwrite table stu1 select id,name from stu where(條件);

這里關鍵字overwrite的作用是替換掉表(或分區)中原有數據,換成into關鍵字,直接追加到原有內容后。

從表中導出數據

1、可以簡單拷貝文件或文件夾

命令如下:

hadoop fs -cp source_path target_path;

2、寫入臨時文件

命令如下:

hive> insert overwrite local directory '/usr/local/hadoop/tmp/stu' select id,name from stu;

查詢操作

和SQL的查詢完全一樣,這里不再贅述。主要使用select…from…where…等語句,再結合關鍵字group by、having、like、rlike等操作。這里我們簡單介紹一下SQL中沒有的case…when…then…句式、join操作和子查詢操作。

case…when…then…句式和if條件語句類似,用于處理單個列的查詢結果,語句如下:

select id,name,case when id=1 then 'first' when id=2 then 'second'else 'third'

結果如下:

hive> select id,name,case when id=1 then 'first' when id=2 then 'second' else 'third' end from stu; OK 1 xiapi first 2 xiaoxue second 3 qingqing third Time taken: 0.108 seconds, Fetched: 3 row(s)

連接
連接(join)是將兩個表中在共同數據項上相互匹配的那些行合并起來, HiveQL 的連接分為內連接、左向外連接、右向外連接、全外連接和半連接 5 種。

1、內連接(等值連接)
內連接使用比較運算符根據每個表共有的列的值匹配兩個表中的行。

首先,我們先把以下內容插入到course表中(自行完成)

hive> select * from course; OK 1 3 2 1 3 1 Time taken: 0.098 seconds, Fetched: 3 row(s)

下面查詢stu和course表中學號相同的所有行,命令如下:

hive> select stu.*, course.* from stu join course on(stu .id=course .sid); ...... OK 1 xiapi 2 1 1 xiapi 3 1 3 qingqing 1 3 Time taken: 19.167 seconds, Fetched: 3 row(s)

2、左連接
左連接的結果集包括“LEFT OUTER”子句中指定的左表的所有行, 而不僅僅是連接列所匹配的行。如果左表的某行在右表中沒有匹配行, 則在相關聯的結果集中右表的所有選擇列均為空值,命令如下:

hive> select stu.*, course.* from stu left outer join course on(stu .id=course .sid); .... OK 1 xiapi 2 1 1 xiapi 3 1 2 xiaoxue NULL NULL 3 qingqing 1 3 Time taken: 18.285 seconds, Fetched: 4 row(s)

3、右連接
右連接是左向外連接的反向連接,將返回右表的所有行。如果右表的某行在左表中沒有匹配行,則將為左表返回空值。命令如下:

hive> select stu.*, course.* from stu right outer join course on(stu .id=course .sid); .... OK 3 qingqing 1 3 1 xiapi 2 1 1 xiapi 3 1 Time taken: 17.139 seconds, Fetched: 3 row(s)

4、全連接
全連接返回左表和右表中的所有行。當某行在另一表中沒有匹配行時,則另一個表的選擇列表包含空值。如果表之間有匹配行,則整個結果集包含基表的數據值。命令如下:

hive> select stu.*, course.* from stu full outer join course on(stu .id=course .sid); .... OK 1 xiapi 3 1 1 xiapi 2 1 2 xiaoxue NULL NULL 3 qingqing 1 3 Time taken: 16.741 seconds, Fetched: 4 row(s)

5、半連接
半連接是 Hive 所特有的, Hive 不支持 in 操作,但是擁有替代的方案; left semi join, 稱為半連接, 需要注意的是連接的表不能在查詢的列中,只能出現在 on 子句中。命令如下:

hive> select stu.* from stu left semi join course on(stu .id=course .sid); .... OK 1 xiapi 3 qingqing Time taken: 17.892 seconds, Fetched: 2 row(s)

子查詢
標準 SQL 的子查詢支持嵌套的 select 子句,HiveQL 對子查詢的支持很有限,只能在from 引導的子句中出現子查詢。

注意,在定義或是操作表時,不要忘記指定所需數據庫。

4、Hive簡單編程實踐

下面我們以詞頻統計算法為例,來介紹怎么在具體應用中使用Hive。詞頻統計算法又是最能體現MapReduce思想的算法之一,這里我們可以對比它在MapReduce中的實現,來說明使用Hive后的優勢。

MapReduce實現詞頻統計的代碼可以通過下載Hadoop源碼后,在 $HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar 包中找到(wordcount類),wordcount類由63行Java代碼編寫而成。下面首先簡單介紹一下怎么使用MapReduce中wordcount類來統計單詞出現的次數,具體步驟如下:

1、創建input目錄,output目錄會自動生成。其中input為輸入目錄,output目錄為輸出目錄。命令如下:

hadoop@hadoop-master:~$ hdfs dfs -mkdir input

2、然后,在input文件夾中創建兩個測試文件file1.txt和file2.txt,命令如下:

hadoop@hadoop-master:~$ echo "hello world" > file1.txt hadoop@hadoop-master:~$ echo "hello hadoop" > file2.txt

3、上傳

hadoop@hadoop-master:~$ hdfs dfs -put /home/hadoop/file* input/hadoop@hadoop-master:~$ hdfs dfs -ls input/ Found 2 items -rw-r--r-- 1 hadoop supergroup 12 2022-04-25 16:02 input/file1.txt -rw-r--r-- 1 hadoop supergroup 13 2022-04-25 16:02 input/file2.txt

4、執行如下hadoop命令:

hadoop@hadoop-master:~$ cd /usr/local/hadoop/hadoop@hadoop-master:/usr/local/hadoop$ hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar wordcount input output

5、我們可以到output文件夾中查看結果,結果如下:

hadoop@hadoop-master:/usr/local/hadoop$ hdfs dfs -cat output/* hadoop 1 hello 2 world 1

下面我們通過HiveQL實現詞頻統計功能,此時只要編寫下面7行代碼,而且不需要進行編譯生成jar來執行。HiveQL實現命令如下:

hive> create table docs(line string); OKhive> load data inpath 'file:///usr/local/hadoop/input' overwrite into table docs; Loading data to table hive.docs OK Time taken: 0.65 secondshive> select * from docs; OK hello world hello hadoop Time taken: 1.06 seconds, Fetched: 2 row(s) hive> create table word_count as > select word, count(1) as count from> (select explode(split(line,' '))as word from docs) w> group by word> order by word;> order by word;hive> select * from word_count; OK hadoop 1 hello 2 world 1 Time taken: 0.117 seconds, Fetched: 3 row(s)

由上可知,采用Hive實現最大的優勢是,對于非程序員,不用學習編寫Java MapReduce代碼了,只需要用戶學習使用HiveQL就可以了,而這對于有SQL基礎的用戶而言是非常容易的。

本文參考:http://dblab.xmu.edu.cn/blog/2440-2/

總結

以上是生活随笔為你收集整理的Hive3.1.2安装指南的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。