Hello World on Impala

來源:互聯網
上載者:User

標籤:impala   hello   cloudera   hdfs   linux   

Cloudera Impala 官方教程 《Impala Tutorial》,講解了Impala一些基本操作,但操作步驟前後缺少連貫性,本文節W選《Impala Tutorial》中的部分樣本,從零開始講解了一個完整樣本:建立表、載入資料、查詢資料。提供了一個入門級教程,通過本文的操作,向Impala說“Hello World”。

本文假設你已經具備了安裝好的Impala環境,環境搭建可以參考: CDH5上安裝Hive,HBase,Impala,Spark等服務

建立cloudera使用者和組

Impala Tutorial中樣本的登入使用者名稱為cloudera,但Cloudera Manager 5.0.2 安裝時並沒有自動在主機節點(例如:h1.worker.com)上建立cloudera使用者,為了和Impala Tutorial 中樣本一致, 需要手工建立cloudera使用者和組。

以root使用者身份登入主機節點(例如:h1.worker.com),先檢查下是否存在cloudera使用者,執行如下的命令:

[[email protected] home]# cat /etc/passwd | grep clouderacloudera-scm:x:496:493:Cloudera Manager:/var/run/cloudera-scm-server:/sbin/nologin
上面顯示不存在cloudera使用者。如果存在,則不需要進行下面的建立使用者步驟了。

建立cloudera使用者和組,並設定密碼為cloudera:

[[email protected] home]# groupadd cloudera[[email protected] home]# useradd -g cloud era cloudera[[email protected] home]# passwd clouderaChanging password for user cloudera.anNew password:BAD PASSWORD: it is based on a dictionary wordRetype new password:passwd: all authentication tokens updated successfully.

在HDFS上建立/user/cloudera檔案夾

我們需要在HDFS上建立/user/cloudera檔案夾,並將這個檔案夾的所有者修改為cloudera,這需要HDFS的超級使用者才有許可權執行這些操作。HDFS的超級使用者即運行name node進程的使用者。寬泛的講,如果你啟動了name node,你就是超級使用者。通過Cloudera Manager 5安裝環境的超級使用者名稱為:hdfs

切換到HDFS的超級使用者,先檢查是否存在 /user/cloudera 檔案夾,如果不存在則建立。

[[email protected] home]# su - hdfs-bash-4.1$ hdfs dfs -ls /userFound 7 itemsdrwx------   - hdfs   supergroup          0 2014-06-26 08:44 /user/hdfsdrwxrwxrwx   - mapred hadoop              0 2014-06-20 10:10 /user/historydrwxrwxr-t   - hive   hive                0 2014-06-20 10:13 /user/hivedrwxrwxr-x   - impala impala              0 2014-06-20 10:18 /user/impaladrwxrwxr-x   - oozie  oozie               0 2014-06-20 10:15 /user/ooziedrwxr-x--x   - spark  spark               0 2014-06-20 10:08 /user/sparkdrwxrwxr-x   - sqoop2 sqoop               0 2014-06-20 10:16 /user/sqoop2
在HDFS上建立 /user/cloudera 目錄,設定目錄的所有者和組為cloudera
-bash-4.1$ hdfs dfs -mkdir -p /user/cloudera-bash-4.1$ hdfs dfs -chown cloudera:cloudera /user/cloudera-bash-4.1$ hdfs dfs -ls /userFound 8 itemsdrwxr-xr-x   - cloudera cloudera            0 2014-06-26 09:05 /user/clouderadrwx------   - hdfs     supergroup          0 2014-06-26 08:44 /user/hdfsdrwxrwxrwx   - mapred   hadoop              0 2014-06-20 10:10 /user/historydrwxrwxr-t   - hive     hive                0 2014-06-20 10:13 /user/hivedrwxrwxr-x   - impala   impala              0 2014-06-20 10:18 /user/impaladrwxrwxr-x   - oozie    oozie               0 2014-06-20 10:15 /user/ooziedrwxr-x--x   - spark    spark               0 2014-06-20 10:08 /user/sparkdrwxrwxr-x   - sqoop2   sqoop               0 2014-06-20 10:16 /user/sqoop2

經過以上的操作已經具備了運行 Impala Tutorial中樣本的條件。

HDFS上建立裝載表資料的目錄本節示範如何建立一些非常小的表,適合初次使用的使用者實驗 Impala SQL 功能。 TAB1 和 TAB2 從 HDFS 檔案中載入資料。可以把你想查詢的資料放入 HDFS 中。想開始這一過程,先在你的 HDFS 使用者目錄下建立一個或多個子目錄。每個表中的資料存放在單獨的子目錄裡。這個例子使用 mkdir 中的 -p 選項,這樣如果不存在的父目錄中則自動建立。

[[email protected] ~]# su - cloudera[[email protected] ~]$ whoamicloudera[[email protected] ~]$ hdfs dfs -ls /userFound 8 itemsdrwxr-xr-x   - cloudera cloudera            0 2014-06-26 09:05 /user/clouderadrwx------   - hdfs     supergroup          0 2014-06-26 08:44 /user/hdfsdrwxrwxrwx   - mapred   hadoop              0 2014-06-20 10:10 /user/historydrwxrwxr-t   - hive     hive                0 2014-06-20 10:13 /user/hivedrwxrwxr-x   - impala   impala              0 2014-06-20 10:18 /user/impaladrwxrwxr-x   - oozie    oozie               0 2014-06-20 10:15 /user/ooziedrwxr-x--x   - spark    spark               0 2014-06-20 10:08 /user/sparkdrwxrwxr-x   - sqoop2   sqoop               0 2014-06-20 10:16 /user/sqoop2[[email protected] ~]$ hdfs dfs -mkdir -p /user/cloudera/sample_data/tab1 /user/cloudera/sample_data/tab2[[email protected] ~]$ 

通過以上的操作,就建立了存放TAB1 和 TAB2表資料的目錄。

csv檔案存放到HDFS目錄拷貝如下的兩個.csv檔案到本地的檔案系統。

tab1.csv:

1,true,123.123,2012-10-24 08:55:00 2,false,1243.5,2012-10-25 13:40:003,false,24453.325,2008-08-22 09:33:21.1234,false,243423.325,2007-05-12 22:32:21.334545,true,243.325,1953-04-22 09:11:33
tab2.csv:
1,true,12789.1232,false,1243.53,false,24453.3254,false,2423.32545,true,243.32560,false,243565423.32570,true,243.32580,false,243423.32590,true,243.325

執行下面的命令將兩個 .csv 檔案放入單獨的 HDFS 目錄:

[[email protected] testdata]$ pwd/home/cloudera/testdata[[email protected] testdata]$ lltotal 8-rw-rw-r--. 1 cloudera cloudera 193 Jun 27 08:33 tab1.csv-rw-rw-r--. 1 cloudera cloudera 158 Jun 27 08:34 tab2.csv[[email protected] testdata]$ hdfs dfs -put tab1.csv /user/cloudera/sample_data/tab1[[email protected] testdata]$ hdfs dfs -ls /user/cloudera/sample_data/tab1Found 1 items-rw-r--r--   3 cloudera cloudera        193 2014-06-27 08:35 /user/cloudera/sample_data/tab1/tab1.csv[[email protected] testdata]$ hdfs dfs -put tab2.csv /user/cloudera/sample_data/tab2[[email protected] testdata]$ hdfs dfs -ls /user/cloudera/sample_data/tab2Found 1 items-rw-r--r--   3 cloudera cloudera        158 2014-06-27 08:36 /user/cloudera/sample_data/tab2/tab2.csv[[email protected] testdata]$ 
每個資料檔案的名稱不重要。事實上,當 Impala 第一次檢測資料目錄的內容時,它認為目錄下的所有檔案都是表中的資料檔案,無論目錄下有多少檔案,無論什麼樣的檔案名稱。
要瞭解你的 HDFS 檔案系統中什麼目錄可用,不同的目錄和檔案都有什麼許可權,執行 hdfs dfs -ls / 並沿著看到的分類樹一直執行 -ls 操作。

