https://blog.csdn.net/wiborgite/article/details/78813342html
背景說明:git
基於CHD quick VM環境,在一個VM中同時包含了HDFS、YARN、HBase、Hive、Impala等組件。shell
本文將一個文本數據從HDFS加載到Hive,同步元數據後,在Impala中進行數據操做。數據庫
-----------------------------------------------------------------------------------------Linux Shell的操做-----------------------------------------------------------app
一、將PC本地的數據文件上傳到VM中/home/data目錄下oop
-
[root@quickstart data]# pwd
-
-
[root@quickstart data]# ls
-
p10pco2a.dat stock_data2.csv
-
[root@quickstart data]# head p10pco2a.dat
-
WOCE_P10,1993,279.479,-16.442,172.219,24.9544,34.8887,1.0035,363.551,2
-
WOCE_P10,1993,279.480,-16.440,172.214,24.9554,34.8873,1.0035,363.736,2
-
WOCE_P10,1993,279.480,-16.439,172.213,24.9564,34.8868,1.0033,363.585,2
-
WOCE_P10,1993,279.481,-16.438,172.209,24.9583,34.8859,1.0035,363.459,2
-
WOCE_P10,1993,279.481,-16.437,172.207,24.9594,34.8859,1.0033,363.543,2
-
WOCE_P10,1993,279.481,-16.436,172.205,24.9604,34.8858,1.0035,363.432,2
-
WOCE_P10,1993,279.489,-16.417,172.164,24.9743,34.8867,1.0036,362.967,2
-
WOCE_P10,1993,279.490,-16.414,172.158,24.9742,34.8859,1.0035,362.960,2
-
WOCE_P10,1993,279.491,-16.412,172.153,24.9747,34.8864,1.0033,362.998,2
-
WOCE_P10,1993,279.492,-16.411,172.148,24.9734,34.8868,1.0031,363.022,2
二、將/home/data/p10pco2a.dat文件上傳到HDFSui
-
[root@quickstart data]# hdfs dfs -put p10pco2a.dat /tmp/
-
[root@quickstart data]# hdfs dfs -ls /tmp
-
-rw-r--r-- 1 root supergroup 281014 2017-12-14 18:47 /tmp/p10pco2a.dat
-----------------------------------------------------------------------Hive的操做----------------------------------------------------------------------------spa
一、啓動Hive CLI.net
# hive
二、Hive中建立數據庫code
CREATE DATABASE weather;
三、Hive中建立表
-
create table weather.weather_everydate_detail
-
-
-
-
-
-
-
-
-
-
-
-
-
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';
四、將HDFS中的數據加載到已建立的Hive表中
-
LOAD DATA INPATH '/tmp/p10pco2a.dat' INTO TABLE weather.weather_everydate_detail;
-
-
hive> LOAD DATA INPATH '/tmp/p10pco2a.dat' INTO TABLE weather.weather_everydate_detail;
-
Loading data to table weather.weather_everydate_detail
-
Table weather.weather_everydate_detail stats: [numFiles=1, totalSize=281014]
-
-
Time taken: 1.983 seconds
五、查看Hive表確保數據已加載
-
-
select * from weather.weather_everydate_detail limit 10;
-
select count(*) from weather.weather_everydate_detail;
-
hive> select * from weather.weather_everydate_detail limit 10;
-
-
WOCE_P10 1993 279.479 -16.442 172.219 24.9544 34.8887 1.0035 363.551 2
-
WOCE_P10 1993 279.48 -16.44 172.214 24.9554 34.8873 1.0035 363.736 2
-
WOCE_P10 1993 279.48 -16.439 172.213 24.9564 34.8868 1.0033 363.585 2
-
WOCE_P10 1993 279.481 -16.438 172.209 24.9583 34.8859 1.0035 363.459 2
-
WOCE_P10 1993 279.481 -16.437 172.207 24.9594 34.8859 1.0033 363.543 2
-
WOCE_P10 1993 279.481 -16.436 172.205 24.9604 34.8858 1.0035 363.432 2
-
WOCE_P10 1993 279.489 -16.417 172.164 24.9743 34.8867 1.0036 362.967 2
-
WOCE_P10 1993 279.49 -16.414 172.158 24.9742 34.8859 1.0035 362.96 2
-
WOCE_P10 1993 279.491 -16.412 172.153 24.9747 34.8864 1.0033 362.998 2
-
WOCE_P10 1993 279.492 -16.411 172.148 24.9734 34.8868 1.0031 363.022 2
-
Time taken: 0.815 seconds, Fetched: 10 row(s)
-
hive> select count(*) from weather.weather_everydate_detail;
-
Query ID = root_20171214185454_c783708d-ad4b-46cc-9341-885c16a286fe
-
-
-
Number of reduce tasks determined at compile time: 1
-
In order to change the average load for a reducer (in bytes):
-
set hive.exec.reducers.bytes.per.reducer=
<number>
-
In order to limit the maximum number of reducers:
-
set hive.exec.reducers.max=
<number>
-
In order to set a constant number of reducers:
-
set mapreduce.job.reduces=
<number>
-
Starting Job = job_1512525269046_0001, Tracking URL = http://quickstart.cloudera:8088/proxy/application_1512525269046_0001/
-
Kill Command = /usr/lib/hadoop/bin/hadoop job -kill job_1512525269046_0001
-
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 1
-
2017-12-14 18:55:27,386 Stage-1 map = 0%, reduce = 0%
-
2017-12-14 18:56:11,337 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 39.36 sec
-
2017-12-14 18:56:18,711 Stage-1 map = 100%, reduce = 100%, Cumulative CPU 41.88 sec
-
MapReduce Total cumulative CPU time: 41 seconds 880 msec
-
Ended Job = job_1512525269046_0001
-
-
Stage-Stage-1: Map: 1 Reduce: 1 Cumulative CPU: 41.88 sec HDFS Read: 288541 HDFS Write: 5 SUCCESS
-
Total MapReduce CPU Time Spent: 41 seconds 880 msec
-
-
-
Time taken: 101.82 seconds, Fetched: 1 row(s)
六、執行一個普通查詢:
-
hive> select * from weather_everydate_detail where sur_sal=34.8105;
-
-
WOCE_P10 1993 312.148 34.602 141.951 24.0804 34.8105 1.0081 361.29 2
-
WOCE_P10 1993 312.155 34.602 141.954 24.0638 34.8105 1.0079 360.386 2
-
Time taken: 0.138 seconds, Fetched: 2 row(s)
-
hive> select * from weather_everydate_detail where sur_sal=34.8105;
-
-
WOCE_P10 1993 312.148 34.602 141.951 24.0804 34.8105 1.0081 361.29 2
-
WOCE_P10 1993 312.155 34.602 141.954 24.0638 34.8105 1.0079 360.386 2
-
Time taken: 1.449 seconds, Fetched: 2 row(s)
-----------------------------------------------------------------------------------------------------Impala的操做-----------------------------------------------------------
一、啓動Impala CLI
# impala-shell
二、在Impala中同步元數據
-
[quickstart.cloudera:21000] > INVALIDATE METADATA;
-
Query: invalidate METADATA
-
Query submitted at: 2017-12-14 19:01:12 (Coordinator: http://quickstart.cloudera:25000)
-
Query progress can be monitored at: http://quickstart.cloudera:25000/query_plan?query_id=43460ace5d3a9971:9a50f46600000000
-
-
Fetched 0 row(s) in 3.25s
三、在Impala中查看Hive中表的結構
-
[quickstart.cloudera:21000] > use weather;
-
-
[quickstart.cloudera:21000] > desc weather.weather_everydate_detail;
-
Query: describe weather.weather_everydate_detail
-
+---------+--------+---------+
-
| name | type | comment |
-
+---------+--------+---------+
-
-
-
-
-
-
-
-
-
-
-
+---------+--------+---------+
-
Fetched 10 row(s) in 3.70s
四、查詢記錄數量
-
[quickstart.cloudera:21000] > select count(*) from weather.weather_everydate_detail;
-
Query: select count(*) from weather.weather_everydate_detail
-
Query submitted at: 2017-12-14 19:03:11 (Coordinator: http://quickstart.cloudera:25000)
-
Query progress can be monitored at: http://quickstart.cloudera:25000/query_plan?query_id=5542894eeb80e509:1f9ce37f00000000
-
-
-
-
-
-
Fetched 1 row(s) in 2.51s
說明:對比Impala與Hive中的count查詢,2.15 VS 101.82,Impala的優點仍是至關明顯的
五、執行一個普通查詢
-
[quickstart.cloudera:21000] > select * from weather_everydate_detail where sur_sal=34.8105;
-
Query: select * from weather_everydate_detail where sur_sal=34.8105
-
Query submitted at: 2017-12-14 19:20:27 (Coordinator: http://quickstart.cloudera:25000)
-
Query progress can be monitored at: http://quickstart.cloudera:25000/query_plan?query_id=c14660ed0bda471f:d92fcf0e00000000
-
+----------+------+---------+--------+---------+---------+---------+---------+---------+----+
-
| section | year | date | latim | longit | sur_tmp | sur_sal | atm_per | xco2a | qf |
-
+----------+------+---------+--------+---------+---------+---------+---------+---------+----+
-
| WOCE_P10 | 1993 | 312.148 | 34.602 | 141.951 | 24.0804 | 34.8105 | 1.0081 | 361.29 | 2 |
-
| WOCE_P10 | 1993 | 312.155 | 34.602 | 141.954 | 24.0638 | 34.8105 | 1.0079 | 360.386 | 2 |
-
+----------+------+---------+--------+---------+---------+---------+---------+---------+----+
-
Fetched 2 row(s) in 0.25s
-
[quickstart.cloudera:21000] > select * from weather_everydate_detail where sur_tmp=24.0804;
-
Query: select * from weather_everydate_detail where sur_tmp=24.0804
-
Query submitted at: 2017-12-14 23:15:32 (Coordinator: http://quickstart.cloudera:25000)
-
Query progress can be monitored at: http://quickstart.cloudera:25000/query_plan?query_id=774e2b3b81f4eed7:8952b5b400000000
-
+----------+------+---------+--------+---------+---------+---------+---------+--------+----+
-
| section | year | date | latim | longit | sur_tmp | sur_sal | atm_per | xco2a | qf |
-
+----------+------+---------+--------+---------+---------+---------+---------+--------+----+
-
| WOCE_P10 | 1993 | 312.148 | 34.602 | 141.951 | 24.0804 | 34.8105 | 1.0081 | 361.29 | 2 |
-
+----------+------+---------+--------+---------+---------+---------+---------+--------+----+
-
Fetched 1 row(s) in 3.86s
6.結論
對於Hive中須要編譯爲mapreduce執行的SQL,在Impala中執行是有明顯的速度優點的,可是Hive也不是全部的查詢都要編譯爲mapreduce,此類型的查詢,impala相比於Hive就沒啥優點了。