將txt、csv等文本文件導入Hive
目錄
00.數據在虛擬機外
如果數據在虛擬機內,請跳過此步,直接執行接下來的操作。
推薦使用SecureCTR的SFTP傳輸至虛擬機。
具體操作步驟:使用SecureCTR連接Hadoop虛擬機,在標題欄右擊,
選擇SFTP傳輸:
然后將txt或者csv文件拖拽至新打開的窗口即可。
檢驗:連接虛擬機在根目錄下使用ls命令即可看到你傳入的文件。
01.啟動hadoop、hdfs
Hadoop所在文件
cd /usr/local/hadoop/
啟動關閉Hdfs
sbin/start-dfs.sh
sbin/stop-dfs.sh
啟動yarn
sbin/start-yarn.sh
sbin/stop-yarn.sh
02.將文件放置在hdfs目錄下
我們以txt文件為例,我們要操作的文件是sale_count.txt
使用下列命令將文件放到tmp文件夾下:
hadoop fs -put sale_count.txt /tmp
查看tmp文件夾下的文件:
hadoop fs -ls -h /tmp
03.登錄hive並進入指定數據庫
進入到hive目錄下的bin目錄:
cd /usr/local/hive/bin
啟動hive:
hadoop@dblab-VirtualBox:/usr/local/hive/bin$ hive
對數據庫操作:
//查看數據庫
show databases;
//創建數據庫
create database test;
//使用test數據庫
use test;
04.根據文件創建表
文件:
create external table if not exists sale_count(
sale_nbr string,
count string
)row format delimited fields terminated by ',' stored as textfile;
主要部分是最后一部分:row format delimited fields terminated by ',' stored as textfile;
查看表結構:
desc sale_count;
05.執行導入語句
load data inpath '/tmp/sale_count.txt' into table sale_count;
load data local inpath '/tmp/sale_count.txt' into table sale_count;
如果有local關鍵字,這個路徑應該為本地文件系統,數據會被拷貝到目標位置。
入股省略local關鍵字,這個路徑應該是分布式文件系統的路徑,數據是從這個路徑轉移到目標路徑。