建立表,載入資料

使用 impala-shell 命令建立表,可以用互動式建立,也可以用 SQL 指令碼。

下面的例子示範建立了三個表。每個表中的列都使用了不同的資料類型,如 Boolean 或 integer。 例子還包括了如何格式資料的命令,例如列以逗號分隔,這樣從 .csv 檔案匯入資料。我們已經有了存放在 HDFS 分類樹中的包含資料的 .csv 檔案,我們給表指定了包含對應 .csv 檔案的路徑位置。Impala 認為這些目錄下的所有檔案裡的所有資料都是表裡的資料。

table_setup.sql 檔案包含如下內容:

DROP TABLE IF EXISTS tab1;-- The EXTERNAL clause means the data is located outside the central location for Impala data files-- and is preserved when the associated Impala table is dropped. We expect the data to already-- exist in the directory specified by the LOCATION clause.CREATE EXTERNAL TABLE tab1(   id INT,   col_1 BOOLEAN,   col_2 DOUBLE,   col_3 TIMESTAMP)ROW FORMAT DELIMITED FIELDS TERMINATED BY ','LOCATION '/user/cloudera/sample_data/tab1';DROP TABLE IF EXISTS tab2;-- TAB2 is an external table, similar to TAB1.CREATE EXTERNAL TABLE tab2(   id INT,   col_1 BOOLEAN,   col_2 DOUBLE)ROW FORMAT DELIMITED FIELDS TERMINATED BY ','LOCATION '/user/cloudera/sample_data/tab2';DROP TABLE IF EXISTS tab3;-- Leaving out the EXTERNAL clause means the data will be managed-- in the central Impala data directory tree. Rather than reading-- existing data files when the table is created, we load the-- data after creating the table.CREATE TABLE tab3(   id INT,   col_1 BOOLEAN,   col_2 DOUBLE,   month INT,   day INT)ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

執行 table_setup.sql 指令碼,使用:
impala-shell -i 172.16.230.152 -f table_setup.sql
操作步驟如下:

[[email protected] testdata]$ pwd/home/cloudera/testdata[[email protected] testdata]$ lltotal 12-rw-rw-r--. 1 cloudera cloudera  193 Jun 27 08:33 tab1.csv-rw-rw-r--. 1 cloudera cloudera  158 Jun 27 08:34 tab2.csv-rw-rw-r--. 1 cloudera cloudera 1106 Jun 27 08:49 table_setup.sql[[email protected] testdata]$ impala-shell -i 172.16.230.152 -f table_setup.sqlStarting Impala Shell without Kerberos authenticationConnected to 172.16.230.152:21000Server version: impalad version 1.3.1-cdh5 RELEASE (build )......Returned 0 row(s) in 0.28s[[email protected] testdata]$ 

查看 Impala 表結構登入impala-shell,執行下面的命令:show tables;
describe tab1;
操作步驟如下:
[[email protected] testdata]$ impala-shell -i 172.16.230.152Starting Impala Shell without Kerberos authenticationConnected to 172.16.230.152:21000Server version: impalad version 1.3.1-cdh5 RELEASE (build )Welcome to the Impala shell. Press TAB twice to see a list of available commands.Copyright (c) 2012 Cloudera, Inc. All rights reserved.(Shell build version: Impala Shell v1.3.1-cdh5 () built on Mon Jun  9 09:30:26 PDT 2014)[172.16.230.152:21000] > show tables;Query: show tables+------+| name |+------+| tab1 || tab2 || tab3 |+------+Returned 3 row(s) in 0.01s[172.16.230.152:21000] > describe tab1;Query: describe tab1+-------+-----------+---------+| name  | type      | comment |+-------+-----------+---------+| id    | int       |         || col_1 | boolean   |         || col_2 | double    |         || col_3 | timestamp |         |+-------+-----------+---------+Returned 4 row(s) in 6.85s[172.16.230.152:21000] > quit;Goodbye[[email protected] testdata]$ 

