將虛擬機本地csv文件導入hive

1、首先,先將windows系統下的csv文件上傳到虛擬機目錄中。我的是在/my_dbdata/下
在這裏插入圖片描述
2、啓動hive,我提前創建了一個名爲toutiao的hive業務數據庫。創建時使用的命令:

create database if not exists toutiao comment "user,news information of 136 mysql" location '/user/hive/warehouse/toutiao.db/';

之後再數據庫中創建table

hive (default)> use toutiao;

開始使用的和mysql中同樣的語句(當然一定會報錯了,,但我也貼出錯誤的代碼了)

create table user_profile(userid VARCHAR(100) NOT NULL,mobile_type VARCHAR(30),mobile_system VARCHAR(30),start_telecom VARCHAR(20) character set utf8,network_type VARCHAR(20) character set utf8,start_address VARCHAR(120) character set utf8);//錯誤,不可取)

以下正確:

create table user_profile(userid VARCHAR(100),mobile_type VARCHAR(30),mobile_system VARCHAR(30),start_telecom VARCHAR(20),network_type VARCHAR(20),start_address VARCHAR(120)) row format delimited fields terminated by ',';

其中在這裏插入圖片描述
紅框標出的語句是爲表指定格式,爲了保證導入數據時不出錯。其中’,'時自己設定的,因爲我的時csv文件,默認分隔符是“逗號”,所以選擇的逗號。這個可以根據自己的實際情況來。如果忘了設定格式,,可以將表刪掉重新建立。。drop table user_profile;
之後,就可以導入數據了

`hive (toutiao)> load data local inpath '/my_dbdata/db_user_info.csv' overwrite into table user_profile;`

之後又創建了另一個表,表中出現了新的數據類型,時間和文本。

Hive 中,可以用String、Date和Timestamp表示日期時間,String 用 yyyy-MM-dd 的形式表示,Date 用yyyy-MM-dd 的形式表示,Timestamp 用 yyyy-MM-dd hh:mm:ss 的形式表示

我使用的Timestamp,文本使用string。

hive (toutiao)> create table news_article_content(newsid VARCHAR(100) ,title string,content string,create_time Timestamp,resource string,content_type string) row format delimited fields terminated by ',';

之後也可以往這個table中導數據了

hive (toutiao)> load data local inpath '/my_dbdata/db_news_info.csv' overwrite into table news_article_content;

在這裏插入圖片描述
可見成功導入。
創建第三個表:

hive (toutiao)> create table u_news_interaction(userid varchar(100),newsid VARCHAR(100) ,event_type varchar(10),click_time Timestamp) row format delimited fields terminated by ',';

繼續開心導數據:

hive (toutiao)> load data local inpath '/my_dbdata/u_news_interaction.csv' overwrite into table u_news_interaction;

撒花

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