hive學習(1)---導入外部數據到hive的表中,爲學習hql做準備

在學習hive前,首先需要準備一份樣例數據,然後把這個數據加載進hive的對應表中,

這裏我用java寫file的方式創建了3個關於城市天氣情況的數據,每個字段以空格隔開,\r\n換行,例子如下:

2014-05-23|07:33:58 China beijin cloudy -12 -4 189


然後,用FlashFXP把這3個文件上傳到用戶目錄下,並把它拷貝到dfs的/tmp目錄下:

[hadoop@Master ~]$ hdfs dfs -put wether*.txt /tmp


然後,創建一個hive表,叫做weather,代碼如下:

hive> 
    > create table weather
    > (date string, country string,
    > city string, weath string, 
    > minTemperat int, maxTemperat int,
    > pmvalue int)
    > ROW FORMAT DELIMITED
    > FIELDS TERMINATED BY ' '
    > STORED AS TEXTFILE;

PS:此處要注意各個字段和類型

最後,表創建成功後,就需要把這3個文件的內容導入到表中:

hive> load data inpath '/tmp/wether*.txt' into table weather;

Loading data to table default.weather
Table default.weather stats: [num_partitions: 0, num_files: 3, num_rows: 0, total_size: 30888799, raw_data_size: 0]
OK
Time taken: 0.653 seconds

日誌顯示將3個文件的數據導入到了其中,此時其實是把文件移動到了表在dfs對應的目錄下,用下面的語句驗證:

hive> dfs -ls /user/hive/warehouse/weather;
Found 3 items
-rw-r--r--   3 hadoop supergroup    5120124 2014-05-23 07:55 /user/hive/warehouse/weather/wetherdata1.txt
-rw-r--r--   3 hadoop supergroup   10293769 2014-05-23 07:55 /user/hive/warehouse/weather/wetherdata2.txt
-rw-r--r--   3 hadoop supergroup   15474906 2014-05-23 07:55 /user/hive/warehouse/weather/wetherdata3.txt
hive> 



最後來看下weather表中是否有數據:

hive> select * from weather;
OK
2014-05-23|07:33:58     China   beijin  cloudy  -12     -4      189
2014-05-23|07:33:58     China   shenzhen        rain    -29     -8      199

...


由於數據太多,此處只好CTRL+Z退出了,下一步要做到就是通過hql來查找自己想要的數據




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章