查詢 Impala 表

登入impala-shell,執行如下的sql語句:

SELECT * FROM tab1;

SELECT * FROM tab2 LIMIT 5;

SELECT tab2.*
FROM tab2,
(SELECT tab1.col_1, MAX(tab2.col_2) AS max_col2
 FROM tab2, tab1
 WHERE tab1.id = tab2.id
 GROUP BY col_1) subquery1
WHERE subquery1.max_col2 = tab2.col_2;

操作步驟如下:

[[email protected] testdata]$ impala-shell -i 172.16.230.152Starting Impala Shell without Kerberos authenticationConnected to 172.16.230.152:21000Server version: impalad version 1.3.1-cdh5 RELEASE (build )Welcome to the Impala shell. Press TAB twice to see a list of available commands.Copyright (c) 2012 Cloudera, Inc. All rights reserved.(Shell build version: Impala Shell v1.3.1-cdh5 () built on Mon Jun  9 09:30:26 PDT 2014)[172.16.230.152:21000] > SELECT * FROM tab1;Query: select * FROM tab1+----+-------+------------+-------------------------------+| id | col_1 | col_2      | col_3                         |+----+-------+------------+-------------------------------+| 1  | true  | 123.123    | 2012-10-24 08:55:00           || 2  | false | 1243.5     | 2012-10-25 13:40:00           || 3  | false | 24453.325  | 2008-08-22 09:33:21.123000000 || 4  | false | 243423.325 | 2007-05-12 22:32:21.334540000 || 5  | true  | 243.325    | 1953-04-22 09:11:33           |+----+-------+------------+-------------------------------+Returned 5 row(s) in 2.39s[172.16.230.152:21000] > SELECT * FROM tab2 LIMIT 5;Query: select * FROM tab2 LIMIT 5+----+-------+-----------+| id | col_1 | col_2     |+----+-------+-----------+| 1  | true  | 12789.123 || 2  | false | 1243.5    || 3  | false | 24453.325 || 4  | false | 2423.3254 || 5  | true  | 243.325   |+----+-------+-----------+Returned 5 row(s) in 1.30s[172.16.230.152:21000] > SELECT tab2.*                       > FROM tab2,                       > (SELECT tab1.col_1, MAX(tab2.col_2) AS max_col2                       >  FROM tab2, tab1                       >  WHERE tab1.id = tab2.id                       >  GROUP BY col_1) subquery1                       > WHERE subquery1.max_col2 = tab2.col_2;Query: select tab2.* FROM tab2, (SELECT tab1.col_1, MAX(tab2.col_2) AS max_col2 FROM tab2, tab1 WHERE tab1.id = tab2.id GROUP BY col_1) subquery1 WHERE subquery1.max_col2 = tab2.col_2+----+-------+-----------+| id | col_1 | col_2     |+----+-------+-----------+| 1  | true  | 12789.123 || 3  | false | 24453.325 |+----+-------+-----------+Returned 2 row(s) in 1.02s[172.16.230.152:21000] > quit;Goodbye[[email protected] testdata]$ 


結束語:

本文講解了一個Impala使用的基本樣本,提供了一個入門指導,更多的樣本參見: Impala Tutorial

本文使用了許多 impala-shell 命令的方法,具體參見 Using the Impala Shell (impala-shell Command)

原創作品,轉載請註明出處 http://blog.csdn.net/yangzhaohui168/article/details/35340387




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.