建立、匯入 hive 資料表

將資料載入到 hive 的 Master table 中,要做兩件事情,第一步是建立hive table,第二步則是把資料載入該 table。

Step1. create table

使用之前上傳到 hdfs 上的其中一項資料Master.csv,其內容大致如下

由於該 csv內有許多 column,因此對應到 table而言需要create 相對應的 fields ,TERMINATED BY ',' 表該 table 你要設定的分隔字元為” , “

create table Master
( lahmanID INT, playerID STRING, managerID INT, hofID STRING, 
  birthYear INT, birthMonth INT, birthDay INT, birthCountry STRING, 
  birthState STRING, birthCity STRING, deathYear INT, deathMonth INT,
  deathDay INT, deathCountry STRING, deathState STRING, deathCity STRING,
  nameFirst STRING, nameLast STRING, nameNote STRING, nameGiven STRING,
  nameNick STRING, weight INT, height INT, bats STRING, throws STRING,
  debut STRING, finalGame STRING, college STRING, lahman40ID STRING,
  lahman45ID STRING, retroID STRING, holtzID STRING, bbrefID STRING )
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' ;

Step2. Load data into table

接下來要開始長資料了

LOAD DATA INPATH "/user/<user_name>/baseball/Master.csv" OVERWRITE INTO TABLE Master;

Ps :有的網站會介紹使用 LOAD DATA LOCAL INPATH … 的語法,可免去事先將資料上傳到 hdfs 上的步驟;不過一旦你登入的server 與 hiveserver2 不同台的時候,就會出現 error : No files matching path

Step3. Select from table

Hive 的 SQL 語法稱為 HiveQL,基本上support 許多 SQL 標準語法,詳細可點 Reference 3 的連結。 使用 select 語法來看剛剛匯入的資料

select count(*) from master;

select playerID, managerID, nameFirst, nameLast  from master limit 10;

恭喜,你已經會使用 Hive 了,如果你有用過 SQL 資料庫的經驗,資料的運算應用對你來說就不陌生了。

results matching ""

    No results matching ""