hive應用示例

2021-09-02 03:46:37 字數 1808 閱讀 1167

簡單示例

我們以以下資料作為測試資料,結構為(班級號,學號,成績)。

c01,n0101,82

c01,n0102,59

c01,n0103,65

c02,n0201,81

c02,n0202,82

c02,n0203,79

c03,n0301,56

c03,n0302,92

c03,n0306,72

執行以下命令:

create table student(classnostring, stuno string, score int) row format delimited fields terminated by ',';

其中,定義表結構和sql類似.。其它設定表示欄位間以逗號分隔,一行為乙個記錄。

load data local inpath '/home/user/input/student.txt'overwrite into table student;

輸出結果如下:

copying data fromfile:/home/user/input/student.txt

copying file:file:/home/user/input/student.txt

loading data to tabledefault.student

rmr: deprecated: please use 'rm-r' instead.

deleted/user/hive/warehouse/student

table default.student stats:[num_partitions: 0, num_files: 1, num_rows: 0, total_size: 117, raw_data_size:0]

這個命令將student.txt檔案內容載入到表student中。這個載入操作將直接把student.txt檔案複製到hive的warehouse目錄中,這個目錄由hive.metastore.warehouse.dir配置項設定,預設值為/user/hive/warehouse。overwrite選項將導致hive事先刪除student目錄下所有的檔案。

hive不會對student.txt做任何格式處理,因為hive本身並不強調資料的儲存格式。

此例中,hive將資料儲存於hdfs系統中。當然,hive也可以將資料儲存於本地。

如果不加overwrite選項,且載入的檔案在hive中已經存在,則hive會為檔案重新命名。比如不加overwrite選項將以上命令執行兩次,則第二次載入後,hive中新產生的檔名將會是「student_copy_1.txt」。(和hadoop權威教程中描述的不一致,讀者請慎重驗證)

接下來,我們執行以下命令:

select * from student;

輸出如下:

c01 n0101 82

c01 n0102 59

c01 n0103 65

c02 n0201 81

c02 n0202 82

c02 n0203 79

c03 n0301 56

c03 n0302 92

c03 n0306 72

執行以下命令:

select classno,count(score) fromstudent where score>=60 group by classno;

輸出如下:

c01 2

c02 3

c03 2

由此看見,hiveql的使用和sql及其類似。我們用到了group和count,其實在後台hive將這些操作都轉換成了mapreduce操作提交給hadoop執行,並最終輸出結果。

Hive 高階應用開發示例 二

hive的一些常用的高階開發 內容 1.開窗函式 2.行轉列,列轉行,多行轉一行,一行轉多行 3.分組 增強型group 4.排序 5.關聯 本次的內容 分組 排序 關聯 1.分組 group by group by with rollup group by with cube group by g...

hive 外部表 建立示例

hdfs dfs mkdir p external sr sr created hdfs dfs mkdir p external sr sr assign hdfs dfs mkdir p external sr sr cancelled hdfs dfs mkdir p external sr ...

hive經典應用

dual的構造 自己構造即可乙個函式幾個,在隨後的select 測試 from dual 前後兩行求時間差 1.hive row number 函式的高階用法 row num 按照某個字段分割槽顯示第幾條資料 select imei,ts,fuel instant,gps longitude,gps...