將txt、csv等文本文件導入Hive


將txt、csv等文本文件導入Hive



00.數據在虛擬機外


如果數據在虛擬機內,請跳過此步,直接執行接下來的操作。

推薦使用SecureCTR的SFTP傳輸至虛擬機。

具體操作步驟:使用SecureCTR連接Hadoop虛擬機,在標題欄右擊,

image-20201129110959994

選擇SFTP傳輸:

image-20201129111127160

然后將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

image-20201129111859136

使用下列命令將文件放到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.根據文件創建表


文件:

image-20201129163358616

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關鍵字,這個路徑應該是分布式文件系統的路徑,數據是從這個路徑轉移到目標路徑。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM